有没有一种方法可以使用 C# 跟踪文件和文件夹并将其上传到 S3?

Is there a way to tracking and upload file and folder to S3 with c#?

我想将包含子文件夹和文件的文件夹上传到 S3。同时,我想跟踪这个过程。我可以获得要上传的文件并跟踪该过程。但是,它只上传文件,如果文件在子文件夹中,则不上传子文件夹。

例如:

C:\backup
  backup\testfile1.txt
  backup\test\
  Backup\test\testfile2.txt

我上传到AWS后的结果:

 Testfile1.txt
 Testfile2.txt

我想要的是:

Testfile1.txt
Test\testfile2.txt

     static void Main(string[] args)
        {
            var credentials = new BasicAWSCredentials("keyID", "secrectKey");
            s3Client = new AmazonS3Client(credentials, bucketRegion);
            TrackMPUAsync().Wait();

        }

        private static async Task TrackMPUAsync()
        {
            string[] files = Directory.GetFileSystemEntries(sourcePath, "*", SearchOption.AllDirectories);
           // string[] directories = Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories);


            try
            {
                var fileTransferUtility = new TransferUtility(s3Client);

                // Use TransferUtilityUploadRequest to configure options.
                // In this example we subscribe to an event.
                foreach(string ss in files)
                {
                    Console.WriteLine(ss);

                        var uploadRequest =
                    new TransferUtilityUploadRequest
                    {
                        BucketName = bucketName,
                        FilePath = ss,

                    };

                        uploadRequest.UploadProgressEvent +=
                            new EventHandler<UploadProgressArgs>
                                (uploadRequest_UploadPartProgressEvent);

                        await fileTransferUtility.UploadAsync(uploadRequest);
                        Console.WriteLine("Upload " + ss + " completed");

                }


            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }

        static void uploadRequest_UploadPartProgressEvent(object sender, UploadProgressArgs e)
        {
            // Process event.
            Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes);
        }

Amazon S3 实际上并不使用文件夹或目录。

相反,对象 Key(实际上是文件名)包含对象 的完整路径。

因此,该文件存储在S3中时应命名为Test/testfile2.txt

Amazon S3 管理控制台会让它 'look' 好像有文件夹,但它们实际上并不存在。

例如,您可以将一个对象上传到 folder1/folder2/foo.txt,它会 'appear' 就好像 S3 已经创建了两个文件夹,但如果对象被删除,文件夹也会消失。

因此,您应该更改代码,以便 Key 包含对象在 Amazon S3 存储桶中的完整路径。