Hello Kerry Mo,
Thank you for reaching out to Microsoft Support!
1.SDK is an enhancement to the API. The Microsoft Graph software development kits (SDKs) are designed to simplify building high-quality, efficient, resilient applications that access Microsoft Graph. The SDKs include two components: a service library and a core library. See the documentation for more information about the SDK.
2.Upload file code example:
The code to upload a small file is relatively simple.
var fileName = "smallfile.txt";
var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);
Console.WriteLine("Uploading file: " + fileName);
FileStream fileStream = new FileStream(filePath, FileMode.Open);
var uploadedFile = client.Me.Drive.Root
.ItemWithPath("smallfile.txt")
.Content
.Request()
.PutAsync<DriveItem>(fileStream)
.Result;
Console.WriteLine("File uploaded to: " + uploadedFile.WebUrl);
The code to upload a larger file requires chunking.
var fileName = "largefile.zip";
var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);
Console.WriteLine("Uploading file: " + fileName);
// load resource as a stream
using (Stream stream = new FileStream(filePath, FileMode.Open))
{
var uploadSession = client.Me.Drive.Root
.ItemWithPath(fileName)
.CreateUploadSession()
.Request()
.PostAsync()
.Result;
// create upload task
var maxChunkSize = 320 * 1024;
var largeUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, stream, maxChunkSize);
// create progress implementation
IProgress<long> uploadProgress = new Progress<long>(uploadBytes =>
{
Console.WriteLine($"Uploaded {uploadBytes} bytes of {stream.Length} bytes");
});
// upload file
UploadResult<DriveItem> uploadResult = largeUploadTask.UploadAsync(uploadProgress).Result;
if (uploadResult.UploadSucceeded)
{
Console.WriteLine("File uploaded to user's OneDrive root folder.");
}
}
There are also code examples in the official reference documentation:
https://learn.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=csharp
Hope this helps.
If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.