什么云存储服务允许开发者使用免费的API上传/下载文件



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

我想写一些脚本(例如python(来自动上传文件。

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

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

还有其他建议吗?

我相信你的目标如下。

  • 您要使用服务帐户使用驱动器API上载文件
  • 您希望使用python来实现您的目标

首先,在您的情况下,使用谷歌api python客户端怎么样?在这个答案中,我想解释下面的流程和使用googleapi-python客户端的示例脚本。

用法:

1.创建服务帐户

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

2.安装google-api-python-client

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

$ pip install google-api-python-client

3.准备一个文件夹

请在Google Drive中创建一个新文件夹。并且,请将创建的文件夹与您的服务帐户的电子邮件共享。因为您帐户的Google Drive与服务帐户的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 python客户端
  • 创建服务帐户
  • 上传文件数据
  1. gdownload.py使用Python3
来自apiclient.http-import MediaIoBaseDownload来自apiclient.discovery导入内部版本从httplib2导入Http从oauth2client导入文件、客户端、工具导入io,osCLIENT_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=文件。存储('tokenWrite.json'(creds=store.get((如果不是信用证或信用证无效:flow=客户端.flow_from_clientsecrets(client_SECRET,SCOPES(flags=tools.argparser.parse_args(args=[](creds=tools.run_flow(流、存储、标志(DRIVE=build('DRIVE','v2',http=creds.authorize(http((((files=DRIVE.files((.list((.execute((.get('items',[](def download_file(文件名,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=错误while done is False:status,done=downloader.next_cchunk((print("下载%d%%."%int(status.progress((*100((fh.seek(0(f=打开(文件名,'w'(f.write(fh.read(((f.关闭((rinput=vars(__builtins__(.get('raw_input',input(fname=rinput('输入文件名:'(对于文件中的f:如果f['title'].encode('utf-8'(==fname:打印('下载…',f[标题'](下载文件(f['itle'],f['id'](os_exit(0(

最新更新