AWS 预签名 URLS 位置约束与此请求发送到的区域特定终端节点不兼容

问题描述 投票:0回答:3

我正在使用 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)```
python amazon-web-services aws-lambda boto3
3个回答
5
投票

在生成

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')

0
投票

您能否尝试直接使用

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'
    }
)

您还可以看看这些 12,它们类似于同一个问题。


0
投票
 s3_client = session.client('s3', 
 region_name='af-south-1',
 endpoint_url='https://{s3_regien_name}.amazonaws.com' # add this line
© www.soinside.com 2019 - 2024. All rights reserved.