【问题标题】:Download json file with axios via s3 presigned_url通过 s3 presigned_url 用 axios 下载 json 文件
【发布时间】:2021-07-03 17:11:13
【问题描述】:

场景很简单。 lambda aws 发送的 json 超出了 10MB 的负载限制。 使用 presigned_url 返回这个巨大的 json 并通过 axios 获取响应是一种好习惯吗?

很遗憾,我收到了这个错误。我收到的链接是有效的,我可以在我的浏览器上显示 json。

Access to XMLHttpRequest at 'https://my-perfect-bucket.s3.amazonaws.com/my-super-key?AWSAccessKeyId=XXX&Signature=XXX&x-amz-security-token=XXX&Expires=XXX' 
from origin 'http://localhost:3000' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

我应该在我的 axios 方法中添加一个参数吗?

我的拉姆达

import json, boto3

def lambda_handler(event, context):
    body = json.loads(event['body'])
    key = body["key"]
    s3 = boto3.client('s3')
    response = s3.generate_presigned_url('get_object',
                                         Params={'Bucket': "my-perfect-bucket",
                                                 'Key': key},                     # json file 
                                         ExpiresIn=300,
                                         HttpMethod='GET')
    return {
        'statusCode': 200,
        'body': json.dumps(response, default=str)
    }

我的 axios 方法:

this.$axios
    .post(`/my-cool-api/get`, {
        key: this.key
    })
    .then(r => {
        this.$axios
            .get(r.data)
            .then(json => {
                this.mySuperData = json.data
            })
    })

【问题讨论】:

  • 您是否在该 s3 存储桶上设置了 cors 策略?

标签: amazon-web-services amazon-s3 axios


【解决方案1】:
[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
    }
]

将此添加到您的 s3 存储桶 -> 权限 -> cors

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    相关资源
    最近更新 更多