Can I Run Command in Script with AWS SDK .Net API

0 votes

 Is there any way of sending an AWS CLI command if I use AWS SDK .NET API? 
To periodically 'refresh' a Bucket's cache-control policy and all the images located in that bucket also I want to trigger/run it from a .Net C# Web Application.

Here is the script I am using:

 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

I got this solution from this website:

Can I send this command to Amazon via the API? If not the API, is there a way to send it as a REST Query?

May 17, 2018 in AWS by Flying geek
• 3,150 points

1 answer to this question.

0 votes

Try this code, will help you for sure: 

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

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


        //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);

answered May 17, 2018 by Cloud gunner
• 4,260 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

AWS EC2 Free Tier- how many instances can i run?

Amazon has a handy page that details ...READ MORE

answered Jul 6, 2018 in AWS by Priyaj
• 56,160 points
0 votes
1 answer
+2 votes
3 answers
+1 vote
2 answers

How do I run python script on aws EC2 instance(ubuntu)?

I believe that you are using the ...READ MORE

answered Apr 17, 2018 in AWS by Cloud gunner
• 4,260 points