MSGraphMailbag - 使用 Microsoft Graph SDK 的 LargeFileUploadTask 上传大文件

Posted Justin-Liu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MSGraphMailbag - 使用 Microsoft Graph SDK 的 LargeFileUploadTask 上传大文件相关的知识,希望对你有一定的参考价值。

本篇,我们将介绍使用 Microsoft Graph SDK 的 LargeFileUploadTask 来上传大文件 (大于 4M 的文件)。

随着 Microsoft Graph SKD 的更新,ChunkedUploadProvider 已经过时了,当前可以使用 LargeFileUploadTask 来上传大文件。示例代码如下:

using (var fileStream = System.IO.File.OpenRead(filePath))

    // Use properties to specify the conflict behavior
    // in this case, replace
    var uploadProps = new DriveItemUploadableProperties
    
        ODataType = null,
        AdditionalData = new Dictionary<string, object>
        
             "@microsoft.graph.conflictBehavior", "replace" 
        
    ;

    // Create the upload session
    // itemPath does not need to be a path to an existing item
    var uploadSession = await graphClient.Me.Drive.Root
        .ItemWithPath(itemPath)
        .CreateUploadSession(uploadProps)
        .Request()
        .PostAsync();

    // Max slice size must be a multiple of 320 KiB
    int maxSliceSize = 320 * 1024;
    var fileUploadTask =
        new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxSliceSize);

    // Create a callback that is invoked after each slice is uploaded
    IProgress<long> progress = new Progress<long>(prog => 
        Console.WriteLine($"Uploaded prog bytes of fileStream.Length bytes");
    );

    try
    
        // Upload the file
        var uploadResult = await fileUploadTask.UploadAsync(progress);

        if (uploadResult.UploadSucceeded)
        
            // The ItemResponse object in the result represents the
            // created item.
            Console.WriteLine($"Upload complete, item ID: uploadResult.ItemResponse.Id");
        
        else
        
            Console.WriteLine("Upload failed");
        
    
    catch (ServiceException ex)
    
        Console.WriteLine($"Error uploading: ex.ToString()");
    

更多信息可参阅 Microsoft Docs文档
Upload large files using the Microsoft Graph SDKs

以上是关于MSGraphMailbag - 使用 Microsoft Graph SDK 的 LargeFileUploadTask 上传大文件的主要内容,如果未能解决你的问题,请参考以下文章

MSGraphMailbag - 只搜索文件类型的DriveItems

MSGraphMailbag - 使用 Microsoft Graph SDK 的 LargeFileUploadTask 上传大文件

MSGraphMailbag - 探索通过 Postman 调用 Microsoft Graph

MSGraphMailbag - 探索通过 Postman 调用 Microsoft Graph

MSGraphMailbag - 对 Microsoft Graph PowerShell SDK 的深入研究

MSGraphMailbag - 对 Microsoft Graph PowerShell SDK 的深入研究