gpt4 book ai didi

node.js - 如何使用nodejs将缓冲区转换为图像?

转载 作者:行者123 更新时间:2023-12-05 02:06:34 25 4
gpt4 key购买 nike

这是我的代码,我从 S3 存储桶中获取图像,但结果在缓冲区中,我需要它作为图像。请告诉我这样做

const AWS = require('aws-sdk')
const fs = require('fs')
const express = require('express')
const app = express()
app.get('/', (req, res) => {
const s3 = new AWS.S3({
accessKeyId: '################',
secretAccessKey: '###############################',
region: '###########'
})

const params = {
Bucket: '#########',
Key: '########'
}
s3.getObject(params, (err, rest) => {
res.send(rest)
})
})

app.listen(3000)

输出

{
AcceptRanges: 'bytes',
LastModified: 2020-06-29T10:42:17.000Z,
ContentLength: 338844,
ETag: '"769af0d798cd0486993711bc63340bd0"',
ContentType: 'application/octet-stream',
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 84 00 02 02 02
02 02 02 02 02 02 02 03 03 02 03 03 04 03 03 03 03 04 06 04 04 04 ... 338794
more bytes>
}

最佳答案

TLDR;

/**
* FINAL/WORKING SOLUTION
*/

// ...
app.get('/', (req, res) => {
const s3 = new AWS.S3({
accessKeyId: '-',
secretAccessKey: '-',
region: '-',
});

const params = {
Bucket: '-',
Key: '-',
};

s3.getObject(params, (err, rest) => {
if (err) throw err;

const b64 = Buffer.from(rest.Body).toString('base64');
// CHANGE THIS IF THE IMAGE YOU ARE WORKING WITH IS .jpg OR WHATEVER
const mimeType = 'image/png'; // e.g., image/png

res.send(`<img src="data:${mimeType};base64,${b64}" />`);
});
});
// ...

原始答案

您将需要使用 node “渲染”图像.. 也就是将响应作为 <img /> 发送标签..

// ...
app.get('/', (req, res) => {
const s3 = new AWS.S3({
accessKeyId: '-',
secretAccessKey: '-',
region: '-',
});

const params = {
Bucket: '-',
Key: '-',
};

s3.getObject(params, (err, rest) => {
if (err) throw err;

const b64 = Buffer.from(rest).toString('base64');
// NOTE:
// Because 'rest' appears to be a buffer, you might not
// need to do `Buffer.from(...)`,you may have to do something like:
/** const b64 = rest.toString('base64'); **/
const mimeType = 'image/png'; // e.g., image/png

res.send(`<img src="data:${mimeType};base64,${b64}" />`);
});
});
// ...

编辑 1:

我认为这个问题与数据从 S3 返回给您的方式有关。您可能必须执行类似 rest.Body 的操作。或 rest.body - 我不确定 S3 中的对象是什么样的。

话虽如此,我保存了this image本地为 sample.png ,并使用下面的代码加载它 - 它工作得很好。这证明问题与 S3 如何将数据返回给您有关。

const fs = require('fs');
const express = require('express');
const app = express();

app.get('/', (req, res) => {
// Simulate getting data from S3 (aka data as buffer)
const rest = Buffer.from(fs.readFileSync('./sample.png'));

console.log('typeof rest = ' + typeof rest + '\r\n\r\n', rest);
// typeof rest = object
//
// <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 34 00 00 00 a4 08 03 00 00 00 2e 4e 22 5a 00 00 00 a2 50 4c 54 45 f7 f7 f7 cd 00 00 d1 00 00 ... 6360 more bytes>


const b64 = rest.toString('base64');
const mimeType = 'image/png'; // e.g., image/png

res.send(`<img src="data:${mimeType};base64,${b64}" />`);
});

app.listen(3000);

编辑 2:

看起来您需要这样做:

// ...
app.get('/', (req, res) => {
const s3 = new AWS.S3({
accessKeyId: '-',
secretAccessKey: '-',
region: '-',
});

const params = {
Bucket: '-',
Key: '-',
};

s3.getObject(params, (err, rest) => {
if (err) throw err;

const b64 = Buffer.from(rest.Body).toString('base64');
// IF THE ABOVE LINE DOES NOT WORK, TRY THIS:
// const b64 = rest.Body.toString('base64');

// CHANGE THIS IF THE IMAGE YOU ARE WORKING WITH IS .jpg OR WHATEVER
const mimeType = 'image/png'; // e.g., image/png

res.send(`<img src="data:${mimeType};base64,${b64}" />`);
});
});
// ...

关于node.js - 如何使用nodejs将缓冲区转换为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62708802/

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