Set the policy associated with a specified bucket.
Declaration Syntax
C# |
public PutBucketPolicyResponse PutBucketPolicy( PutBucketPolicyRequest request )
Parameters
- request (PutBucketPolicyRequest)
- The PutBucketPolicyRequest that defines the parameters of the operation.
Return Value
Returns a PutBucketPolicyResponse from S3.
Remarks
Sets the policy associated with the specified bucket. Only the owner of the bucket can set a bucket policy. If a policy already exists for the specified bucket, the new policy will replace the existing policy.
Bucket policies provide access control management at the bucket level for both the bucket resource and contained object resources. Only one policy may be specified per-bucket.
For more information on forming bucket polices, see: http://docs.amazonwebservices.com/AmazonS3/latest/dev/
Examples
This example shows how to Get, Put and Delete bucket policies.
CopyBucketPolicy samples
// Create a client AmazonS3Client client = new AmazonS3Client(); // Put sample bucket policy (overwrite an existing policy) string newPolicy = @"{ ""Statement"":[{ ""Sid"":""BasicPerms"", ""Effect"":""Allow"", ""Principal"": { ""AWS"": ""*"" }, ""Action"":[""s3:PutObject"",""s3:GetObject""], ""Resource"":[""arn:aws:s3:::samplebucketname/*""] }]}"; PutBucketPolicyRequest putRequest = new PutBucketPolicyRequest { BucketName = "SampleBucket", Policy = newPolicy }; client.PutBucketPolicy(putRequest); // Retrieve current policy GetBucketPolicyRequest getRequest = new GetBucketPolicyRequest { BucketName = "SampleBucket" }; string policy = client.GetBucketPolicy(getRequest).Policy; Console.WriteLine(policy); Debug.Assert(policy.Contains("BasicPerms")); // Delete current policy DeleteBucketPolicyRequest deleteRequest = new DeleteBucketPolicyRequest { BucketName = "SampleBucket" }; client.DeleteBucketPolicy(deleteRequest); // Retrieve current policy and verify that it is null policy = client.GetBucketPolicy(getRequest).Policy; Debug.Assert(policy == null);*/
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | |
WebException | |
AmazonS3Exception |