我曾尝试使用AWS lambda API网关在s3桶中上传图像和视频
成功上传图像并获得位置也在响应,但我无法预览它
const multipart = require('aws-lambda-multipart-parser');
const AWS = require('aws-sdk');
const { config } = require('./config')
const { v4: uuidv4 } = require('uuid')
const fs = require('fs');
const s3 = new AWS.S3({
accessKeyId:config?.dev?.aws_s3?.ACCESS_KEY,
secretAccessKey:config?.dev?.aws_s3?.SECRET_KEY
})
//After the parse data from the event. Event declared as a param
const result = await multipart.parse(param);
let myFile= result.media.filename.split(".")
let fileType = myFile[myFile.length - 1];
const fileName = uuidv4()+'.'+fileType;
const uploadData = {
Bucket : config?.dev?.aws_s3?.BUCKETNAME,
Key : fileName,
Body : Buffer.from(result.media.content, 'binary'),
ContentType : result.media.contentType,
ACL : 'public-read',
Conditions: [
['content-length-range', 0, 10000000] // 10 Mb
]
}
try {
const responseData = await s3.upload(uploadData).promise()
return {
"body" : {
"message" : "Image uploaded successfully",
"data" : responseData
},
'statusCode': 200
}
} catch (e) {
return {
"body" : {
"message" : e.message
},
'statusCode': 500
}
}
//上传数据到s3
//响应
{
"message": "Image uploaded successfully",
"data": {
"ETag": "\"662d32878c3e0628d2958e95fc055855\"",
"Location": "https://bucketname.s3.amazonaws.com/03c2fcd1-ab92-42a8-93cf-d6a89545c77e.jpeg",
"key": "03c2fcd1-ab92-42a8-93cf-d6a89545c77e.jpeg",
"Key": "03c2fcd1-ab92-42a8-93cf-d6a89545c77e.jpeg",
"Bucket": "bucketname"
}
}
该问题是图像未加载。您可以帮助任何修复此问题,并应支持它的视频和音频
错误:图像无法显示,因为它包含错误
1条答案
按热度按时间6ju8rftf1#
我的Lambda NodeJS函数也有类似的问题。使用fetch()从的click handler将文件作为FormData推到前端网页
NodeJS Lambda函数解析了数据,存储到S3,但它是无效的图像文件。
我在其他一些SO帖子中找到了修复方法,导致我做了两个AWS Jmeter 板的更改,使所有的差异,并让我工作:
希望这对你有帮助。