Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

上传目录

您可以使用 TransferUtility 类上传整个目录。默认情况下,该 API 仅上传位于指定目录的根目录中的文件。但是,您可以指定以递归方式上传所有子目录中的文件。

要根据筛选条件选择指定目录中的文件,请指定筛选表达式。例如,要从目录中仅上传 .pdf 文件,请指定 "*.pdf" 筛选表达式。

在从目录中上传文件时,您不必为生成的对象指定键名。Amazon S3 会使用原始文件路径构造键名。例如,假设您有一个名为 c:\myfolder 的目录,并且此目录具有以下结构:

C:\myfolder \a.txt \b.pdf \media\ An.mp3

上传此目录时,Amazon S3 将使用以下键名称:

a.txt b.pdf media/An.mp3

以下 C# 示例将目录上传到 Amazon S3 存储桶。它说明如何使用各种 TransferUtility.UploadDirectory 重载来上传目录。每个对上传的后续调用都将替换先前的上传。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

using Amazon.S3; using Amazon.S3.Transfer; using System; using System.IO; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class UploadDirMPUHighLevelAPITest { private const string existingBucketName = "*** bucket name ***"; private const string directoryPath = @"*** directory path ***"; // The example uploads only .txt files. private const string wildCard = "*.txt"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 s3Client; static void Main() { s3Client = new AmazonS3Client(bucketRegion); UploadDirAsync().Wait(); } private static async Task UploadDirAsync() { try { var directoryTransferUtility = new TransferUtility(s3Client); // 1. Upload a directory. await directoryTransferUtility.UploadDirectoryAsync(directoryPath, existingBucketName); Console.WriteLine("Upload statement 1 completed"); // 2. Upload only the .txt files from a directory // and search recursively. await directoryTransferUtility.UploadDirectoryAsync( directoryPath, existingBucketName, wildCard, SearchOption.AllDirectories); Console.WriteLine("Upload statement 2 completed"); // 3. The same as Step 2 and some optional configuration. // Search recursively for .txt files to upload. var request = new TransferUtilityUploadDirectoryRequest { BucketName = existingBucketName, Directory = directoryPath, SearchOption = SearchOption.AllDirectories, SearchPattern = wildCard }; await directoryTransferUtility.UploadDirectoryAsync(request); Console.WriteLine("Upload statement 3 completed"); } catch (AmazonS3Exception e) { Console.WriteLine( "Error encountered ***. 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); } } } }