我正在使用 lambda 创建预签名 URL 来下载位于 S3 存储桶中的文件 - 代码有效,我得到了一个 URL,但是当尝试访问它时,我得到了
af-south-1 location constraint is incompatible for the region-specific endpoint this request was sent to.
桶和 lambda 都在同一区域
我对实际发生的情况感到茫然,任何想法或解决方案将不胜感激。
我的代码如下
import json
import boto3
import boto3.session
def lambda_handler(event, context):
session = boto3.session.Session(region_name='af-south-1')
s3 = session.client('s3')
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
url = s3.generate_presigned_url(ClientMethod='get_object',
Params={'Bucket': bucket,
'Key': key}, ExpiresIn = 400)
print (url)```
在生成
endpoint_url=https://s3.af-south-1.amazonaws.com
的同时设置
s3_client
s3_client = session.client('s3',
region_name='af-south-1',
endpoint_url='https://s3.af-south-1.amazonaws.com')
您能否尝试直接使用
boto3
client 而不是通过 session,并生成预签名 url :
import boto3
import requests
# Get the service client.
s3 = boto3.client('s3',region_name='af-south-1')
# 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'
}
)
s3_client = session.client('s3',
region_name='af-south-1',
endpoint_url='https://{s3_regien_name}.amazonaws.com' # add this line