Python Flask AWS S3 EU Central |错误请求不支持您提供的授权机制。请使用AWS4HMACSHA256

2024-04-23 14:59:54 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我知道有许多类似的线程,我红色的所有他们和s3docu(请不要关闭这个线程)。修复方法在任何地方都是一样的:

只需将sugnature_version更改为v4,因为{}是在2014年之后创建的,不再支持{}。在

我已经试过了所有的语法,但仍然有错误。在

session = boto3.Session(
    aws_access_key_id=app.config['MY_AWS_ID'],
    aws_secret_access_key=app.config['MY_AWS_SECRET'],
    region_name='eu-central-1'
)

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

presigned_post = s3.generate_presigned_post(
Bucket = 'mybucket',
Key = 'videos/' + file_name,
Fields = {"acl": "public-read", "Content-Type": file_type},
Conditions = [
  {"acl": "public-read"},
  {"Content-Type": file_type}
],
ExpiresIn = 3600
)

我试着到处换。我还将我的boto3安装降级为1.6.6和{}版本,但效果并不理想。我把它升级到最新版本,它是boto3==1.7.26

错误:

^{pr2}$

每个线程都建议相同的修复,可能因为我使用了Python / Flask,所以它不起作用。一定要用不同的方式来做什么?在

我试图通过客户端直接上传大量视频文件到S3,因此我需要签署请求。在

编辑

我想可能是SSL问题。我正在本地主机上测试所有内容,use_ssl的默认选项是true。在

我尝试将这个版本上传到实时站点(有SSL启用)。没用,还是同样的错误。在

我还试图在localhost上使用use_ssl = False,仍然是相同的错误。在


Tags: key版本awsconfigapps3accessversion
3条回答

问题出在HTML和我如何命名输入字段。我从以前的教程中取了一个例子,但是您必须按照解释的方式构建表单here by amazon

我使用了他们提供的每一条信息。我已经检查了我的响应,该响应是由sign_s3函数生成的,并对表单中的所有相应字段进行了求值。在

这是我的符号功能:

# Sign request for direct file upload through client for video
@app.route('/sign_s3/<path:file_name_data>/<path:file_type_data>/<up_type>', methods=["GET", "POST"])
@login_required
@check_confirmed
def sign_s3(file_name_data, file_type_data, up_type):
    if "localhost" in request.url_root:
        if up_type == "profile_vid":
            file_name = str(current_user.id) + get_random_code(5) + "local-profil-video." + file_name_data.split(".")[-1]
        else:
            file_name = str(current_user.id) + str(randint(1,100)) + "local-post-video-temp." + file_name_data.split(".")[-1]
    else:
        if up_type == "profile_vid":
            file_name = str(current_user.id) + get_random_code(5) + "-profil-video." + file_name_data.split(".")[-1]
        else:
            file_name = str(current_user.id) + str(randint(1,100)) + "-post-video-temp." + file_name_data.split(".")[-1]

    file_type = file_type_data

    session = boto3.Session(
        aws_access_key_id=app.config['MY_AWS_ID'],
        aws_secret_access_key=app.config['MY_AWS_SECRET'],
        region_name='eu-central-1'
    )

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

    presigned_post = s3.generate_presigned_post(
    Bucket = 'mybucket',
    Key = 'videos/' + file_name,
    Fields = {"acl": "public-read", "Content-Type": file_type},
    Conditions = [
      {"acl": "public-read"},
      {"Content-Type": file_type}
    ],
    ExpiresIn = 3600
    )

    if up_type == "profile_vid":
        if current_user.profile_video != None:
            delete_file_from_aws("videos/", current_user.profile_video)
        setattr(current_user, "profile_video", file_name)
    else:
        print ('post video has been uploaded, no need to delete or set here')

    db_session.commit()

    return json.dumps({'data': presigned_post, 'url': 'https://s3.eu-central-1.amazonaws.com/mybucket/' + 'videos/' + file_name, 'created_file_name' : file_name})

我查看了开发控制台中生成的响应,其中有以下值:

enter image description here

我使用的HTML表单在这里,所有未注释的输入字段都没有被我使用过。我只是简单地将它们包括在亚马逊的示例中:

^{pr2}$

还请注意,文件输入必须位于底部,因为:

elements after this will be ignored

在我的例子中,表单的值是动态创建的,所以我用JS填充表单:

$('#direct_s3_profile_video_form').find('input[name="key"]').val(response_json_data.data.fields['key']);
$('#direct_s3_profile_video_form').find('input[name="acl"]').val(response_json_data.data.fields['acl']);
$('#direct_s3_profile_video_form').find('input[name="Content-Type"]').val(response_json_data.data.fields['Content-Type']);
$('#direct_s3_profile_video_form').find('input[name="X-Amz-Credential"]').val(response_json_data.data.fields['x-amz-credential']);
$('#direct_s3_profile_video_form').find('input[name="X-Amz-Algorithm"]').val(response_json_data.data.fields['x-amz-algorithm']);
$('#direct_s3_profile_video_form').find('input[name="X-Amz-Date"]').val(response_json_data.data.fields['x-amz-date']);
$('#direct_s3_profile_video_form').find('input[name="Policy"]').val(response_json_data.data.fields['policy']);
$('#direct_s3_profile_video_form').find('input[name="X-Amz-Signature"]').val(response_json_data.data.fields['x-amz-signature']);  
$('#direct_s3_profile_video_form').attr('action', 'https://mybucket.s3.amazonaws.com');

相关问题 更多 >