Carregar arquivos grandes usando os SDKs do Microsoft GraphUpload large files using the Microsoft Graph SDKs

Várias entidades no Microsoft Graph dão suporte a carregamentos de arquivos retomáveis para facilitar o carregamento de arquivos grandes.A number of entities in Microsoft Graph support resumable file uploads to make it easier to upload large files. Em vez de tentar carregar todo o arquivo em uma única solicitação, o arquivo é dividido em partes menores e uma solicitação é usada para carregar uma única fatia.Instead of trying to upload the entire file in a single request, the file is sliced into smaller pieces and a request is used to upload a single slice. Para simplificar esse processo, os SDKs do Microsoft Graph implementam uma tarefa de upload de arquivo grande que gerencia o carregamento das fatias.In order to simplify this process, the Microsoft Graph SDKs implement a large file upload task that manages the uploading of the slices.

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>(progress => {
        Console.WriteLine($"Uploaded {progress} 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()}");
    }
}

Retomando um upload de arquivoResuming a file upload

Os SDKs do Microsoft Graph dão suporte à continuação dos carregamentos em andamento.The Microsoft Graph SDKs support resuming in-progress uploads. Se o aplicativo encontrar uma interrupção de conexão ou um status HTTP de 5. x. x durante o carregamento, você poderá retomar o upload.If your application encounters a connection interruption or a 5.x.x HTTP status during upload, you can resume the upload.

fileUploadTask.ResumeAsync(progress);