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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

Make 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