Skip to content

keithweaver/python-aws-s3

Repository files navigation

python-aws-s3

About

This is a demo of setting up an Amazon Web Service (AWS) S3 bucket and uploading a file with Python.

Setting Up Bucket

Open AWS Console and log in.

alt text

Click the Services dropdown and select the S3 service.

alt text

alt text

Click Create Bucket. Give it a name, region then hit next through each step.

alt text

alt text

alt text

alt text

alt text

Now click your new bucket

alt text

Upload a test image to your bucket

alt text

alt text

alt text

alt text

You can find your new file. If you click it, you should see a link. Open the link in a new tab.

alt text

As you can see, you'll get "Access Denied".

alt text

Click the file, and under "more" press make public. Refresh the link.

alt text

alt text

alt text

Now click Services then go to IAM dashboard.

alt text

You should see your IAM dashboard. On the left menu, you can click Users.

alt text

alt text

Click the Add User.

alt text

alt text

alt text

alt text

alt text

Now click your new user from the list of users.

alt text

Copy the User ARN

alt text

Reopen the S3 dashboard

alt text

alt text

Now click the permissions tab.

alt text

Then click Bucket Policy.

alt text

alt text

Set your Bucket Policy to be the same as below. Change arn:aws:iam::281979644754:user/sample-user to be your User ARN. Also change arn:aws:s3:::img-bucket-00123 to your Bucket ARN. The bucket ARN is above the textarea.

{
    "Version": "2012-10-17",
    "Id": "Policy1488494182833",
    "Statement": [
        {
            "Sid": "Stmt1488493308547",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::281979644754:user/sample-user"
            },
            "Action": [
                "s3:ListBucket",
                "s3:ListBucketVersions",
                "s3:GetBucketLocation",
                "s3:Get*",
                "s3:Put*"
            ],
            "Resource": "arn:aws:s3:::img-bucket-00123"
        }
    ]
}

Click CORS configuration and add the following policy:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
  </CORSRule>
</CORSConfiguration>

Reopen the IAM dashboard.

alt text

Open your new user.

alt text

Click on the New inline policy

alt text

alt text

Update the policy to be as follows:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets",
                "s3:PutObject",
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::*"
            ]
        }
    ]
}
git clone https://github.com/keithweaver/python-aws-s3.git
cd python-aws-s3
python example.py

alt text

python example-w-folder-create.py

alt text

Releases

No releases published

Packages

No packages published

Languages