Upload File to Google Drive Using Ruby
Using Google Drive API with Ruby
A few weeks ago I was looking for a fashion to implement Google Drive in 1 of my personal projects but the only affair I establish was a dark documentation and no useful stackoverflow answers to solve my trouble. Afterwards some hours I managed to decrypt the documentation and decided to print my own "how to apply the Google Bulldoze API with Ruby-red".
The projection
The idea backside my problem is to create an interface to shop and recall large quantities of files like student photos, schoolhouse grades, and nigh anything. With that in heed I await for an S3 (which results a litle expensive for my needs), and also for a cocky-managed storage using minio, but fugitive the cost of storage results in an equally expensive cost for a server, and then the right respond for my needs was to utilise Google Drive which has a practiced daily margin of get and upload requests.
The solution
As I said I used Google Drive to solve my problem, and I did it through google_drive gem which give usa an interface to connect the API and make the get and upload requests we demand.
First we have to bargain with hallmark, which commonly is the biggest problem when we try to utilize any external APIs merely in this case it results actually easy simply I will not become into the details, just follow this guide using service account to authenticate will give yous a resulting json file to download which we are going to use for configuration. Once you have the file save it where you like, I commonly save this kind of things into a config binder
main/
...projection scripts
downloads/
...Will contain our downloaded files
config/
|__ config_file.json # really this file will have kinda ID proper noun but we will refer to information technology as config_file.json
With the file already saved nosotros can start to create our scripts. For educational purposes only I will show y'all how to upload and retrieve a file.
Upload Script
I created a class which will make the interface piece of work to utilise Google Drive
crave 'google_drive' class GoogleDriveService
# Starting time nosotros build the session using the config file
def self.get_session
GoogleDrive::Session.from_service_account_key(File.bring together(__dir__, 'config', 'config_file.json'))
cease # next we can upload a file by passing the file_path
def self.upload_file(file_path, file_name)
session = get_session
session.upload_from_file(file_path, file_name, convert: fake)
end
finish
and equally like shooting fish in a barrel as that nosotros can upload a file, now allow me explain what is going on here.
Outset we have a get_session
method which builds a session past passing our config_file.json
path, so we apply that session on the upload_file
method, which receives a file_path in absolute or relative class and the proper noun we want it to take in google drive as the second parameter, so if we want to upload the ./files/tomato.txt
and nosotros want it to have the name "tomato_file.txt" in Google Drive nosotros tin do information technology in the following way
file_path_to_upload = File.bring together(__dir__, 'files', 'lycopersicon esculentum.txt')
file_name = 'tomato_file.txt'
GoogleDriveService.upload_file(file_path_to_upload, file_name)
And that's all we demand to upload a local file
Get Script
Okay, I know, now we want to check if the file really uploaded, and we can achieve that past adding the following method to our existent grade
class GoogleDriveService
Existent Code... def self.download_file(file_name)
session = get_session
file = session.file_by_title(file_name)
blank_file = File.write(File.join(__dir__, 'downloads', file_name), nil)
file.download_to_file(blank_file.path)
stop
terminate
Yeah, it'south piece of cake as that. Now we have to explicate it. This new download_file
method will receive a file_name as its unique argument and this file name must be the i with which we saved it in Google Drive, in this example it is "tomato_file.txt" what this method will do is to find a file in the storage with that name, then we gonna need a container, which is just a blank file we can create on the get, that will receive the file information and nosotros can use this as the post-obit
file_name = 'tomato_file.txt'
GoogleDriveService.download_file(file_name) # Volition result on a new file saved on our downloads folder
downloads/
|__ tomato_file.txt
And that's it, we can complement this with a loop through some specific folder filesif we demand to make a massive upload as I said.
I hope you find this useful if information technology is what you lot were looking for.
stewartnineirackly1994.blogspot.com
Source: https://arquetipo28.medium.com/using-google-drive-api-with-ruby-3f40e342439e
0 Response to "Upload File to Google Drive Using Ruby"
Postar um comentário