I am trying to get an image from S3 and I can't seem to get back the image even though I think I am giving it the right information.
Currently it returns a null value after a few seconds and it makes the page seem like it is broken / server isn't responding for 5 seconds.
Route
// GET TEST
app.get('/test',
isAuthenticated,
(req, res) => {
var image;
var params = {
Bucket: "<bucket name>",
Key: "<tried both the Etag and the file name both failed>"
};
s3.getObject(params, (err, data) => {
image = data;
console.log('Data: ',data);
res.render('test/test', {header: 'TEST', image: image});
});
console.log('Image: ', image);
});
I know from reading the docs that getObject()
is what it seems like I need but actually getting it to work has proven difficult.
AWS config
var aws = require("aws-sdk"),
s3 = new aws.S3();
var config = require('../config/config');
aws.config.update({
secretAccessKey: config.aws_s3.secretAccessKey,
accessKeyId: config.aws_s3.accessKeyId,
region: config.aws_s3.region
});
This has been taken from a known working part of my code base.
What am I missing that the file is not downloaded? I also would love it if the file was sent directly to the client and not to my server then the client.
EDIT
I have gotten it to give me back some data but still will not display the image
Data
Data: { AcceptRanges: 'bytes',
LastModified: 2018-09-22T01:53:39.000Z,
ContentLength: 2117,
ETag: '"<Etag>"',
ContentType: 'application/octet-stream',
ServerSideEncryption: 'AES256',
Metadata: {},
Body: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 30 00 00 00 30 08 06 00 00 00 57 02 f9 87 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 04 ... > }