使用SDK将大文件上传到OneDrive

3
我想上传一个超过2GB的大文件到OneDrive。
我已经尝试使用SDK页面 (https://github.com/OneDrive/onedrive-sdk-python) 中提供的代码。
returned_item = client.item(drive='me', path=backupPath).children['photos.tgz'].upload_async('/Users/koot/photos.tgz')

虽然这段代码可以处理小文件,但在上传大文件时,我遇到了以下错误:

BrokenPipeError: [Errno 32] Broken pipe

requests.exceptions.ConnectionError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))


你试过这个吗?https://github.com/OneDrive/onedrive-sdk-python/issues/122 - RandomCoder
错误是否一直发生(在大文件上),或者重新开始上传就足够了?手动上传相同的文件是否有效(一致性)? - Serge Ballesta
非常感谢。我在上传命令的末尾添加了“.to_dict()”,这解决了问题。 - Krystian
1个回答

4

SDK现已被废弃。 https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online 您可以使用Microsoft Graph和OneDrive API将文件上传到OneDrive中。 OneDrive API支持小文件(<4MB)的简单上传,对于更大的文件,它支持断点续传,您需要创建一个上传会话,并一次一个分块地上传大文件。

Github仓库

这是我写的有关解决此问题的教程: https://dev.to/jsnmtr/automating-files-upload-to-microsoft-onedrive-unexpected-challenges-and-a-success-story-2ini

下面是上传大型文件的代码:

#Creating an upload session
        upload_session = requests.post(onedrive_destination+"/"+file_name+":/createUploadSession", headers=headers).json()

        with open(file_path, 'rb') as f:
            total_file_size = os.path.getsize(file_path)
            chunk_size = 327680
            chunk_number = total_file_size//chunk_size
            chunk_leftover = total_file_size - chunk_size * chunk_number
            i = 0
            while True:
                chunk_data = f.read(chunk_size)
                start_index = i*chunk_size
                end_index = start_index + chunk_size
                #If end of file, break
                if not chunk_data:
                    break
                if i == chunk_number:
                    end_index = start_index + chunk_leftover
                #Setting the header with the appropriate chunk data location in the file
                headers = {'Content-Length':'{}'.format(chunk_size),'Content-Range':'bytes {}-{}/{}'.format(start_index, end_index-1, total_file_size)}
                #Upload one chunk at a time
                chunk_data_upload = requests.put(upload_session['uploadUrl'], data=chunk_data, headers=headers)
                print(chunk_data_upload)
                print(chunk_data_upload.json())
                i = i + 1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接