Skip to main content
 首页 » 编程设计

python之如何使用 Microsoft Graph 覆盖上传到 SharePoint 的文件

2024年12月31日45xiaohuochai

我有一个 Python 脚本,它会使用 Microsoft Graph 将文件上传到 Sharepoint,但当我尝试两次上传同一文件时,它会给我一个 500 状态代码错误。

这是上传文件的函数代码:

def upload_file(session,filename,driveid,folder): 
 
    """Upload a file to Sharepoint. 
 
    """ 
    fname_only = os.path.basename(filename) 
 
    # create the Graph endpoint to be used 
 
    endpoint = f'drives/{driveid}/root:/{folder}/{fname_only}:/createUploadSession' 
 
    start_response = session.put(api_endpoint(endpoint)) 
    json_response = start_response.json() 
    upload_url = json_response["uploadUrl"] 
 
# upload in chunks 
 
    filesize = os.path.getsize(filename) 
 
    with open(filename, 'rb') as fhandle: 
        start_byte = 0 
        while True: 
            file_content = fhandle.read(10*1024*1024) 
            data_length = len(file_content) 
            if data_length <= 0: 
                break 
 
            end_byte = start_byte + data_length - 1 
            crange = "bytes "+str(start_byte)+"-"+str(end_byte)+"/"+str(filesize) 
            print(crange) 
            chunk_response = session.put(upload_url, 
                                         headers={"Content-Length": str(data_length),"Content-Range": crange}, 
                                         data=file_content) 
            if not chunk_response.ok: 
                print(f'<Response [{chunk_response.status_code}]>') 
                pprint.pprint(chunk_response.json()) # show error message 
                break 
 
            start_byte = end_byte + 1 
 
    return chunk_response 

这是第一次运行的输出:

bytes 0-10485759/102815295 
bytes 10485760-20971519/102815295 
bytes 20971520-31457279/102815295 
bytes 31457280-41943039/102815295 
bytes 41943040-52428799/102815295 
bytes 52428800-62914559/102815295 
bytes 62914560-73400319/102815295 
bytes 73400320-83886079/102815295 
bytes 83886080-94371839/102815295 
bytes 94371840-102815294/102815295 

这是第二次运行的输出:

bytes 0-10485759/102815295 
bytes 10485760-20971519/102815295 
bytes 20971520-31457279/102815295 
bytes 31457280-41943039/102815295 
bytes 41943040-52428799/102815295 
bytes 52428800-62914559/102815295 
bytes 62914560-73400319/102815295 
bytes 73400320-83886079/102815295 
bytes 83886080-94371839/102815295 
bytes 94371840-102815294/102815295 
<Response [500]> 
{'error': {'code': 'generalException', 
           'message': 'An unspecified error has occurred.'}} 

我想我可以在覆盖文件之前弄清楚如何删除它,但是保留历史记录会很好,因为 Sharepoint 会保留版本。

感谢您对此的任何帮助。

鲍比

附注我一直在破解 https://github.com/microsoftgraph/python-sample-console-app 中的代码,让它将文件上传到 SharePoint,因此该函数中的一些代码来自 Microsoft 的示例应用程序。

请您参考如下方法:

对于任何在调查文件名冲突问题时最终来到这里的人,根据下面的 Microsoft 文章,如果存在文件名冲突并且您没有正确指定应该替换它,最终字节范围上传将失败OP描述的方式。希望这对某人有所帮助。

Handle upload errors

When the last byte range of a file is uploaded, it is possible for an error to occur. This can be due to a name conflict or quota limitation being exceeded. The upload session will be preserved until the expiration time, which allows your app to recover the upload by explicitly committing the upload session.

发件人:https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online#create-an-upload-session