gpt4 book ai didi

node.js - 从 URL 获取图像到 S3

转载 作者:太空宇宙 更新时间:2023-11-03 22:19:54 26 4
gpt4 key购买 nike

目标是让 AWS lambda 每天将特定的 jpg 从 URL 复制到 S3 存储桶。这看起来是一个 super 简单的 lambda 函数,但它就是不起作用。我将此设置为本地 NPM 项目,然后将压缩文件上传到 AWS lambda 控制台。下面的代码运行没有错误,但不复制图像。任何帮助将不胜感激。

"use strict";

const AWS = require("aws-sdk");
const Jimp = require("jimp");
const s3 = new AWS.S3();
const imageType = "image/jpeg";
const bucket = 'mybucket';

exports.handler = (event, context, callback) => {
let objectKey = 'myimage.jpg';

Jimp.read('sampleURL.com/image.jpg')
.then(image => {
s3.putObject({
Bucket: bucket,
Key: objectKey,
Body: image,
ContentType: imageType
})
})
.catch(err => {
// Handle an exception.
});


};

AWS CloudWatch 日志

2019-10-24T12:48:23.105Z    bac7d80e-5544-4ea5-ae12-478281338389    INFO    { Error: Could not find MIME for Buffer <null>
at Jimp.parseBitmap (/var/task/node_modules/@jimp/core/dist/utils/image-bitmap.js:120:15)
at Jimp.parseBitmap (/var/task/node_modules/@jimp/core/dist/index.js:506:32)
at /var/task/node_modules/@jimp/core/dist/index.js:448:15
at /var/task/node_modules/@jimp/core/dist/index.js:176:14
at /var/task/node_modules/@jimp/core/dist/request.js:66:9
at IncomingMessage.<anonymous> (/var/task/node_modules/phin/lib/phin.compiled.js:1:2100)
at IncomingMessage.emit (events.js:203:15)
at IncomingMessage.EventEmitter.emit (domain.js:448:20)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19) methodName: 'constructor' }
END RequestId: bac7d80e-5544-4ea5-ae12-478281338389
REPORT RequestId: bac7d80e-5544-4ea5-ae12-478281338389 Duration: 612.63 ms Billed Duration: 700 ms Memory Size: 128 MB Max Memory Used: 97 MB Init Duration: 557.69 ms

最佳答案

如果这对其他人有帮助,则需要将图像写入缓冲区。以下行修复了它:

const buffer = await image.getBufferAsync(imageType);

然后缓冲区用于 S3 Body 参数。所以完整的脚本是:


"use strict";

const AWS = require("aws-sdk");
const Jimp = require("jimp");
const s3 = new AWS.S3();
const imageType = "image/jpeg";
const bucket = 'bucketxzy';

exports.handler = async (event, context) => {
let objectKey = 'sampleimage.jpeg';
const image = await Jimp.read('https://www.sampleurl.com/sampleimage.jpg/');
const buffer = await image.getBufferAsync(imageType);

return s3.putObject({
Bucket: bucket,
Key: objectKey,
Body: buffer,
ContentType: imageType
}).promise();


};

关于node.js - 从 URL 获取图像到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58530146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com