0

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 ... > }
Josh Kirby
  • 11
  • 7
  • try review accepted ans here : https://stackoverflow.com/questions/32702431/display-images-fetched-from-s3 is about std img retreive from S3 for typical use on a client page. Or u can 'fetch' the s3 file explicitly and go to img.src from there – Robert Rowntree Sep 22 '18 at 15:58
  • @RobertRowntree Thanks for that post. While it seems to work if I do it in plain JS (which exposes my API keys) I can't seem to make it work in node.js and passing through the data I need for the image. – Josh Kirby Sep 22 '18 at 17:48

2 Answers2

0

I finally resolved the issue, it took a fairly different method of getting it compared to what I had been trying and what was linked. Much simpler than the linked method.

The method that worked was getSignedUrl which used the getOjbect function that I was trying to use before.

My route

// GET TEST
    app.get('/test',
    isAuthenticated,
    (req, res) => {
        var urlParams = {Bucket: config.aws_s3.logoBucket, Key: <key>'};
        logoBucket.getSignedUrl('getObject', urlParams, function(err, url){
            res.render('test/test', {header: 'TEST', url: url});
        });
    });

Config

var aws = require("aws-sdk");
var config = require('../config/config');
aws.config.update({
   secretAccessKey: config.aws_s3.secretAccessKey,
   accessKeyId: config.aws_s3.accessKeyId,
   region: config.aws_s3.region,
  sslEnabled: true
});
var s3 = new aws.S3();
var logoBucket = new aws.S3( { params: {Bucket: config.aws_s3.logoBucket} } )
Josh Kirby
  • 11
  • 7
0

Try this:

First, configure the profile with credential

Create a credentials file or edit if exist at
~/.aws/credentials on Mac/Linux or
C:\Users\.aws\credentials on Windows

[/*Give Name to your profile*/]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key

Change the code with this

var AWS = require('aws-sdk');
AWS.config.region = '<Your region>';
var credentials = new AWS.SharedIniFileCredentials({profile: '<Your profile>'});
AWS.config.credentials = credentials;
var s3 = new aws.S3();

var params = {
  Bucket: "<Your bucket name>", 
  Key: "<file name to retrieve>"
 };


app.get('/test', isAuthenticated, function(req, res) {
  s3.getObject(params, function(err, data) {
    var image = data;
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
    res.render('test/test', {header: 'TEST', image: image});
  }
}