1

我使用 pisa 创建 pdf 文档以呈现给用户:

response = HttpResponse()
pisa.CreatePDF(src=html, dest=response, show_error_as_pdf=True)
return response

response.content 包含pdf。我使用了 dropbox-python sdk 来做到这一点:

dropbox_client.put_file(folder_path, response.content)

似乎将 response.content 理解为 pdf 并正确上传文件

我需要对 google-drive-python-api 做同样的事情。这个参考(https://developers.google.com/drive/v2/reference/files/insert)显示了一个基本的方法,但 MediaFileUpload 似乎在寻找一个物理文件。还有 MediaIoBaseUpload,但它似乎不接受 response.content。我对文件/i/o 的东西不是很熟悉,所以我在这里列出了从 django 到 dropbox 再到 G-Drive 的所有内容,希望它能澄清我的使用;希望我没有混淆问题。

4

1 回答 1

2

python Google API 工具包中的apiclient.http文件包含MediaIoBaseUpload完全符合您需要的对象。

只是它需要一个文件句柄或行为类似于文件句柄(fh参数)的东西。你很幸运:这正是StringIO模块的用途:

import StringIO # You could try importing cStringIO which gives better performance
fh = StringIO.StringIO(response.content)
media = MediaIoBaseUpload(fh, mimetype='some/mimetype')
# See https://developers.google.com/drive/v2/reference/files/insert for the rest

MediaInMemoryUpload也可以解决问题,但现在已弃用。

于 2012-12-16T18:33:08.950 回答