Microsoft Graph - .NET SDK - OneDrive 大文件上传(> 4MB)
Microsoft Graph - .NET SDK - OneDrive Large File Upload (> 4MB in size)
使用 Microsoft Graph 的 .NET SDK,您可以上传(小)文件。示例 .
如何使用 .NET SDK 上传大文件(> 4MB)?
也就是说,SDK可以用来实现"Upload large files with an upload session"吗?
这将在下一个版本的 .NET Microsoft Graph 客户端库中提供。它的工作方式与 .NET OneDrive 客户端库中的功能相同。您可以在我的工作 branch 中查看此内容。您可以在回购中提供反馈。
这是我最近使用 Microsoft Graph .Net SDK 编写的代码。
需要 GraphServiceClient(graphClient) 身份验证。
if (fileSize.MegaBytes > 4)
{
var session = await graphClient.Drive.Root.ItemWithPath(uploadPath).CreateUploadSession().Request().PostAsync();
var maxSizeChunk = 320 * 4 * 1024;
var provider = new ChunkedUploadProvider(session, graphClient, stream, maxSizeChunk);
var chunckRequests = provider.GetUploadChunkRequests();
var exceptions = new List<Exception>();
var readBuffer = new byte[maxSizeChunk];
DriveItem itemResult = null;
//upload the chunks
foreach (var request in chunckRequests)
{
// Do your updates here: update progress bar, etc.
// ...
// Send chunk request
var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, exceptions);
if (result.UploadSucceeded)
{
itemResult = result.ItemResponse;
}
}
// Check that upload succeeded
if (itemResult == null)
{
await UploadFilesToOneDrive(fileName, filePath, graphClient);
}
}
else
{
await graphClient.Drive.Root.ItemWithPath(uploadPath).Content.Request().PutAsync<DriveItem>(stream);
}
使用 Microsoft Graph 的 .NET SDK,您可以上传(小)文件。示例
如何使用 .NET SDK 上传大文件(> 4MB)?
也就是说,SDK可以用来实现"Upload large files with an upload session"吗?
这将在下一个版本的 .NET Microsoft Graph 客户端库中提供。它的工作方式与 .NET OneDrive 客户端库中的功能相同。您可以在我的工作 branch 中查看此内容。您可以在回购中提供反馈。
这是我最近使用 Microsoft Graph .Net SDK 编写的代码。 需要 GraphServiceClient(graphClient) 身份验证。
if (fileSize.MegaBytes > 4)
{
var session = await graphClient.Drive.Root.ItemWithPath(uploadPath).CreateUploadSession().Request().PostAsync();
var maxSizeChunk = 320 * 4 * 1024;
var provider = new ChunkedUploadProvider(session, graphClient, stream, maxSizeChunk);
var chunckRequests = provider.GetUploadChunkRequests();
var exceptions = new List<Exception>();
var readBuffer = new byte[maxSizeChunk];
DriveItem itemResult = null;
//upload the chunks
foreach (var request in chunckRequests)
{
// Do your updates here: update progress bar, etc.
// ...
// Send chunk request
var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, exceptions);
if (result.UploadSucceeded)
{
itemResult = result.ItemResponse;
}
}
// Check that upload succeeded
if (itemResult == null)
{
await UploadFilesToOneDrive(fileName, filePath, graphClient);
}
}
else
{
await graphClient.Drive.Root.ItemWithPath(uploadPath).Content.Request().PutAsync<DriveItem>(stream);
}