使用python boto3使用v4签名生成S3预签名URL

时间:2017-09-21 02:22:01

标签: python amazon-s3

以下不起作用:

来自boto 3 docs:

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

这是我的带有占位符存储桶和键值的脚本:

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以403:

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

1 个答案:

答案 0 :(得分:1)

您需要为boto3客户端提供aws凭据。 Docs here

如果您需要获取有关访问凭据的帮助,可以查看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,
)