You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python版AWS Lambda函数中通过Google Drive API下载图片文件?

在AWS Lambda中用Python从Google Drive下载CR2图片的问题

我需要将Google Drive中的.raw格式(如CR2)图片下载到AWS Lambda,使用Pillow编辑后再上传回Drive。按照Google Drive官方文档的指引,尝试了两种下载方式:

  • Get接口
  • Export接口

方法1:使用Get接口

代码实现:

credentials = service_account.Credentials.from_service_account_file(filename=SERVICE_ACCOUNT_FILE, scopes=SCOPES)
drive_service = build('drive', 'v3', credentials=credentials)

request = drive_service.files().get_media(fileId=file_id)

fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print("Download %d%%" % int(status.progress() * 100))

执行后出现的错误:

{
  "errorMessage": "<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/XXXXX?alt=media returned \"Only files with binary content can be downloaded. Use Export with Docs Editors files.\". Details: \"[{'message': 'Only files with binary content can be downloaded. Use Export with Docs Editors files.', 'domain': 'global', 'reason': 'fileNotDownloadable', 'location': 'alt', 'locationType': 'parameter'}]\">",
  "errorType": "HttpError",
  "requestId": "XXXXX",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 44, in lambda_handler\n    status, done = downloader.next_chunk()\n",
    "  File \"/opt/python/lib/python3.12/site-packages/googleapiclient/_helpers.py\", line 130, in positional_wrapper\n    return wrapped(*args, **kwargs)\n",
    "  File \"/opt/python/lib/python3.12/site-packages/googleapiclient/http.py\", line 780, in next_chunk\n    raise HttpError(resp, content, uri=self._uri)\n"
  ]
}

方法2:使用Export接口

代码实现:

credentials = service_account.Credentials.from_service_account_file(filename=SERVICE_ACCOUNT_FILE, scopes=SCOPES)
drive_service = build('drive', 'v3', credentials=credentials)

request = drive_service.files().export_media(fileId=file_id, mimeType=file_type)

fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print("Download %d%%" % int(status.progress() * 100))

执行后出现的错误:

{
  "errorMessage": "<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/XXXXX/export?mimeType=image%2Fjpeg&alt=media returned \"Export only supports Docs Editors files.\". Details: \"[{'message': 'Export only supports Docs Editors files.', 'domain': 'global', 'reason': 'fileNotExportable'}]\">",
  "errorType": "HttpError",
  "requestId": "XXXXX",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 47, in lambda_handler\n    status, done = downloader.next_chunk()\n",
    "  File \"/opt/python/lib/python3.12/site-packages/googleapiclient/_helpers.py\", line 130, in positional_wrapper\n    return wrapped(*args, **kwargs)\n",
    "  File \"/opt/python/lib/python3.12/site-packages/googleapiclient/http.py\", line 780, in next_chunk\n    raise HttpError(resp, content, uri=self._uri)\n"
  ]
}

请问这两种方法是不是在Lambda函数中用Python从Drive下载图片的正确方式?

内容的提问来源于stack exchange,提问作者filipebarretto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:06:02