我正在使用 jimp 插件來讀取檔案并嘗試優化影像。我可以使用公共影像 url 讀取檔案和優化。
問題出在從 s3 存盤桶中讀取影像時。我可以從 s3 存盤桶中獲取影像,同時將影像jimp.read(buffer)作為緩沖區傳遞,它不起作用并且也無法得到任何錯誤。
這是我的代碼:
module.exports.handler = async (event, context, callback) => {
// Get the object from the event and show its content type
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\ /g, " "));
const params = {
Bucket: bucket,
Key: key,
};
console.log("params ::::::::::", params);
try {
let objectId = uuidv4();
let objectKey = `resize-${width}x${height}-${objectId}.jpg`;
const origimage = await s3.getObject(params).promise();
console.log("origimage ====>", origimage.Body);
Jimp.read(origimage.Body)
.then(image => image.resize(width, height)
.getBufferAsync(imageType))
.then(resizedBuffer => uploadToS3(resizedBuffer, objectKey))
.then(function(response) {
console.log(`Image ${objectKey} was uploaed and resized`);
callback(null, {
statusCode: 200,
body: JSON.stringify(response)
});
})
.catch(error => console.log(error));
} catch (err) {
console.log(err);
const message = `Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.`;
console.log(message);
throw new Error(message);
}
這是我從 s3 存盤桶得到的回應:
2022-10-13T09:19:25.099Z 924b89b2-69ab-467e-8143-6f9e22e5e67a INFO CONTENT DATA: {
AcceptRanges: 'bytes',
LastModified: 2022-10-12T11:58:38.000Z,
ContentLength: 331338,
ETag: '"917eba20c634253a97ff6dfe3958db0a"',
ContentType: 'image/jpeg',
Metadata: {},
Body: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 03 02 02 02 02 02 03 02 02 02 03 03 03 03 04 06 04 04 04 04 04 08 06 06 05 ... 331288 more bytes>
}
在發送緩沖區資料以發揮作用時,我是否缺少任何東西jimp.read。我也嘗試過傳遞 imagename 和 base64,但仍然沒有運氣。
無論如何我可以使用帶有 lambda 功能的影像 url 訪問 s3 存盤桶影像嗎?
提前致謝
uj5u.com熱心網友回復:
看起來您并沒有在等待Jimp.read(buffer)創建解決/拒絕的承諾。
嘗試一下await Jimp.read(...),您應該會以解決/拒絕承諾的方式獲得一些回報,從而觸發您的一些日志記錄。
同樣,您也可以這樣做return Jimp.read(...),Lambda 將在結束執行之前等待該承諾完成。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/515192.html
標籤:节点.js亚马逊网络服务亚马逊-s3aws-lambda吉普
