尝试将图像文件上传到集合时出现400错误
问题描述:
我想写一个脚本将我的照片上传到Google云端硬盘。经过几个小时的挖掘Google文档列表API。我选择gdata-python-client 2.0.17(最新版)来构建我的脚本。一切运作良好,除了我不能上传文件到一个集合。这是例外。尝试将图像文件上传到集合时出现400错误
Traceback (most recent call last): File "./upload.py", line 27, in upload(sys.argv[1]) File "./upload.py", line 22, in upload client.create_resource(p, collection=f, media=ms) File "/usr/local/lib/python2.7/dist-packages/gdata/docs/client.py", line 300, in create_resource return uploader.upload_file(create_uri, entry, **kwargs) File "/usr/local/lib/python2.7/dist-packages/gdata/client.py", line 1090, in upload_file start_byte, self.file_handle.read(self.chunk_size)) File "/usr/local/lib/python2.7/dist-packages/gdata/client.py", line 1048, in upload_chunk raise error gdata.client.RequestError: Server responded with: 400, <errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>InvalidEntryException</code><internalReason>We're sorry, a server error occurred. Please try again.</internalReason></error></errors>
在对gdata的源代码进行黑客入侵之后,我打印了一些调试信息。
> Range: bytes 0-524287/729223 PUT TO: https://docs.google.com/feeds/upload/create-session/default/private/full/folder%3A0B96cfHivZx6ddGFwYXVCbzc4U3M/contents?upload_id=AEnB2UqnYRFTOyCCIGIESUIctWg6hvQIHY4JRMnL-CUQhHii3RGMFWZ12a7lXWd1hgOChd1Vqlr8d-BmvyfmhFhzhYK9Vnw4Xw > Range: bytes 524288-729222/729223 PUT TO: https://docs.google.com/feeds/upload/create-session/default/private/full/folder%3A0B96cfHivZx6ddGFwYXVCbzc4U3M/contents?upload_id=AEnB2UqnYRFTOyCCIGIESUIctWg6hvQIHY4JRMnL-CUQhHii3RGMFWZ12a7lXWd1hgOChd1Vqlr8d-BmvyfmhFhzhYK9Vnw4Xw
PUT
文件的最后部分引发异常。
答
我会建议你去尝试新的谷歌云端硬盘API V2,使得它更与媒体上传更好的支持更容易:https://developers.google.com/drive/v2/reference/files/insert
一旦你得到一个authorized service instance,你可以简单地插入一个新的文件,像这样:
from apiclient import errors
from apiclient.http import MediaFileUpload
# ...
def insert_file(service, title, description, parent_id, mime_type, filename):
"""Insert new file.
Args:
service: Drive API service instance.
title: Title of the file to insert, including the extension.
description: Description of the file to insert.
parent_id: Parent folder's ID.
mime_type: MIME type of the file to insert.
filename: Filename of the file to insert.
Returns:
Inserted file metadata if successful, None otherwise.
"""
media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
body = {
'title': title,
'description': description,
'mimeType': mime_type
}
# Set the parent folder.
if parent_id:
body['parents'] = [{'id': parent_id}]
try:
file = service.files().insert(
body=body,
media_body=media_body).execute()
return file
except errors.HttpError, error:
print 'An error occured: %s' % error
return None
我搬到了Google Drive SDK v2。有用!另一个问题是,有没有办法让匿名用户下载带有秘密链接的照片?也许我需要检查权限? – xiaowl 2012-07-11 03:38:40
对于匿名访问,您可以简单地向公共文件的编辑链接发送未经授权的GET请求,该链接的格式应为“https://www.googleapis.com/drive/v2/files/”。由于未提供授权标头,因此您还需要提供[API密钥](https://developers.google.com/console/help/#WhatIsKey)作为'?key = '查询参数。 –
Alain
2012-07-11 16:51:33
它不起作用。响应为:'{ “错误”:{ “错误”: { “域”: “全局”, “理由”: “NOTFOUND”, “消息”: “找不到文件:0B2KSeJRpuK1PVm8xYTVaMHdVWGc” } ], “代码”:404, “消息”:“找不到文件:0B2KSeJRpuK1PVm8xYTVaMHdVWGc” } }'更进一步,我需要一个直接链接到图片或点击到下载链接。那可能吗? – xiaowl 2012-07-12 02:59:38