2014-09-25 42 views
1

我的代碼會使用gdata將單行.txt文件上傳到我的Google Drive。相同的文件,但是換行符不會上傳並給出:使用gdata上傳多行.txt文件會產生503錯誤

gdata.client.RequestError: Server responded with: 503, 

刪除換行符,並且它通過就好了。任何想法如何解決這個問題?

編輯補充工作示例:

import sys 
import time 
import os.path 
import atom.data 
import gdata.client, gdata.docs.client, gdata.docs.data 
import urllib2 

class GoogleDriveFileUpload: 

    def __init__(self, fileName, targetFolder, username, password, ftype='txt'): 

     self.fileName = fileName 
     self.targetFolder = targetFolder 
     self.username = username 
     self.password = password 
     self.file_type = self.cvtFileType(ftype) 


    def cvtFileType(self, ftype): 
     if ftype == 'jpg': 
      file_type = 'image/jpeg' 
     elif ftype == 'kml': 
      file_type = 'application/vnd.google-earth.kml+xml' 
     elif ftype == 'txt': 
      file_type = 'text/plain' 
     elif ftype == 'csv': 
      file_type = 'text/csv' 
     elif ftype == 'mpg': 
      file_type = 'audio/mpeg' 
     elif ftype == 'mp4': 
      file_type = 'video/mp4' 

     return file_type 

    def changeFile(self, fileName, ftype = 'txt'): 
     self.fileName = fileName 
     self.file_type = cvtFileType(ftype) 
     self.file_size = os.path.getsize(fhandle.name) 

    def changeTarget(self, targetFolder): 
     self.targetFolder = targetFolder 

    def upload(self): 
     #Start the Google Drive Login 
     docsclient = gdata.docs.client.DocsClient(source='GausLabAnalysis') 

     # Get a list of all available resources (GetAllResources() requires >= gdata-2.0.15) 
     # print 'Logging in...', 
     try: 
      docsclient.ClientLogin(self.username, self.password, docsclient.source); 
     except (gdata.client.BadAuthentication, gdata.client.Error), e: 
      sys.exit('Unknown Error: ' + str(e)) 
     except: 
      sys.exit('Login Error. Check username/password credentials.') 
     # print 'Success!' 

     # The default root collection URI 
     uri = 'https://docs.google.com/feeds/upload/create-session/default/private/full' 
     # Get a list of all available resources (GetAllResources() requires >= gdata-2.0.15) 
     # print 'Fetching Collection/Directory ID...', 
     try: 
      resources = docsclient.GetAllResources(uri='https://docs.google.com/feeds/default/private/full/-/folder?title=' + self.targetFolder + '&title-exact=true') 
     except: 
      sys.exit('ERROR: Unable to retrieve resources') 
     # If no matching resources were found 
     if not resources: 
      sys.exit('Error: The collection "' + self.targetFolder + '" was not found.') 
     # Set the collection URI 
     uri = resources[0].get_resumable_create_media_link().href 
     # print 'Success!' 
     # Make sure Google doesn't try to do any conversion on the upload (e.g. convert images to documents) 
     uri += '?convert=false' 


     fhandle = open(self.fileName) 
     self.file_size = os.path.getsize(fhandle.name) 
     print 'Uploading ', self.fileName,'....' 
     # Create an uploader object and upload the file 
     uploader = gdata.client.ResumableUploader(docsclient, fhandle, self.file_type, self.file_size, chunk_size=262144, desired_class=gdata.data.GDEntry) 
     new_entry = uploader.UploadFile(uri, entry=gdata.data.GDEntry(title=atom.data.Title(text=os.path.basename(fhandle.name)))) 
     # print 'Success!', 
     print 'File ' + self.fileName + ' uploaded to ' + self.targetFolder + ' at ' + time.strftime("%H:%M:%S %d/%m/%Y ", time.localtime()) + '.' 


def internet_on(): 
    try: 
     response=urllib2.urlopen('http://74.125.228.100', timeout=5) 
     return True 
    except: 
     return False 

def main(): 
    gdoc = GoogleDriveFileUpload('...\HelloWorld.txt', 'GoogleDriveFolderName', 'username', 'password') 
    if internet_on(): 
     gdoc.upload() 


if __name__ == "__main__": 
    # stuff only to run when not called via 'import' here 
    main() 

這工作時HelloWorld.txt是:

Hello World! 

,但未能與503錯誤時相同的文件是:

Hello 
World! 

唯一的區別是用記事本放入換行符。使用'\ n'而不是'\ r \ n'寫入文件時的響應相同。

有什麼辦法解決這個問題?

+2

您能否包含更完整的代碼示例?特別有用的是看到你用來上傳的gdata調用。 – ArthurDenture 2014-09-25 04:11:53

+0

當然。已添加代碼。 – Staus 2014-09-25 23:30:17

回答

0

自己解決了這個問題。我經歷了使用新API設置東西的整個過程,但是這會讓您通過單獨的雲端硬盤帳戶,然後共享該文件,而不是直接(從我可以工作的地方)上傳它。適合應用,但不適合我。

無法讓事情與ResumableUploader類一起工作,但CreateResource似乎很好地完成了這項工作。

解決方案是經過

# Create an uploader object and upload the file 

除去兩行上面的代碼,並添加

mS = gdata.data.MediaSource(content_type = gdata.docs.service.SUPPORTED_FILETYPES[self.ftype.upper()]) 
    mS.SetFileHandle(self.fileName, self.file_type) 
    doc = gdata.docs.data.Resource(type='file', title=os.path.basename(self.fileName)) 
    doc = docsclient.CreateResource(doc, media=mS, create_uri = uri) 

這就行了!

相關問題