简体   繁体   English

使用PyDrive将大文件上传到Google云端硬盘

[英]Upload a huge file to Google Drive using PyDrive

Currently, I am using PyDrive to upload my backup (.tar file) to google drive. 目前,我正在使用PyDrive将我的备份(.tar文件)上传到Google驱动器。

Is there a special thing to do with this library to upload a huge file to Google Drive (around 5gb). 此库有什么特别的事情可以将巨大的文件上传到Google云端硬盘(约5GB)。 In the Google Drive API documentation, it says that we must use the Resumable upload ? 在Google Drive API文档中,它说我们必须使用可恢复上传? https://developers.google.com/drive/web/manage-uploads https://developers.google.com/drive/web/manage-uploads

My problem is that when I try to send a huge file, the script executes without any errors, really quickly and the file does not appear in GoogleDrive. 我的问题是,当我尝试发送大文件时,脚本执行得很快,而且没有任何错误,而且速度很快,并且文件没有出现在GoogleDrive中。 However, if I do this with a small file around 100mb, everything works perfectly fine... 但是,如果我用一个大约100mb的小文件来执行此操作,则一切正常。

My code is the following: 我的代码如下:

def upload(self, backupFile, backupFileName):

    json_data=open(os.path.join(__location__, 'client_secrets.json'))

    data = json.load(json_data)

    """Email of the Service Account"""
    SERVICE_ACCOUNT_EMAIL = data['client_email']

    """Path to the Service Account's Private Key file"""
    SERVICE_ACCOUNT_PKCS12_FILE_PATH = os.path.join(__location__, 'key.p12')

    f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
    key = f.read()
    f.close()

    credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
        scope='https://www.googleapis.com/auth/drive', sub='email')
    http = httplib2.Http()
    credentials.authorize(http)

    gauth = GoogleAuth()
    gauth.credentials = credentials

    drive = GoogleDrive(gauth)

    file1 = drive.CreateFile({'title': backupFileName, "parents" : [{"id":"0B7FoN03AUUdZVlNETEtWLS1VTzQ"}]} )  # Create GoogleDriveFile instance with title 'Hello.txt'

    file1.SetContentFile(backupFile);
    file1.Upload()

When I try to send a large file, no errors are returned whatsoever. 当我尝试发送大文件时,不会返回任何错误。 The python script simply ends without anything being shown... python脚本只是结束而没有任何显示...

There are 100MB limits on some types of files 某些类型的文件有100MB的限制

https://support.google.com/drive/answer/37603?hl=en https://support.google.com/drive/answer/37603?hl=en

Size limits 大小限制

Documents: 1,024,000 characters, regardless of the number of pages or font size. 文件:1,024,000个字符,与页数或字体大小无关。 If using the new version of Drive (with the red "New" button on the left side), uploaded document files that are converted to the Google documents format can't be larger than 50 MB. 如果使用新版云端硬盘(左侧带有红色的“新建”按钮),则已转换为Google文档格式的上载文档文件不能大于50 MB。 If using the classic version of Drive, you can't convert document files that are larger than 10 MB. 如果使用旧版云端硬盘,则不能转换大于10 MB的文档文件。

Spreadsheets: 400,000 cells, with a maximum of 256 columns per sheet. 电子表格:400,000个单元格,每张最多256列。 Uploaded spreadsheet files that are converted to the Google spreadsheets format can't be larger than 100 MB, and need to be under 400,000 cells and 256 columns per sheet. 转换为Google电子表格格式的已上传电子表格文件不能大于100 MB,并且必须小于400,000个单元格和每张256列。

More information about spreadsheet size limits All spreadsheet limits mentioned above have been removed in the new version of Google Sheets. 有关电子表格大小限制的更多信息,上述所有电子表格限制已在新版Google表格中删除。 The new version of Google Sheets should support 2 million cells of data, though please note that extremely large spreadsheets may have slower performance. 新版本的Google表格可以支持200万个数据单元,不过请注意,大型电子表格的性能可能会降低。 Learn more about switching to the new version of Google Sheets. 了解更多有关切换到新版Google表格的信息。 Presentations: Presentations created in Google Slides can be up to 100 MB. 演示文稿:在Google幻灯片中创建的演示文稿最大为100 MB。 Uploaded presentation files that are converted to Google Slides can also be up to 50 MB. 上载的演示文稿文件也可以转换为Google幻灯片,最大为50 MB。

Drawings: We've never seen anyone make a drawing that was too big (but that's not a dare). 图纸:我们从未见过有人制作过大的图纸(但这并不敢)。

Other files: Files that you upload but don't convert to a Google Docs, Sheets, or Slides format can be up to 5 TB each. 其他文件:您上传但未转换为Google文档,表格或幻灯片格式的文件,每个文件的最大容量为5 TB。

Across Google Drive, Gmail, and Google+ Photos, every user is given 15 GB of free storage space, and can purchase additional storage as well. 在Google云端硬盘,Gmail和Google+相册中,每个用户都有15 GB的免费存储空间,他们还可以购买其他存储空间。

You can upload any size by passing a file like object to .content of GoogleDriveFile 您可以通过将类似对象的文件传递到GoogleDriveFile的.content来上传任何大小的文件

file1.content=fileobj file1.content = FileObj文件

from my experience it reads in 8192 bytes at at ime first it seeks 0,2, then seeks 0,0, then calls tell() before every read(8192) and it also calls read(8192) at seek(0,2)+1 根据我的经验,它首先在ime读取8192字节,然后先寻找0,2,然后寻找0,0,然后在每次read(8192)之前调用tell(),并且还在seek(0,2)处调用read(8192) +1

so you have to put read(),tell() and seek() methods in the object this is probably from the http.py portion of the api 因此您必须将read(),tell()和seek()方法放在对象中,这可能来自api的http.py部分

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

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