gpt4 book ai didi

python - 使用 SDK 将大文件上传到 OneDrive

转载 作者:行者123 更新时间:2023-12-05 04:00:21 49 4
gpt4 key购买 nike

我想将一个巨大(大于 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] 管道损坏

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

最佳答案

SDK 现已弃用。 https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online您可以使用 Microsoft graph 和 OneDriveAPI 将文件上传到 OneDriveOneDriveAPI 支持小文件 (<4MB) 的简单上传和大文件的可恢复上传,您基本上可以创建上传 session 并上传大文件,一次一个 block 。

Github repo

这是我为解决这个问题而写的教程: 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

关于python - 使用 SDK 将大文件上传到 OneDrive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56270467/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com