简体   繁体   English

通过 Python 脚本将文件上传到谷歌文档

[英]Uploading a file to google docs via a Python script

I'm trying to upload a backup file from my server to my Google storage in a Python script.我正在尝试使用 Python 脚本将备份文件从我的服务器上传到我的 Google 存储。 The backup file is an encrypted zip file (gpg file).备份文件是一个加密的 zip 文件(gpg 文件)。 It connects successfully, but fails transferring the file which the following error:它连接成功,但传输文件失败,出现以下错误:

Traceback (most recent call last):
  File "backup.py", line 37, in <module>
    entry = gd_client.Upload(ms, 'Backup.gpg', folder_or_uri=uri)
  File "/usr/local/lib/python2.6/dist-packages/gdata/docs/service.py", line 306, in Upload
    folder_or_uri)
  File "/usr/local/lib/python2.6/dist-packages/gdata/docs/service.py", line 161, in _UploadFile
    converter=gdata.docs.DocumentListEntryFromString)
  File "/usr/local/lib/python2.6/dist-packages/gdata/service.py", line 1236, in Post
    media_source=media_source, converter=converter)
  File "/usr/local/lib/python2.6/dist-packages/gdata/service.py", line 1358, in PostOrPut
    'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 400, 'body': 'Invalid request URI', 'reason': 'Bad Request'}

Here is my code:这是我的代码:

import os
import sys
import gdata.docs
import gdata.docs.service
import gdata.docs.client

gd_client = gdata.docs.service.DocsService()
gd_client.ClientLogin('me@myemail.com', 'mypassword')

uri = '%s?convert=false' % gdata.docs.client.DOCLIST_FEED_URI

f = open('backup.zip.gpg')
ms = gdata.MediaSource(file_handle=f, content_type='application/octet-stream', content_length=os.path.getsize(f.name))
entry = gd_client.Upload(ms, 'Backup.gpg', folder_or_uri=uri)

It's possible that the file is just too big (56mb), and I can't use Google Docs storage this way.文件可能太大(56mb),我不能以这种方式使用 Google Docs 存储。 I'd hope for a more explicit error message if this was the case though.如果是这种情况,我希望有更明确的错误消息。 I'm wondering if it's a problem with my saying 'convert=false'.我想知道我说的“convert=false”是否有问题。 It's a Google Apps account that I'm using.这是我正在使用的 Google Apps 帐户。

Uploading files of any type are only allowed for Google Apps for Business accounts.只有 Google Apps for Business帐户才允许上传任何类型的文件。 A regular account cannot upload this filetype.普通帐户无法上传此文件类型。 See this link from the Google Docs API.请参阅 Google 文档 API 中的此链接

Good news: now Google Apps for Free accounts can also upload files of any type .好消息:现在 Google Apps for Free帐户也可以上传任何类型的文件。

Three key points here:这里的三个关键点:

To see an example in python, please go to download gdata-2.0.14 and find resumable_upload_sample.py under the directory samples/docs.要查看 python 中的示例,请 go 下载 gdata-2.0.14 并在目录 samples/docs 下找到 resumable_upload_sample.py。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM