user3642049
user3642049

Reputation: 51

How can I upload multiple files to Amazon S3 asynchronously using the AWS SDK for .NET?

I am trying to upload multiple files asynchronously on Amazon S3 using the .NET SDK. Any examples to get me started will be greatly appreciated. Thanks in advance.

Upvotes: 3

Views: 6377

Answers (1)

Steffen Opel
Steffen Opel

Reputation: 64711

The Amazon S3 and AWS SDK for .NET functionality you are looking for is Using the High-Level .NET API for Multipart Upload:

The AWS SDK for .NET exposes a high-level API that simplifies multipart upload (see Uploading Objects Using Multipart Upload API). You can upload data from a file, directory, or a stream. [...] You can optionally set advanced options such as the part size you want to use for the multipart upload, number of threads you want to use when uploading the parts concurrently, optional file metadata, the storage class (STANDARD or REDUCED_REDUNDANCY), or ACL. The high-level API provides the TransferUtilityUploadRequest class to set these advanced options. [emphasis mine]

An example snippet is provided in Upload a Directory:

using System;
using System.IO;
using Amazon.S3;
using Amazon.S3.Transfer;

namespace s3.amazon.com.docsamples
{
    class UploadDirectoryMPUHighLevelAPI
    {
        static string existingBucketName = "*** Provide bucket name ***";
        static string directoryPath      = "*** Provide directory name ***";

        static void Main(string[] args)
        {
            try
            {
                TransferUtility directoryTransferUtility =
                    new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));

                // 1. Upload a directory.
                directoryTransferUtility.UploadDirectory(directoryPath,
                                                         existingBucketName);
                Console.WriteLine("Upload statement 1 completed");

                // 2. Upload only the .txt files from a directory. 
                //    Also, search recursively. 
                directoryTransferUtility.UploadDirectory(
                                               directoryPath,
                                               existingBucketName,
                                               "*.txt",
                                               SearchOption.AllDirectories);
                Console.WriteLine("Upload statement 2 completed");

                // 3. Same as 2 and some optional configuration 
                //    Search recursively for .txt files to upload).
                TransferUtilityUploadDirectoryRequest request =
                    new TransferUtilityUploadDirectoryRequest
                    {
                        BucketName = existingBucketName,
                        Directory = directoryPath,
                        SearchOption = SearchOption.AllDirectories,
                        SearchPattern = "*.txt"
                    };

                directoryTransferUtility.UploadDirectory(request);
                Console.WriteLine("Upload statement 3 completed");
            }

            catch (AmazonS3Exception e)
            {
                Console.WriteLine(e.Message, e.InnerException);
            }
        }
    }
}

Upvotes: 2

Related Questions