通过 s3 presigned_url 使用 axios 下载 json 文件 [英] Download json file with axios via s3 presigned_url
本文介绍了通过 s3 presigned_url 使用 axios 下载 json 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
场景很简单.lambda aws 发送的 json 超过了 10MB 的负载限制.使用 presigned_url 返回这个巨大的 json 并通过 axios 获取响应是一个好习惯吗?
The scenario is simple. The json sent by the lambda aws exceeds the 10MB payload limit. Is it a good practice to use a presigned_url to return this huge json and get the response by axios?
很遗憾,我收到此错误.我收到的链接是有效的,我可以在浏览器上显示json.
Unfortunately I receive this error. The link I received is valid, I can display the json on my browser.
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 方法添加一个参数吗?
Should I add a parameter to my axios method?
我的拉姆达
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 方法:
My axios method:
this.$axios
.post(`/my-cool-api/get`, {
key: this.key
})
.then(r => {
this.$axios
.get(r.data)
.then(json => {
this.mySuperData = json.data
})
})
推荐答案
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"HEAD"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": [],
"MaxAgeSeconds": 3000
}
]
将此添加到您的 s3 存储桶中 ->权限 ->科斯
add this to your s3 bucket -> permissions -> cors
这篇关于通过 s3 presigned_url 使用 axios 下载 json 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文