我创建了一个示例无服务器框架,它具有在s3上上传文件的功能,但是我在上传文件时遇到问题(文件正在上传,但事实证明它已损坏)。 请参考下面的节点js代码,其中我使用无服务器框架在s3上为文件上传创建了路由,控制器和服务
//Router
app.post('/api/fileUpload', controller.fileUpload);
//Controller
exports.fileUpload = (req, res) => {
let params = {
Bucket: 'BucketName',
Key: 'keyofAWSS3',
ContentType: 'image/jpeg',
Body: data,
ACL: 'private'
};
//to create file on S3
return s3Service.CreateToS3(params).then((fromResolve) => {
console.log(fromResolve);
}).catch((error) => {
console.log(error);
});
}
//Service
exports.CreateToS3 = (params) => {
let s3 = new AWS.S3();
return new Promise((resolve, reject) => {
//Upload as a file to S3
s3.putObject(params, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
};
以下是serverless.yml设置 服务:lamda功能的服务名称
provider:
name: aws
runtime: nodejs8.10
timeout: 90
stage: production
memorySize: 128
deploymentBucket:
name: bucketname
functions:
funciton-name-of-lambda:
handler: index.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'