AWS Lambda 调整大小中未定义尖锐错误
sharp is not defined error in AWS Lambda resize
在我尝试添加 overlayWith 之前,下面的代码工作正常。叠加图像为 png,小于调整后的图像。我使用的Lambda函数包来自https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/
'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;
var match = key.match(/(\_[xsm][smd]).(jpg)/);
var version = (match[1]);
console.log("Match 1: " + (match[1]));
console.log("Match 2: " + (match[2]));
var width = 400;
// IF version = "_md" then 1000 elseif _xs then 100 else 400
if (version == "_md") {
width = 1000;
} else if (version=="_xs") {
width = 100;
}
console.log("key: " + event.queryStringParameters.key);
var originalKey = key.replace(/\_[xsm][smd]/g, "");
var newKey = "resized/" + key;
console.log("OriginalKey: " + originalKey)
console.log("Bucket: " + URL)
console.log("NewKey: " + newKey)
S3.getObject({Bucket: BUCKET, Key: originalKey}).promise()
.then(data => Sharp(data.Body)
// .resize(width, height)
.resize(width, width)
.max()
.overlayWith('s3-us-west-2.amazonaws.com/s.cdpn.io/someimage.png', { gravity: sharp.gravity.northeast } )
.toFormat('jpeg')
.toBuffer()
)
.then(buffer => S3.putObject({
Body: buffer,
Bucket: BUCKET,
ContentType: 'image/jpeg',
Key: newKey, // newKey
}).promise()
)
.then(() => callback(null, {
statusCode: '302',
headers: {'location': `${URL}/${newKey}`},
body: '',
})
)
.catch(err => callback(err))
}
这是日志错误
{
"errorMessage": "sharp is not defined",
"errorType": "ReferenceError",
"stackTrace": [
"S3.getObject.promise.then.data (/var/task/index.js:42:92)",
"process._tickDomainCallback (internal/process/next_tick.js:135:7)"
]
}
示例代码来自 http://sharp.dimens.io/en/stable/api-composite/
sharp('input.png')
.rotate(180)
.resize(300)
.flatten()
.background('#ff6600')
.overlayWith('overlay.png', { gravity: sharp.gravity.southeast } )
.sharpen()
.withMetadata()
.quality(90)
.webp()
.toBuffer()
.then(function(outputBuffer) {
// outputBuffer contains upside down, 300px wide, alpha channel flattened
// onto orange background, composited with overlay.png with SE gravity,
// sharpened, with metadata, 90% quality WebP image data. Phew!
});
当你 require
锐化时,你将它保存在一个大写 'S' 的变量 Sharp
中。您可以在代码中以这种方式使用它,sharp.gravity.northeast
除外。这就是它生成错误的原因 — 当您定义了 Sharp
时,您还没有定义 sharp
。您应该能够通过将错误更改为 Sharp.gravity.northeast
来消除错误,但我认为更好的主意是将要求更改为:
const sharp = require('sharp');
因此您的代码将与文档匹配。这意味着您需要更改代码中用大写 'S' 调用它的地方。例如 Sharp(data.Body)
需要变成 sharp(data.Body)
.
在我尝试添加 overlayWith 之前,下面的代码工作正常。叠加图像为 png,小于调整后的图像。我使用的Lambda函数包来自https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/
'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;
var match = key.match(/(\_[xsm][smd]).(jpg)/);
var version = (match[1]);
console.log("Match 1: " + (match[1]));
console.log("Match 2: " + (match[2]));
var width = 400;
// IF version = "_md" then 1000 elseif _xs then 100 else 400
if (version == "_md") {
width = 1000;
} else if (version=="_xs") {
width = 100;
}
console.log("key: " + event.queryStringParameters.key);
var originalKey = key.replace(/\_[xsm][smd]/g, "");
var newKey = "resized/" + key;
console.log("OriginalKey: " + originalKey)
console.log("Bucket: " + URL)
console.log("NewKey: " + newKey)
S3.getObject({Bucket: BUCKET, Key: originalKey}).promise()
.then(data => Sharp(data.Body)
// .resize(width, height)
.resize(width, width)
.max()
.overlayWith('s3-us-west-2.amazonaws.com/s.cdpn.io/someimage.png', { gravity: sharp.gravity.northeast } )
.toFormat('jpeg')
.toBuffer()
)
.then(buffer => S3.putObject({
Body: buffer,
Bucket: BUCKET,
ContentType: 'image/jpeg',
Key: newKey, // newKey
}).promise()
)
.then(() => callback(null, {
statusCode: '302',
headers: {'location': `${URL}/${newKey}`},
body: '',
})
)
.catch(err => callback(err))
}
这是日志错误
{
"errorMessage": "sharp is not defined",
"errorType": "ReferenceError",
"stackTrace": [
"S3.getObject.promise.then.data (/var/task/index.js:42:92)",
"process._tickDomainCallback (internal/process/next_tick.js:135:7)"
]
}
示例代码来自 http://sharp.dimens.io/en/stable/api-composite/
sharp('input.png')
.rotate(180)
.resize(300)
.flatten()
.background('#ff6600')
.overlayWith('overlay.png', { gravity: sharp.gravity.southeast } )
.sharpen()
.withMetadata()
.quality(90)
.webp()
.toBuffer()
.then(function(outputBuffer) {
// outputBuffer contains upside down, 300px wide, alpha channel flattened
// onto orange background, composited with overlay.png with SE gravity,
// sharpened, with metadata, 90% quality WebP image data. Phew!
});
当你 require
锐化时,你将它保存在一个大写 'S' 的变量 Sharp
中。您可以在代码中以这种方式使用它,sharp.gravity.northeast
除外。这就是它生成错误的原因 — 当您定义了 Sharp
时,您还没有定义 sharp
。您应该能够通过将错误更改为 Sharp.gravity.northeast
来消除错误,但我认为更好的主意是将要求更改为:
const sharp = require('sharp');
因此您的代码将与文档匹配。这意味着您需要更改代码中用大写 'S' 调用它的地方。例如 Sharp(data.Body)
需要变成 sharp(data.Body)
.