python - 使用 Python Boto3+DjangoRest+React Redux Saga 对 URL 进行签名以上传对象

标签 python reactjs redux boto3 saga

我正在尝试使用 boto3 签署一个网址,如下所示:

 @list_route(methods=['get'])
    def sign(self, request):
        key = request.GET.get('key', None)
        if key:
           if key.count('.') == 1 and key.count('/') == 1:
               extension = key.split('.')
               if '*' in settings.ALLOWED_FILE_TYPES or extension.lower() in settings.ALLOWED_FILE_TYPES:
                   # Get the service client
                   s3 = boto3.client('s3')
                   # Generate the POST attributes
                   post = s3.generate_presigned_url(
                       'put_object',
                       {'Bucket': settings.FILE_BUCKET, 'Key': 'tmp/{}'.format(key)},
                       300,
                       'PUT'
                   )
                   return Response(
                       status=200,
                       data={'presigned_post': post}
                   )
               else:
                   return Response(
                       status=400,
                       data="File Type not Allowed"
                   )
           else:
               return Response(
                   status=400,
                   data="Invalid Key"
               )
        else:
            return Response(
                status=400,
                data='Missing Key'
            )

我可以生成 URL,然后发出以下请求以在 React 上的 Saga 上上传文件:

    // Generate Signed URL
    const resultSignUrl = yield call([File.custom, 'sign'], {token, params:{key: data.key}});
    let signedUrl = resultSignUrl.presigned_post;
    let options = {
        headers: {
            'Content-Type': file.type
        }
    };
    console.log("SIGN URL", resultSignUrl);
    console.log("SIGN URL file", file);

    // Upload to S3
    const resultUpload = yield call([axios, 'put'], signedUrl, file, options);
    console.log("S3 UPLOAD", resultUpload);

问题是,当我发出请求时,AWS 会回复此内容:

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>"SomeAccessKey"</AWSAccessKeyId><StringToSign>PUT

application/vnd.oasis.opendocument.text
1516318279
/student-tracker-files/tmp/14a09716-8850-47cb-bf3a-7820f42c97d4/test%20honduras.odt</StringToSign><SignatureProvided>EFqdML8OgILC/L/hoJBqmyhuzRk=</SignatureProvided><StringToSignBytes>50 55 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 76 6e 64 2e 6f 61 73 69 73 2e 6f 70 65 6e 64 6f 63 75 6d 65 6e 74 2e 74 65 78 74 0a 31 35 31 36 33 31 38 32 37 39 0a 2f 73 74 75 64 65 6e 74 2d 74 72 61 63 6b 65 72 2d 66 69 6c 65 73 2f 74 6d 70 2f 31 34 61 30 39 37 31 36 2d 38 38 35 30 2d 34 37 63 62 2d 62 66 33 61 2d 37 38 32 30 66 34 32 63 39 37 64 34 2f 74 65 73 74 25 32 30 68 6f 6e 64 75 72 61 73 2e 6f 64 74</StringToSignBytes><RequestId>193F6C165445A3E4</RequestId><HostId>odoIo4+4sdVUuDJucnrkzeyJJUYJ5sMDqpS7DAzV3nJvSrN4Eu+7b1Srs2FEQy2aDHuYEakMBGY=</HostId></Error>

谁能告诉我我可能做错了什么?我已经更改了访问 key 和 secret ,但仍然得到相同的响应。我已阅读 boto3 上的文档,但我发现我的代码与他们在示例中显示的代码没有区别。

最佳答案

您应该尝试将内容类型添加到 django boto 函数

ost = s3.generate_presigned_url(
         'put_object',
         {'Bucket': settings.FILE_BUCKET,
         'ContentType': request.GET.get('type'),
         'Key': 'tmp/{}'.format(key)},
         3600,
         'PUT'
      )

关于python - 使用 Python Boto3+DjangoRest+React Redux Saga 对 URL 进行签名以上传对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48331969/

相关文章:

获取 pandas 数据框中元素的序列相关性的 Pythonic 方法

reactjs - 使用 redux 命名导出?

javascript - 在同构应用程序上 react 路由器 redux 初始状态设置

reactjs - 在 Google Kubernetes 上设置 react app build 文件夹

javascript - 如何在MouseMove事件上冒泡

javascript - 在 React Redux 应用程序中使用 JWT 身份验证 token 的正确方法

javascript - 如何在 redux 操作中处理 XMLHttpRequest?

python - 动态重命名方法

python - 如何让任务栏在 PyQt4 中显示我的进度条?

python - Numpy 整数 nan