Menu
Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Enabling Cross-Origin Resource Sharing (CORS) Using the AWS SDK for .NET

You can use the AWS SDK for .NET to manage cross-origin resource sharing (CORS) for a bucket. For more information about CORS, see Cross-Origin Resource Sharing (CORS).

This section provides sample code for the tasks in the following table, followed by a complete example program listing.

Managing Cross-Origin Resource Sharing

1

Create an instance of the AmazonS3Client class.

2

Create a new CORS configuration.

3

Retrieve and modify an existing CORS configuration.

4

Add the configuration to the bucket.


Cross-Origin Resource Sharing Methods

AmazonS3Client()

Constructs AmazonS3Client with the credentials defined in the App.config file.

PutCORSConfiguration()

Sets the CORS configuration that should be applied to the bucket. If a configuration already exists for the specified bucket, the new configuration will replace the existing one.

GetCORSConfiguration()

Retrieves the CORS configuration for the specified bucket. If no configuration has been set for the bucket, the Configuration header in the response will be null.

DeleteCORSConfiguration()

Deletes the CORS configuration for the specified bucket.


For more information about the AWS SDK for .NET API, go to Using the AWS SDK for .NET.

Creating an Instance of the AmazonS3 Class

The following sample creates an instance of the AmazonS3Client class.

static IAmazonS3 client;
using (client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2))

Adding a CORS Configuration to a Bucket

To add a CORS configuration to a bucket:

  1. Create a CORSConfiguration object describing the rule.

  2. Create a PutCORSConfigurationRequest object that provides the bucket name and the CORS configuration.

  3. Add the CORS configuration to the bucket by calling client.PutCORSConfiguration.

The following sample creates two rules, CORSRule1 and CORSRule2, and then adds each rule to the rules array. By using the rules array, it then adds the rules to the bucket bucketName.

// Add a sample configuration
CORSConfiguration configuration = new CORSConfiguration
{
    Rules = new System.Collections.Generic.List<CORSRule>
    {
        new CORSRule
        {
            Id = "CORSRule1",
            AllowedMethods = new List<string> {"PUT", "POST", "DELETE"},
            AllowedOrigins = new List<string> {"http://*.example.com"}
        },
        new CORSRule
        {
            Id = "CORSRule2",
            AllowedMethods = new List<string> {"GET"},
            AllowedOrigins = new List<string> {"*"},
            MaxAgeSeconds = 3000,
            ExposeHeaders = new List<string> {"x-amz-server-side-encryption"}
        }
    }
};

// Save the configuration
PutCORSConfiguration(configuration);

static void PutCORSConfiguration(CORSConfiguration configuration)
{

    PutCORSConfigurationRequest request = new PutCORSConfigurationRequest
    {
        BucketName = bucketName,
        Configuration = configuration
    };

    var response = client.PutCORSConfiguration(request);
}

Updating an Existing CORS Configuration

To update an existing CORS configuration

  1. Get a CORS configuration by calling the client.GetCORSConfiguration method.

  2. Update the configuration information by adding or deleting rules.

  3. Add the configuration to a bucket by calling the client.PutCORSConfiguration method.

The following snippet gets an existing configuration and then adds a new rule with the ID NewRule.

// Get configuration.
configuration = GetCORSConfiguration(); 
// Add new rule.
configuration.Rules.Add(new CORSRule
{
    Id = "NewRule",
    AllowedMethods = new List<string> { "HEAD" },
    AllowedOrigins = new List<string> { "http://www.example.com" }
});

// Save configuration.
PutCORSConfiguration(configuration);

Example Program Listing

The following C# program incorporates the preceding tasks.

For information about creating and testing a working sample, see Running the Amazon S3 .NET Code Examples.

using System;
using System.Configuration;
using System.Collections.Specialized;
using System.Net;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Util;
using System.Diagnostics;
using System.Collections.Generic;

namespace s3.amazon.com.docsamples
{
    class CORS
    {
        static string bucketName = "*** Provide bucket name ***"; 

