我已按照本文的说明操作,它可以作为魅力: https://aws.amazon.com/tw/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/
而不是文章中调整大小的图像路径格式:http://YOUR_BUCKET_WEBSITE_HOSTNAME_HERE/300x300/blue_marble.jpg
我希望它是: http://YOUR_BUCKET_WEBSITE_HOSTNAME_HERE/blue_marble_300.jpg
所以我修改了index.js并上传了新函数,如下所示:
'use strict';
const AWS = require('aws-sdk');
const S3 = new AWS.S3({
signatureVersion: 'v4',
});
const Sharp = require('sharp');
const BUCKET = process.env.BUCKET;
const URL = process.env.URL;
exports.handler = function(event, context, callback) {
const key = event.queryStringParameters.key;
const match = key.match(/([A-Za-z0-9/]*)((_\d+)?.([A-Za-z0-9/]*))$/);
const size = match[2].split('.')[0].replace("_", "");
let originalKey = key.replace("_" + size, "");
console.log(originalKey);
console.log(size);
S3.getObject({ Bucket: BUCKET, Key: originalKey }).promise()
.then(data => Sharp(data.Body)
.resize(size, size)
.toFormat('png')
.toBuffer()
)
.then(buffer => S3.putObject({
Body: buffer,
Bucket: BUCKET,
ContentType: 'image/png',
Key: key,
}).promise())
.then(() => callback(null, {
statusCode: '301',
headers: { 'location': `${URL}/${key}` },
body: '',
}))
.catch(err => callback(err));
}
但如果失败则显示错误消息:
{"message": "Internal server error"}
我应该如何实现这个并进行调试?顺便说一下,我检查了它显示的云监视日志
Syntax error in module 'index': SyntaxError
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)