Python 使用gdata上载多行.txt文件时出现503错误
我有一些代码,可以使用gdata将单行.txt文件上传到我的谷歌硬盘。相同的文件,但带有换行符,将不会上载并提供:Python 使用gdata上载多行.txt文件时出现503错误,python,google-drive-api,Python,Google Drive Api,我有一些代码,可以使用gdata将单行.txt文件上传到我的谷歌硬盘。相同的文件,但带有换行符,将不会上载并提供: gdata.client.RequestError: Server responded with: 503, 删除换行符,它就可以正常运行了。你知道怎么解决这个问题吗 编辑以添加工作示例: import sys import time import os.path import atom.data import gdata.client, gdata.docs.client,
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”写入文件时的相同响应
有什么办法解决这个问题吗?我自己解决了这个问题。我用新的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)
就是这样 您能提供一个更完整的代码示例吗?特别有用的是看到你用来上传的gdata调用。当然。代码添加。