我一直在尝试按照示例从 Ms Graph SDK 上传 onedrive 的大文件,但我似乎无法弄清楚 DriveUpload 对象驻留在哪里,或者我需要安装什么 nuget 包才能完成这项工作。我的代码如下:
public async Task<Models.CloudStorage?> UploadFileByPathAsync(MemoryStream SourceFile, string DestinationPath, string RootFolderId = "")
{
if(RootFolderId == "")
{
RootFolderId = _armsFolderId;
}
using var stream = SourceFile;
// Use properties to specify the conflict behavior
// in this case, replace
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 uploadSession = await _graphServiceClient.Drives[RootFolderId].Root
.ItemWithPath(DestinationPath)
.CreateUploadSession
.PostAsync(uploadSessionRequestBody);
// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320 * 1024;
var fileUploadTask =
new LargeFileUploadTask<DriveItem>(uploadSession, stream, maxSliceSize);
var totalLength = stream.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");
});
// Upload the file
var uploadResult = await fileUploadTask.UploadAsync(progress);
if (uploadResult.UploadSucceeded)
{
var response = uploadResult.ItemResponse;
Console.WriteLine(uploadResult.UploadSucceeded ?
$"Upload complete, item ID: {uploadResult.ItemResponse.Id}" :
"Upload failed");
}
}
我克服冲突行为时遇到的错误是:
The type or namespace name 'DriveUpload' could not be found (are you missing a using directive or an assembly reference?)
我在网上找不到太多关于在哪里添加此内容的信息。另外,我试图了解这种大文件上传与此处所示方法之间的性能差异:https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-休息-1.0&tabs=http
DriveUpload 是命名空间 Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession 的简写。因此添加:
using Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession;
然后删除 DriveUpload 就可以了。