        static IAmazonS3 client;

        public static void Main(string[] args)
        {
            try
            {
                using (client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2))
                {
                    // Create a new configuration request and add two rules    
                    CORSConfiguration configuration = new CORSConfiguration
                    {
                        Rules = new System.Collections.Generic.List<CORSRule>
                        {
                          new CORSRule
                          {
                            Id = "CORSRule1",
                            AllowedMethods = new List<string> {"PUT", "POST", "DELETE"},
                            AllowedOrigins = new List<string> {"http://*.example.com"}
                          },
                          new CORSRule
                          {
                            Id = "CORSRule2",
                            AllowedMethods = new List<string> {"GET"},
                            AllowedOrigins = new List<string> {"*"},
                            MaxAgeSeconds = 3000,
                            ExposeHeaders = new List<string> {"x-amz-server-side-encryption"}
                          }
                        }
                    };

                    // Add the configuration to the bucket 
                    PutCORSConfiguration(configuration);

                    // Retrieve an existing configuration 
                    configuration = GetCORSConfiguration();

                    // Add a new rule.
                    configuration.Rules.Add(new CORSRule
                    {
                        Id = "CORSRule3",
                        AllowedMethods = new List<string> { "HEAD" },
                        AllowedOrigins = new List<string> { "http://www.example.com" }
                    });

                    // Add the configuration to the bucket 
                    PutCORSConfiguration(configuration);

                    // Verify that there are now three rules
                    configuration = GetCORSConfiguration();
                    Console.WriteLine(); 
                    Console.WriteLine("Expected # of rulest=3; found:{0}", configuration.Rules.Count);
                    Console.WriteLine();
                    Console.WriteLine("Pause before configuration delete. To continue, click Enter...");
                    Console.ReadKey();

                    // Delete the configuration
                    DeleteCORSConfiguration();

                    // Retrieve a nonexistent configuration
                    configuration = GetCORSConfiguration();
                    Debug.Assert(configuration == null);
                }

                Console.WriteLine("Example complete.");
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString());
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
                Console.ReadKey();
            }
            
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        static void PutCORSConfiguration(CORSConfiguration configuration)
        {

            PutCORSConfigurationRequest request = new PutCORSConfigurationRequest
            {
                BucketName = bucketName,
                Configuration = configuration
            };

            var response = client.PutCORSConfiguration(request);
        }

        static CORSConfiguration GetCORSConfiguration()
        {
            GetCORSConfigurationRequest request = new GetCORSConfigurationRequest
            {
                BucketName = bucketName

            };
            var response = client.GetCORSConfiguration(request);
            var configuration = response.Configuration;
            PrintCORSRules(configuration);
            return configuration;
        }

        static void DeleteCORSConfiguration()
        {
            DeleteCORSConfigurationRequest request = new DeleteCORSConfigurationRequest
            {
                BucketName = bucketName
            };
            client.DeleteCORSConfiguration(request);
        }

        static void PrintCORSRules(CORSConfiguration configuration)
        {
            Console.WriteLine();

            if (configuration == null)
            {
                Console.WriteLine("\nConfiguration is null");
                return;
            }

            Console.WriteLine("Configuration has {0} rules:", configuration.Rules.Count);
            foreach (CORSRule rule in configuration.Rules)
            {
                Console.WriteLine("Rule ID: {0}", rule.Id);
                Console.WriteLine("MaxAgeSeconds: {0}", rule.MaxAgeSeconds);
                Console.WriteLine("AllowedMethod: {0}", string.Join(", ", rule.AllowedMethods.ToArray()));
                Console.WriteLine("AllowedOrigins: {0}", string.Join(", ", rule.AllowedOrigins.ToArray()));
                Console.WriteLine("AllowedHeaders: {0}", string.Join(", ", rule.AllowedHeaders.ToArray()));
                Console.WriteLine("ExposeHeader: {0}", string.Join(", ", rule.ExposeHeaders.ToArray()));
            }
        }
    }
}