首页 > 解决方案 > 如何将多个文件上传到 OneDrive

问题描述

我使用 MS Graph API 将数据迁移到 OneDrive。总数据大小为 100Gb +。

目前我正在做以下事情:

当我在一个文件夹中时,我使用 FileStream 来获取文件:

foreach (var file in System.IO.Directory.GetFiles(startPath))
        {
            using (FileStream stream = System.IO.File.Open(file, FileMode.Open))
            {
                string fileName = Path.GetFileName(file);
                await WriteFilesToOneDriveAsync(graphClien, stream, user, fileName, folderId);
            }
        }

当我拥有第一个文件时,我使用以下方法将其上传到 OneDrive:

 var driveItemList = await graphServiceClient.Users[user.Id].Drive.Root.Children.Request().GetAsync();

                if (!driveItemList.Any(x => x.Name == fileName))
                {
                    UploadSession uploadSession = null;

                    uploadSession = await graphServiceClient.Users[user.Id].Drive.Root.ItemWithPath(fileName).CreateUploadSession().Request().PostAsync();

                    var uploadProvider = new ChunkedUploadProvider(uploadSession, graphServiceClient, stream);
                    var chunks = uploadProvider.GetUploadChunkRequests();
                    var exceptions = new List<Exception>();

                    foreach (var (chunk, i) in chunks.WithIndex())
                    {
                        var chunkRequestResponse = await uploadProvider.GetChunkRequestResponseAsync(chunk, exceptions);

                        Console.WriteLine($"Uploading chunk {i} out of {chunks.Count()}");

                        if (chunkRequestResponse.UploadSucceeded)
                        {
                            Console.WriteLine("Upload is complete", chunkRequestResponse.ItemResponse);
                        }
                    }
                }
                else
                {
                    logger.Info($"File Exists {fileName}");
                }

这段代码工作得很好。它允许我将文件上传到 OneDrive。

我现在的问题是我有速度问题。将 100Gb 上传到 OneDrive 需要几个小时。根据微软文档:

https://docs.microsoft.com/en-us/sharepointmigration/sharepoint-online-and-onedrive-migration-speed

包装尺寸。为了提高迁移吞吐量,我们建议您每次传输至少打包 250 个文件。对于传输大小,我们建议每个包至少 100 MB 且小于 250 MB。遵守这些准则将加快上传到 Azure 的速度,并利用迁移 API 的扩展功能。

如果我有一个包含 10 个文件(Excel、文本文件...)的文件夹,我如何将这些文件合并到一个包中?

标签: c#apigraphonedrive

解决方案


推荐阅读