상위 수준 .NET TransferUtility 클래스를 사용하여 디렉터리 업로드 - Amazon Simple Storage Service

상위 수준 .NET TransferUtility 클래스를 사용하여 디렉터리 업로드

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; 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); } } } }