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

标签 python google-drive-api backup onedrive cloud-storage

我想找一个免费的云存储服务,它可以帮助我自动备份一些文件。
我想编写一些脚本(例如 python)来自动上传文件。
我调查了 OneDrive 和 GoogleDrive。 OneDrive API 不是免费的,GoogleDrive API 是免费的,但在使用 API 之前需要人工交互授权。
现在我只是使用电子邮件 SMTP 协议(protocol)将文件作为电子邮件附件发送,但是有一个最大文件大小限制,随着我的文件大小不断增长,这在 future 会让我失望。
还有其他建议吗?

最佳答案

我相信你的目标如下。

  • 您想通过服务帐户使用 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 云端硬盘中创建一个新文件夹。并且,请与您的服务帐户的电子邮件共享创建的文件夹。因为您帐户的 Google Drive 与服务帐户的 Drive 不同。通过与服务帐户共享文件夹,可以使用服务帐户将文件上传到您的 Google Drive 中的文件夹。这样,您可以通过浏览器在您的 Google Drive 上查看上传的文件。
    4. 准备示例脚本。
    请将服务账户凭证的文件名、您要上传的文件的文件名以及您与服务账户共享文件夹的文件夹的文件夹ID设置为SERVICE_ACCOUNT的变量。 , UPLOAD_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) .

  • 引用:
  • google-api-python-client
  • Creating a service account
  • Upload file data
  • 关于python - 什么云存储服务允许开发者使用免费 API 上传/下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67275889/

    相关文章:

    css - Google Drive CSS/JS 返回 404 错误

    android - Unity 3D 插件的 Google Drive 授权错误

    sql-server - 备份 DBmssql : BACKUP DATABASE is terminating abnormally 时出现 Golang 错误

    database - AWS RDS 自动备份

    python - 在 python 中用坐标数组填充矩阵

    Python:从字符串末尾修剪下划线

    python - 从另一个文件 PyQT 打开一个 GUI 文件

    python - 属性错误 : 'module' object has no attribute 'load'

    javascript - 如何使用 Google Drive Javascript API 更新 Google Drive 中的 zip 文件

    kubernetes - GKE 集群的 velero 备份失败