What cloud storage service allow developer upload/download files with free API?

1.3k Views Asked by At

I want to find a free cloud storage service with free API, that could help me back up some files automatically.

I want to write some script (for example python) to upload files automatically.

I investigated OneDrive and GoogleDrive. OneDrive API is not free, GoogleDrive API is free while it need human interactive authorization before using API.

For now I'm simply using email SMTP protocol to send files as email attachments, but there's a max file size limition, which will fail me in the future, as my file size is growing.

Is there any other recommendations ?

2

There are 2 best solutions below

0
On BEST ANSWER

I believe your goal as follows.

  • You want to upload a file using Drive API with the service account.
  • You want to achieve your goal using python.

At first, in your situation, how about using google-api-python-client? In this answer, I would like to explain the following flow and the sample script using google-api-python-client.

Usage:

1. Create service account.

Please create the service account and download a JSON file. Ref

2. Install google-api-python-client.

In order to use the sample script, please install google-api-python-client.

$ pip install google-api-python-client

3. Prepare a folder.

Please create a new folder in your Google Drive. And, please share the created folder with the email of your service account. Because the Google Drive of your account is different from the Drive of service account. By sharing the folder with the service account, the file can be uploaded to the folder in your Google Drive using the service account. By this, you can see the uploaded file on your Google Drive by your browser.

4. Prepare sample script.

Please set the filename of credentials of service account, the filename of the file you want to upload and the folder ID of folder you shared your folder with the service account to the variables of SERVICE_ACCOUNT, UPLOAD_FILE and FOLDER_ID, respectively.

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

SERVICE_ACCOUNT = '###' # Please set the file of your credentials of service account.
UPLOAD_FILE = 'sampleFilename' # Please set the filename with the path you want to upload.
FOLDER_ID = '###' # Please set the folder ID that you shared your folder with the service account.
FILENAME = 'sampleFilename' # You can set the filename of the uploaded file on Google Drive.

SCOPES = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT, SCOPES)
drive = build('drive', 'v3', credentials=credentials)
metadata = {'name': FILENAME, "parents": [FOLDER_ID]}
file = MediaFileUpload(UPLOAD_FILE, resumable=True)
response = drive.files().create(body=metadata, media_body=file).execute()
fileId = response.get('id')
print(fileId)  # You can see the file ID of the uploaded file.
  • When you run this script, the file is uploaded to the shared folder in your Google Drive.
  • When you set the mimeType of the file you want to use, please modify file = MediaFileUpload(UPLOAD_FILE, resumable=True) to file = MediaFileUpload(UPLOAD_FILE, mimeType='###', resumable=True).

References:

0
On
  1. gdownload.py using Python3

    from apiclient.http import MediaIoBaseDownload
    from apiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    import io,os
    
    CLIENT_SECRET = 'client_secrets.json'
    SCOPES = ['https://www.googleapis.com/auth/admin.datatransfer','https://www.googleapis.com/auth/drive.appfolder','https://www.googleapis.com/auth/drive']
    
    store = file.Storage('tokenWrite.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET, SCOPES)
        flags = tools.argparser.parse_args(args=[])
        creds = tools.run_flow(flow, store, flags)
    DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
    
    files = DRIVE.files().list().execute().get('items', [])
    
    def download_file(filename,file_id):
        #request = DRIVE.files().get(fileId=file_id)
        request = DRIVE.files().get_media(fileId=file_id)
        fh = io.BytesIO()
        downloader = MediaIoBaseDownload(fh, request,chunksize=-1)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Download %d%%." % int(status.progress() * 100))
        fh.seek(0)
        f=open(filename,'wb')
        f.write(fh.read())
        f.close()
    
    rinput = vars(__builtins__).get('raw_input',input)
    fname=rinput('enter file name: ')
    for f in files:
     if f['title'].encode('utf-8')==fname:
      print('downloading...',f['title'])
      download_file(f['title'],f['id'])
    os._exit(0)