最近更新时间:2021-10-29
以下代码用于设置桶的policy。以列举桶为例。
using System; using Amazon.S3; using Amazon.S3.Model;
namespace PutBucketPolicy { class Program { static void Main(string[] args) { var Ak = "xxx"; var Sk = "xxx"; var endpoint = "http://s3.test.com"; AmazonS3Client serviceClient = new AmazonS3Client(Ak,Sk, new AmazonS3Config{ ServiceURL = endpoint });
try { PutBucketPolicyResponse response; // Put sample bucket policy (overwrite an existing policy) string newPolicy = @"{ ""Version"":""2012-10-17"", ""Statement"":[{ ""Effect"":""Allow"", ""Action"":[""s3:ListBucket""], ""Resource"":[""arn:aws:s3:::*""] }]}"; PutBucketPolicyRequest putRequest = new PutBucketPolicyRequest { BucketName = "xxx", Policy = newPolicy }; response = serviceClient.PutBucketPolicyAsync(putRequest).GetAwaiter().GetResult(); //Console.WriteLine(response);
} catch (AmazonS3Exception e) { Console.WriteLine(e.Message); throw; } catch (Exception e) { Console.WriteLine(e); throw; } } } } |