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. The backup file is an encrypted zip file (gpg file). It connects successfully, but fails t开发者_开发问答ransferring 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. 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'. It's a Google Apps account that I'm using.
Uploading files of any type are only allowed for Google Apps for Business accounts. A regular account cannot upload this filetype. See this link from the Google Docs API.
Good news: now Google Apps for Free accounts can also upload files of any type.
Three key points here:
- Use resumable upload
- Append "?convert=false" to the resumable-create-media link
- Upload chunk size should be multiple of 512 kilobyte
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.
精彩评论