使用 boto3 在 S3 中生成签名 URL

Generate Signed URL in S3 using boto3

在 Boto 中,我曾经使用以下函数生成签名的 URL。

import boto
conn = boto.connect_s3()
bucket = conn.get_bucket(bucket_name, validate=True)
key = bucket.get_key(key)
signed_url = key.generate_url(expires_in=3600)

如何在 boto3 中执行完全相同的操作?
我搜索了 boto3 GitHub codebase 但找不到对 generate_url.
的单个引用 功能名称是否更改?

来自Generating Presigned URLs

import boto3
import requests

# Get the service client.
s3 = boto3.client('s3')

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

# Use the URL to perform the GET operation. You can use any method you like
# to send the GET, but we will use requests here to keep things simple.
response = requests.get(url)

函数参考:generate_presigned_url()

I get error InvalidRequestThe authorization mechanism you have provided is not supported when trying to access the url generated in normal browser – Aseem Apr 30 '19 at 5:22

由于信息不多,我假设您遇到了签名版本问题,如果没有,也许它会对其他人有所帮助! :P

为此,您可以从 botocore 导入配置:-

from botocore.client import Config

然后让客户端使用此配置并提供签名版本 's3v4'

s3 = boto3.client('s3', config=Config(signature_version='s3v4'))