RandomUser
RandomUser

Reputation: 4220

Generate S3 pre-signed URL with v4 signature using python boto3

The following does not work:

From the boto 3 docs:

http://boto3.readthedocs.io/en/latest/guide/s3.html#generating-presigned-urls

This is my script with placeholder bucket and key values:

import boto3
import requests
from botocore.client import Config

# Get the service client.
s3 = boto3.client('s3', config=Config(signature_version='s3v4'))

# Generate the URL to get 'key-name' from 'bucket-name'
url = s3.generate_presigned_url(
    ClientMethod='get_object',
    Params={
        'Bucket': 'mybucketname',
        'Key': 'myObject.txt'
    }
)

print url
response = requests.get(url)
print response

S3 responds with a 403:

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>B5681E888657E2A1</RequestId>
<HostId>
FMS7oPPOXt4I0KXPPQwdBx2fyxze+ussMmy/BOWLVFusWMoU2zAErE08ez34O6VhSYRvIYFm7Bs=
</HostId>
</Error>

Upvotes: 5

Views: 9233

Answers (1)

Kyle
Kyle

Reputation: 1066

You need to provide aws credentials with your boto3 client. Docs here

If you need help getting access to your credentials on aws you can look here.

import boto3
client = boto3.client(
    's3',
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
    aws_session_token=SESSION_TOKEN,
)

# Or via the Session
session = boto3.Session(
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
    aws_session_token=SESSION_TOKEN,
)

Upvotes: 5

Related Questions