次の方法で共有


Microsoft Graph SDK を使用して大きなファイルをアップロードする

Microsoft Graph の多数のエンティティでは、再開 可能なファイルのアップロードが サポートされ、大きなファイルのアップロードが容易になります。 1 つの要求でファイル全体をアップロードする代わりに、ファイルはより小さな部分にスライスされ、要求を使用して 1 つのスライスをアップロードします。 このプロセスを簡略化するために、Microsoft Graph SDK では、スライスのアップロードを管理する大きなファイルアップロード タスクが実装されています。

大きなファイルを OneDrive にアップロードする

using var fileStream = File.OpenRead(filePath);

// Use properties to specify the conflict behavior
// using DriveUpload = Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession;
var uploadSessionRequestBody = new DriveUpload.CreateUploadSessionPostRequestBody
{
    Item = new DriveItemUploadableProperties
    {
        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 myDrive = await graphClient.Me.Drive.GetAsync();
var uploadSession = await graphClient.Drives[myDrive?.Id]
    .Items["root"]
    .ItemWithPath(itemPath)
    .CreateUploadSession
    .PostAsync(uploadSessionRequestBody);

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

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

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

    Console.WriteLine(uploadResult.UploadSucceeded ?
        $"Upload complete, item ID: {uploadResult.ItemResponse.Id}" :
        "Upload failed");
}
catch (ODataError ex)
{
    Console.WriteLine($"Error uploading: {ex.Error?.Message}");
}

ファイルのアップロードを再開する

Microsoft Graph SDK では、 進行中のアップロードの再開がサポートされています。 アップロード中にアプリケーションで接続の中断または 5.x.x HTTP 状態が発生した場合は、アップロードを再開できます。

await fileUploadTask.ResumeAsync(progress);

大きな添付ファイルを Outlook メッセージにアップロードする

// Create message
var draftMessage = new Message
{
    Subject = "Large attachment",
};

var savedDraft = await graphClient.Me
    .Messages
    .PostAsync(draftMessage);

using var fileStream = File.OpenRead(filePath);
var largeAttachment = new AttachmentItem
{
    AttachmentType = AttachmentType.File,
    Name = Path.GetFileName(filePath),
    Size = fileStream.Length,
};

// using AttachmentUpload = Microsoft.Graph.Me.Messages.Item.Attachments.CreateUploadSession;
var uploadSessionRequestBody = new AttachmentUpload.CreateUploadSessionPostRequestBody
{
    AttachmentItem = largeAttachment,
};

var uploadSession = await graphClient.Me
    .Messages[savedDraft?.Id]
    .Attachments
    .CreateUploadSession
    .PostAsync(uploadSessionRequestBody);

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

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

try
{
    // Upload the file
    var uploadResult = await fileUploadTask.UploadAsync(progress);
    Console.WriteLine(uploadResult.UploadSucceeded ? "Upload complete" : "Upload failed");
}
catch (ODataError ex)
{
    Console.WriteLine($"Error uploading: {ex.Error?.Message}");
}