0

I have images stored in an s3 bucket, these are private photos.

Currently I can get a an image url via:

getSignedUrl('getObject', params, callback);

Then I embed the url into an images src='' string (express, pug). However, I get invalid request errors and no image. How do I go about properly getting the image urls and embedding them into the html?

index.js

app.get('/', (req, res) => {
    s3.getSignedUrl('getObject', {
        Bucket: Bucket,
        Key: Key
    }, (err, url) => {
        res.render('index', {
            img: url
        })
    })
})

index.pug

html
    head
        title example
    body
        img(src="" + img)

I am just learning AWS SDK.

How do I embed an s3 image URL into img src from nodejs?

Drew
  • 1,171
  • 4
  • 21
  • 36

1 Answers1

1

I figured out a solution. Rather than try and embed the actual url to the s3 bucket and object into the pug render. I have a separate get for the image, that returns the byte stream itself:

app.get('/img.jpg', (req, res) => {
    s3.getObject({...}, (err, data) => {
        ...
        res.send(data.Body)
    })
})
Drew
  • 1,171
  • 4
  • 21
  • 36