1

Using the AWS SDK .NET API, is there any way of sending a AWS CLI command?

I need to periodically 'refresh' a Bucket's cache-control policy (and all the images located in that bucket) and I want to trigger/run it from a .Net C# Web Application.

The script is the following:

 aws s3 cp s3://mybucket/ s3://mybucket/ --recursive --metadata-directive REPLACE \ --expires 2034-01-01T00:00:00Z --acl public-read --cache-control max-age=2592000,public

which I got from this solution: Set cache-control for entire S3 bucket automatically (using bucket policies?)

Is there a way to send this command to Amazon via the API? I'm kind of a newbie in this so actual code would be helpful (ie. how to authenticate and send it). If not the API, is there a way to send it as a REST Query?

Mike Smith
  • 618
  • 10
  • 27
  • You could do the same thing using their .NET SDK, [here's an example](http://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjectUsingNetSDK.html) but in your case you'd need to set more properties on the [`CopyObjectRequest`](http://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/T_Amazon_S3_Model_CopyObjectRequest.htm). You may also have to write your own recursion. – Taylor Wood Nov 29 '17 at 04:42

1 Answers1

2

Well, for future reference, here's what I ended up with that seems to be working

string accessKeyID = "ACCESKEYID";
string secretAccessKey = "SECRETACCESSKEY";

using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKey))
{

    try
    {

        //First, get a list of all objects in the bucket.  We need to go through them one at a time.
        ListObjectsRequest listobjectsrequest = new ListObjectsRequest();
        listobjectsrequest = new ListObjectsRequest();
        listobjectsrequest.BucketName = "mybucketname";

        ListObjectsResponse listobjectresponse = client.ListObjects(listobjectsrequest);

        // Process each item
        foreach (S3Object entry in listobjectresponse.S3Objects)
        {
            //Get the object so we can look at the headers to see if it already has a cache control header
            GetObjectRequest getobjectrequest = new GetObjectRequest
            {
                BucketName = listobjectsrequest.BucketName,
                Key = entry.Key,

            };

            GetObjectResponse object1 = client.GetObject(getobjectrequest);

            string cacheControl1 = object1.Headers["Cache-Control"] ?? "none";

            //If no cache control header, then COPY the object to ITSELF but add the headers that we need
            if (cacheControl1 != "none")
            {

                CopyObjectRequest copyobjectrequest = new CopyObjectRequest
                {
                    SourceBucket = listobjectsrequest.BucketName,
                    SourceKey = entry.Key,
                    DestinationBucket = listobjectsrequest.BucketName,
                    DestinationKey = entry.Key,
                    Directive = S3MetadataDirective.REPLACE, //Required if we will overwrite headers
                    CannedACL = S3CannedACL.PublicRead //Need to set to public so it can be read

                };

                copyobjectrequest.AddHeader("Cache-Control", "max-age=31536000,public");
                CopyObjectResponse copyobjectresponse = client.CopyObject(copyobjectrequest);

            }


        }



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

}
Mike Smith
  • 618
  • 10
  • 27