samedi 29 août 2015

Trouble with amazon s3 uploads

I've managed to get file uploads working, but I can't seem to get the file names to be what I want. I think amazon is assigning them randomized file names upon upload because I don't think my code is specifying what to name them.

I am able to get it so that every user has their own folder in my bucket, which is how I want it, but I don't know how to get 1) tell it to name the file what it was originally named, and, 2) how to get back that URL / path after the upload is complete.

Here's my code, and for the life of me, everything I've tried isn't working. I've tried pushing the path to a user object in Mongo, but it never pushes anything, and it won't console.log the object when I try to see what it actually is (file.path or any of the methods associated like file.extension)

Here's the code I'm using:

app.use(multiMiddle); 
app.post('/api/merch/upload/:merchid', function(req, res){
    var file = req.files.file; //last property must match key from upload 
    var stream = fs.createReadStream(file.path); 

// gives every user their own folder on my bucket 
fileName = req.user._id+file.path; 
console.log(file.extension); //this doesn't log anything

return s3.writeFile(fileName, stream).then(function(){
    fs.unlink(fileName, function(err, path){
            if(err) console.error(err);
            console.log("FILE PATH", path); 
        });
    res.redirect('/');
});
});

How do I get these to be named correctly?




Aucun commentaire:

Enregistrer un commentaire