使用python的覆盖模式将文档上传到GDrive

时间:2015-04-20 03:53:32

标签: python gmail gdata

我可以将文档上传到GDrive,但我希望它在覆盖模式下工作,这意味着第二次上传会覆盖同一个文件。

代码段如下:

import subprocess
import re
import gdata.client, gdata.docs.client, gdata.docs.data
import atom.data
import os
import time

#----------------------------------------#
def main():
    filename = "test.xls"
    mimetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    username = "mymail@gmail.com"
    password = "mypassword"

    upload_gdoc(filename,mimetype,username,password)
    return

def upload_gdoc(filename,mimetype,username,password):
    try:
        fdin = open(filename)
    except IOError, e:
        print 'ERROR: Unable to open ' + filename + ': ' + e[1]
        return

    file_size = os.path.getsize(fdin.name)
    docsclient = gdata.docs.client.DocsClient()

    try:
        docsclient.ClientLogin(username, password, docsclient.source);
    except (gdata.client.BadAuthentication, gdata.client.Error), e:
        print 'ERROR: ' + str(e)
        return
    except:
        print 'ERROR: Unable to login'
        return

    # The default root collection URI
    uri = 'https://docs.google.com/feeds/upload/create-session/default/private/full'
    uri += '?convert=true'

    t1 = time.time()
    uploader = gdata.client.ResumableUploader(docsclient,fdin,mimetype,file_size,chunk_size=1048576,desired_class=gdata.data.GDEntry)
    new_entry = uploader.UploadFile(uri,entry=gdata.data.GDEntry(title=atom.data.Title(text=os.path.basename(fdin.name))))
    t2 = time.time()
    print 'Uploaded', '{0:.2f}'.format(file_size / 1024.0 / 1024.0) + ' MiB in ' + str(round(t2 - t1, 2)) + ' secs'
    fdin.close()
    return

if __name__ == "__main__":
    main()

现在每次上传时,它都会创建一个具有相同文件名的新文件。

0 个答案:

没有答案