aws Verified current stable Not installed? Cloud Infrastructure

Aws / Apply Bucket Policy

Apply Bucket Policy

Applies a bucket policy to a specified S3 bucket.

$
Terminal
aws s3api put-bucket-policy --bucket <bucket_name> --policy file://<path/to/bucket_policy.json>

When To Use

When enforcing access policies or compliance mandates for S3 data.

Pro Tip

Policy syntax errors can lead to immediate rejections; validate JSON syntax before applying.

Command Builder

Tune the command before you copy it

Back to syntax
$
Generated Command
aws s3api put-bucket-policy --bucket <bucket_name> --policy file://<path/to/bucket_policy.json>

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
{ 
    "ResponseMetadata": {
        "RequestId": "XXXXXXXXXXX",
        "HostId": "XXXXXXXXXXX",
        "HTTPStatusCode": 200,
        "HTTPHeaders": {
            "x-amz-id-2": "XXXXXXXXXXX",
            "x-amz-request-id": "XXXXXXXXXXX",
            "date": "Tue, 01 Jan 2024 12:00:00 GMT"
        },
        "RetryAttempts": 0
    }
}

Anatomy of Output

Understanding the result

Applying bucket policy to 'unique-bucket-name'... Policy Application Status

Initiates the application process of the bucket policy.

Bucket policy applied successfully: 'unique-bucket-name' Success Status

Confirms the successful application of the policy.

Policy document size: 2034 bytes Policy Size

Displays size of the applied policy document.

Troubleshooting

Common pitfalls

An error occurred (NoSuchBucket) when calling the PutBucketPolicy operation: The specified bucket does not exist

Solution: Ensure the bucket name is correct.

An error occurred (InvalidJSON) when calling the PutBucketPolicy operation: Invalid JSON

Solution: Validate the JSON structure of the policy file.

An error occurred (BucketAlreadyExists) when calling the PutBucketPolicy operation: Bucket already exists

Solution: Check S3 for existing compliance structures.

Command Breakdown

What each part is doing

aws
Base Command
The executable that performs this operation. Here it runs Aws before the shell applies any redirect operators.
<bucket_name>
bucket name
The value supplied for bucket name.
<path/to/bucket_policy.json>
path to bucket policy.json
The value supplied for path to bucket policy.json.
--bucket
Command Option
Tool-specific option used by this command invocation.
--policy
Command Option
Tool-specific option used by this command invocation.

How To Run

Execution path

  1. Step 1

    Run the command: aws s3api put-bucket-policy --bucket my-bucket --policy file://policy.json

  2. Step 2

    Verify the policy with: aws s3api get-bucket-policy --bucket my-bucket

  3. Step 3

    Check policy details output for confirmation.

Alternative Approaches

Comparable commands in other tools

Alternative cloud infrastructure tools for the same job.