AWS Code Sample

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.


UploadDirMPUHighLevelAPITest.cs demonstrates how to upload an entire directory using the TransferUtility class.

/** * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ 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); } } } }

Sample Details

Service: s3

Last tested: 2018-04-30

Author: AWS

Type: full-example

On this page: