admin管理员组文章数量:1297097
On my NodeJS server I download an image that I need to embed in an email. My bucket is NOT public so just using the link will not work, as is not the solution I want for the requirements of either this question or the project.
I'm using a HTML email for this with something like:
<p>Embedded image: <img src="data:image/jpeg;charset=utf-8;base64,{{url}}" /></p>
So I download from S3
s3.getObject(
{ Bucket: "mybucket", Key: "mykey" },
function (error, data) {
if (error != null) {
console.log("Errror" + error)
} else {
console.log("Loaded " + data.ContentLength + " bytes")
and then I'm trying to convert data.body to UTF-8 base 64
I thought something like
"data:image/png;base64," + new String(encoder.encode(data.body), "UTF-8")
But it doesn't seem to work, and I'm struggling to define encoder to be able to do this.
On my NodeJS server I download an image that I need to embed in an email. My bucket is NOT public so just using the link will not work, as is not the solution I want for the requirements of either this question or the project.
I'm using a HTML email for this with something like:
<p>Embedded image: <img src="data:image/jpeg;charset=utf-8;base64,{{url}}" /></p>
So I download from S3
s3.getObject(
{ Bucket: "mybucket", Key: "mykey" },
function (error, data) {
if (error != null) {
console.log("Errror" + error)
} else {
console.log("Loaded " + data.ContentLength + " bytes")
and then I'm trying to convert data.body to UTF-8 base 64
I thought something like
"data:image/png;base64," + new String(encoder.encode(data.body), "UTF-8")
But it doesn't seem to work, and I'm struggling to define encoder to be able to do this.
Share Improve this question asked Sep 13, 2019 at 8:04 WishIHadThreeGunsWishIHadThreeGuns 1,4793 gold badges22 silver badges46 bronze badges2 Answers
Reset to default 8Make sure you are getting image data in data.Body
. I think data.Body
already a buffer and you can construct src URL as bellow:
// Convert Body from a Buffer to a String
let base64String= data.Body.toString('base64');
let src = "data:image/jpeg;base64,"+base64String;
if you store s3 bucket object in binary format, to received it and convert it to base64 can be done:
const blobToBase64 = (blob) => {
const reader = new FileReader()
reader.readAsDataURL(blob)
return new Promise((rs, rj) => {
reader.onloadend = () => {
rs(reader.result)
}
reader.onerror = rj
})
}
function useImage({s3Link}) {
const [src, setSrc] = React.useState(null)
React.useEffect(() => {
async function query({link}) {
//link is link to s3 bucket URL link e.g
// const link = s3.getSignedUrl('getObject', {
// Bucket: bucketnamw,
// Key: key,
// Expires: 30,
// })
const r = await fetch(link)
const blob = await r.blob()
const base64 = await blobToBase64(blob)
console.log(`base64!`, base64)
setSrc(base64)
}
if (s3Link) {
query({link: s3Link})
}
}, [s3Link, setSrc])
return [{src}]
}
本文标签: javascriptConvert S3 byte array to base64Stack Overflow
版权声明:本文标题:javascript - Convert S3 byte array to base64 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741643012a2390026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论