Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Upload a File

The following tasks guide you through using the high-level .NET classes to upload a file. The API provides several variations, overloads, of the Upload method to easily upload your data.

High-Level API File Uploading Process

1

Create an instance of the TransferUtility class by providing your AWS credentials.

2

Execute one of the TransferUtility.Upload overloads depending on whether you are uploading data from a file, a stream, or a directory.


The following C# code sample demonstrates the preceding tasks.

TransferUtility utility = new TransferUtility(accessKeyID, secretAccessKey);
utility.Upload(filePath, existingBucketName);			

When uploading large files using the .NET API, timeout might occur even while data is being written to the request stream. You can set explicit timeout using the TransferUtilityConfig.DefaultTimeout as demonstrated in the following C# code sample.

TransferUtilityConfig config = new TransferUtilityConfig();
config.DefaultTimeout = 11111;
TransferUtility utility = new TransferUtility(accessKeyID, secretAccessKey, config);

Example

The following C# code example uploads a file to an Amazon S3 bucket. The example illustrates the use of various TransferUtility.Upload overloads to upload a file; each successive call to upload replaces the previous upload. For instructions on how to create and test a working sample, see Testing the .NET Code Examples

using System;
using System.Configuration;
using Amazon.S3;
using Amazon.S3.Model;
using System.Collections.Specialized;
using Amazon.S3.Transfer;
using System.IO;

namespace s3.amazon.com.docsamples.highlevel_uploadfile
{
    class Program
    {
        static string accessKeyID     = "";
        static string secretAccessKey = "";

        static string existingBucketName = "*** Provide bucket name ***";
        static string keyName        = "*** Provide your object key ***";
        static string filePath       = "*** Provide file name ***";

        static void Main(string[] args)
        {

            NameValueCollection appConfig = ConfigurationManager.AppSettings;
            accessKeyID     = appConfig["AWSAccessKey"];
            secretAccessKey = appConfig["AWSSecretKey"];

            try
            {
                TransferUtility fileTransferUtility = new 
                    TransferUtility(accessKeyID, secretAccessKey);

                // 1. Upload a file, file name is used as the object key name.
                fileTransferUtility.Upload(filePath, existingBucketName);
                Console.WriteLine("Upload 1 completed");

                // 2. Specify object key name explicitly.
                fileTransferUtility.Upload(filePath, 
                                          existingBucketName, keyName);
                Console.WriteLine("Upload 2 completed");

                // 3. Upload data from a type of System.IO.Stream.
                using (FileStream fileToUpload = 
                    new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    fileTransferUtility.Upload(fileToUpload, 
                                               existingBucketName, keyName);
                }
                Console.WriteLine("Upload 3 completed");

                // 4.// Specify advanced settings/options.
                TransferUtilityUploadRequest fileTransferUtilityRequest = 
                    new TransferUtilityUploadRequest()
                    .WithBucketName(existingBucketName)
                    .WithFilePath(filePath)
                    .WithStorageClass(S3StorageClass.ReducedRedundancy)
                    .WithMetadata("param1", "Value1")
                    .WithMetadata("param2", "Value2")
                    .WithPartSize(6291456) // This is 6 MB.
                    .WithKey(keyName)
                    .WithCannedACL(S3CannedACL.PublicRead);
                fileTransferUtility.Upload(fileTransferUtilityRequest);
                Console.WriteLine("Upload 4 completed");
            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message, 
                                  s3Exception.InnerException);
            }
        }
    }
}