【问题标题】:AWS SDK .Net API - Run Command in Script?AWS SDK .Net API - 在脚本中运行命令?
【发布时间】:2018-05-12 15:56:07
【问题描述】:

使用 AWS SDK .NET API,有什么方法可以发送 AWS CLI 命令?

我需要定期“刷新”存储桶的缓存控制策略(以及位于该存储桶中的所有图像),并且我想从 .Net C# Web 应用程序触发/运行它。

脚本如下:

 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

我从这个解决方案中得到: Set cache-control for entire S3 bucket automatically (using bucket policies?)

有没有办法通过 API 将此命令发送到亚马逊?我是这方面的新手,所以实际的代码会有所帮助(即如何验证和发送它)。如果不是 API,有没有办法将其作为 REST 查询发送?

【问题讨论】:

  • 您可以使用他们的 .NET SDK here's an example 做同样的事情,但在您的情况下,您需要在 CopyObjectRequest 上设置更多属性。您可能还需要编写自己的递归。

标签: c# .net amazon-s3 aws-sdk


【解决方案1】:

好吧,为了将来参考,这就是我最终得到的似乎可行的方法

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

}

【讨论】:

    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2019-10-25
    相关资源
    最近更新 更多