我想找一个免费的云存储服务,它可以帮助我自动备份一些文件。
我想编写一些脚本(例如 python)来自动上传文件。
我调查了 OneDrive 和 GoogleDrive。 OneDrive API 不是免费的,GoogleDrive API 是免费的,但在使用 API 之前需要人工交互授权。
现在我只是使用电子邮件 SMTP 协议(protocol)将文件作为电子邮件附件发送,但是有一个最大文件大小限制,随着我的文件大小不断增长,这在 future 会让我失望。
还有其他建议吗?
最佳答案
我相信你的目标如下。
首先,在你的情况下,使用 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 云端硬盘中创建一个新文件夹。并且,请与您的服务帐户的电子邮件共享创建的文件夹。因为您帐户的 Google Drive 与服务帐户的 Drive 不同。通过与服务帐户共享文件夹,可以使用服务帐户将文件上传到您的 Google Drive 中的文件夹。这样,您可以通过浏览器在您的 Google Drive 上查看上传的文件。
4. 准备示例脚本。
请将服务账户凭证的文件名、您要上传的文件的文件名以及您与服务账户共享文件夹的文件夹的文件夹ID设置为
SERVICE_ACCOUNT
的变量。 , UPLOAD_FILE
和 FOLDER_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.
file = MediaFileUpload(UPLOAD_FILE, resumable=True)
至file = MediaFileUpload(UPLOAD_FILE, mimeType='###', resumable=True)
. 引用:
关于python - 什么云存储服务允许开发者使用免费 API 上传/下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67275889/