我正在尝试从 Google Drive 下载图像文件(原始 .cr2 格式,但可以根据需要进行更改)到 AWS Lambda,以便我可以使用 Pillow 进行编辑并将编辑后的图像上传回驱动器。
我尝试了两种方法来下载此文档之后的图像。
当我使用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"
]
}
当我尝试导出方法时:
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"
]
}
在 Python 中使用 Lambda 函数从驱动器下载图像的正确方法是吗?
您遇到的错误是因为 Google Drive API 的 get_media 和 export_media 方法不是为下载原始图像文件(在您的情况下为 .cr2)而设计的。
get_media
错误: 此方法需要 PDF 或可执行文件等二进制文件。由于 .cr2 是原始图像格式,因此 API 拒绝下载,因为它不被视为“二进制内容”
export_media
错误:此方法旨在将Google文档、工作表、幻灯片等导出为不同的格式(例如PDF、JPEG)。它不适合存储在驱动器中的原始图像文件
您可能需要使用
drive.files().download
方法(您可以在此处的 Google Drive api 文档中找到该方法:https://developers.google.com/drive/api/guides/manage-downloads
这也可能有帮助:https://github.com/googleapis/google-api-python-client/issues/405