Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Copy an Object Using the AWS SDK for .NET

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.

Copying Objects

1

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

2

Execute one of the AmazonS3.CopyObject. You need to provide information such as source bucket, source key name, target bucket, and target key name. You provide this information by creating an instance of the CopyObjectRequest class.


The following C# code sample demonstrates the preceding tasks.

static AmazonS3 client;
client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                    accessKeyID, secretAccessKeyID);

CopyObjectRequest request = new CopyObjectRequest();
request.SourceBucket = bucketName;
request.SourceKey = keyName;
request.DestinationBucket = bucketName;
request.DestinationKey = destKeyName;
S3Response response = client.CopyObject(request);

Example

The following C# code example makes a copy of an object in the same source bucket. The example illustrates the use of the AmazonS3.CopyObject method. For instructions on how to create and test a working sample, see Testing the .NET Code Examples

using System;
using System.Configuration;
using System.Collections.Specialized;

using Amazon.S3;
using Amazon.S3.Model;

namespace s3.amazon.com.docsamples.copyobject
{
    class S3Sample
    {
        static string bucketName = "*** Provide bucket name ***";
        static string keyName = "*** Provide key name ***";
        static string destKeyName = "*** Provide destination key name ***";
        static AmazonS3 client;

        public static void Main(string[] args)
        {
            if (checkRequiredFields())
            {
                NameValueCollection appConfig =
                    ConfigurationManager.AppSettings;

                string accessKeyID = appConfig["AWSAccessKey"];
                string secretAccessKeyID = appConfig["AWSSecretKey"];

                using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                    accessKeyID, secretAccessKeyID))
                {
                    Console.WriteLine("Copying an object");
                    CopyingObject();
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        static bool checkRequiredFields()
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            if (string.IsNullOrEmpty(appConfig["AWSAccessKey"]))
            {
                Console.WriteLine(
                    "AWSAccessKey was not set in the App.config file.");
                return false;
            }
            if (string.IsNullOrEmpty(appConfig["AWSSecretKey"]))
            {
                Console.WriteLine(
                    "AWSSecretKey was not set in the App.config file.");
                return false;
            }
            if (string.IsNullOrEmpty(bucketName))
            {
                Console.WriteLine("The variable bucketName is not set.");
                return false;
            }
            if (string.IsNullOrEmpty(keyName))
            {
                Console.WriteLine("The variable keyName is not set.");
                return false;
            }

            return true;
        }

        static void CopyingObject()
        {
            try
            {
                // simple object put
                CopyObjectRequest request = new CopyObjectRequest();
                request.SourceBucket = bucketName;
                request.SourceKey = keyName;
                request.DestinationBucket = bucketName;
                request.DestinationKey = destKeyName;
                S3Response response = client.CopyObject(request);
                response.Dispose();
            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message,
                                  s3Exception.InnerException);
            }
        }
    }
}