哪些云存储服务可以让开发者通过免费的API上传/下载文件? [英] What cloud storage service allow developer upload/download files with free API?

查看:35
本文介绍了哪些云存储服务可以让开发者通过免费的API上传/下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找一个带有免费API的免费云存储服务,它可以帮助我自动备份一些文件。

我想编写一些脚本(例如,python)来自动上载文件。

我调查了OneDrive和GoogleDrive。OneDrive API不是免费的,GoogleDrive API是免费的,但在使用API之前需要人工交互授权。

目前我只是使用电子邮件SMTP协议将文件作为电子邮件附件发送,但有一个最大文件大小限制,随着我的文件大小不断增长,这一限制在将来会失败。

还有其他建议吗?

推荐答案

我相信您的目标如下。

  • 您要使用服务帐户使用Drive API上载文件。
  • 您希望使用python实现您的目标。
首先,在您的情况下,使用google-api-python-client如何?在这个回答中,我想解释一下使用google-api-python-client的以下流程和示例脚本。

用法:

1.创建服务帐户。

请创建服务帐户并下载JSON文件。Ref

2.安装google-api-python-client

为了使用示例脚本,请安装google-api-python-client

$ pip install google-api-python-client

3.准备文件夹。

请在您的Google Drive中创建一个新文件夹。并且,请将创建的文件夹与您的服务帐户的电子邮件共享。因为您帐户的Google Drive与Drive of Service帐户不同。通过与服务帐户共享文件夹,可以使用服务帐户将文件上载到Google Drive中的文件夹。这样,您就可以通过浏览器在您的Google Drive上看到上传的文件。

4.准备示例脚本。

请将服务帐户凭据的文件名、要上载的文件的文件名以及与服务帐户共享文件夹的文件夹ID分别设置为变量SERVICE_ACCOUNTUPLOAD_FILEFOLDER_ID

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.
  • 当您运行此脚本时,文件将上载到Google Drive中的共享文件夹。
  • 设置要使用的文件的MimeType时,请将file = MediaFileUpload(UPLOAD_FILE, resumable=True)修改为file = MediaFileUpload(UPLOAD_FILE, mimeType='###', resumable=True)

引用:

这篇关于哪些云存储服务可以让开发者通过免费的API上传/下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