Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 - How to stop images from force downloading instead of displaying.

I am currently trying to load images from my website to AWS S3. I have the functionality working where it uploads the image to the server but when i try to view the images they download instead of displaying. I read there is a way to set the file type so this would not happen. I am not sure how to do that. Any help would be great.

                router.post('/heroes/createNewHeroes', function(req,res) {
                    var formidable = require('formidable'),
                    http = require('http'),
                    util = require('util');

                    var form = new formidable.IncomingForm();

                    form.parse(req, function(err, fields, files) {
                        console.log(fields);
                        console.log(files);

                        // Load the AWS SDK for Node.js
                        var AWS = require('aws-sdk');
                        var shortid = require('shortid');
                        var fs = require('fs');
                        var fileStream = fs.createReadStream(files.asset.path);
                        var newFilename = shortid.generate()+"_"+files.asset.name;

                        // Set your region for future requests.
                        AWS.config.region = 'us-west-2';
                        AWS.config.accessKeyId = 'access Key';
                        AWS.config.secretAccessKey = 'secret Key';

                        console.log(newFilename);

                        fileStream.on('error', function (err) {
                          if (err) { throw err; }
                        });

                        fileStream.on('open', function () {
                            var s3bucket = new AWS.S3({params: {Bucket: ' '}});
                            s3bucket.createBucket(function() {
                              var params = {Key: newFilename, Body: fileStream};
                              s3bucket.upload(params, function(err, data) {
                                if (err) {
                                    console.log("Error uploading data: ", err);
                                } else {
                                    console.log("Successfully uploaded data");

                                    projectX.createHeroes(['plantTypes', 'asset', 'cost', 'energy', 'isSunProducer', 'isShooter', 'isExploding', 'sunFrequency', 'shootingFrequency', 'damage'], [fields.plantTypes, newFilename, fields.cost, fields.energy, fields.isSunProducer, fields.isShooter, fields.isExploding, fields.sunFrequency, fields.shootingFrequency, fields.damage], function(data){
                                        res.redirect('/heroes')
                                    });
                                }
                              });
                            });

                        });

                    });
                });
like image 527
Jason Martocci Avatar asked Jun 19 '16 01:06

Jason Martocci


1 Answers

var params = {Key: newFilename, ContentType: 'image/png', Body: fileStream};

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property

like image 180
Michael - sqlbot Avatar answered Nov 15 '22 05:11

Michael - sqlbot