Using AWS command-line tools for managing S3

From Notes_Wiki

Home > Amazon web services > Using AWS command-line tools for managing S3

Listing buckets

After configuring appropriate AWS credentials, we can list current S3 buckets using:

aws s3api list-buckets


Creating bucket

A new S3 bucket can be created using:

aws s3 mb s3://<bucket-name>


Updating bucket CORS policy

We can update bucket CORS plicy using by using a cors policy file in JSON format, such as:

{
    "CORSRules": [
        {
            "AllowedHeaders": [
                "*"
            ], 
            "ExposeHeaders": [
                "x-amz-server-side-encryption", 
                "x-amz-request-id", 
                "x-amz-id-2"
            ], 
            "MaxAgeSeconds": 3000, 
            "AllowedMethods": [
                "GET", 
                "PUT", 
                "POST", 
                "DELETE"
            ], 
            "AllowedOrigins": [
                "*"
            ]
        }
    ]
}

This policy can be applied to a bucket using:

aws s3api put-bucket-cors --bucket <bucket-name> --cors-configuration file://cors.txt

where 'cors.txt' is the name of file where desired cors configuration is stored in current folder.


Updating bucket security policy

We can update bucket security policy using policy in JSON format such as:

{
    "Version": "2008-10-17",
    "Id": "PolicyN",
    "Statement": [
        {
        "Sid": "StmtM",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::P:user/<aws-username>"
        },
        "Action": "s3:*",
        "Resource": "arn:aws:s3:::<bucket-name>/*"
        }
    ]
}

where N, M and P should be substituted by auto-generated unique numbers. The policy can be applied to a bucket using command:

aws s3api put-bucket-policy --bucket <bucket-name> --policy file://policy.txt

where 'policy.txt' is the name of file where policy is stored.


Empty bucket

To empty or delete all the contents of a bucket we can use:

aws s3 rm s3://<bucket-name> --recursive


Deleting bucket

To delete a bucket we can use:

aws s3 rb s3://<bucket-name> --force



Home > Amazon web services > Using AWS command-line tools for managing S3