0

This code is upload objects on s3 successfully but I was not able to figure out how to get uploaded object url return.

Code snnipet

let s3 = new aws.S3({
        accessKeyId: 'XXXXXXXXXXXXX',
        secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXX',
        region: 'XXXXXXXX'
    });
    let upload = multer({
        storage: multerS3({
            s3: s3,
            acl: 'public-read',
            bucket: 'XXXXXXXXXXx',
            metadata: (req: any, file: any, cb: any) => {                    
                cb(null, { fieldName: file.fieldname });
            },
            key: (req: any, file: any, cb: any) => {
                cb(null, Date.now().toString() + '-' + file.originalname)
            }
        })
    });

    routes.post(`api/upload-image`, upload.single('photo'), async (req: any, res: Response) => {
        
        res.send('Image uploaded successfully');
    });

Is there any way or callback for getting the url?

Deepak
  • 1,510
  • 1
  • 14
  • 27
  • what are you getting when you do this `console.log(req.file)` ? – turivishal Aug 11 '20 at 13:18
  • I'm getting Undefined – Deepak Aug 11 '20 at 13:22
  • just tested code is working perfectly, you can check again your credentials or configurations of s3, confirm again image is uploading in s3 and verify name of the image. you can get file details using `req.file` this is the final. – turivishal Aug 11 '20 at 13:52
  • It's working absolutely as expected. Thanks for help. You can also give answer to this question in details if you want for other developers. – Deepak Aug 11 '20 at 14:19
  • what was the problem? – turivishal Aug 11 '20 at 14:22
  • Not sure! I think I was looking into res object since then. :-( – Deepak Aug 11 '20 at 14:30
  • Does this answer your question? [How to get direct URL to multipart file uploaded via Node.js](https://stackoverflow.com/questions/59054367/how-to-get-direct-url-to-multipart-file-uploaded-via-node-js) – turivishal Aug 11 '20 at 14:40
  • Anyway, `req.file.location` may give you the url of the uploaded stuff. – Tura Sep 23 '22 at 04:15

1 Answers1

-1
  • multer middleware puts all the information regarding uploaded in the req.file.
  • The uploaded file url can be accessed as req.file.location
let s3 = new aws.S3({
        accessKeyId: 'XXXXXXXXXXXXX',
        secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXX',
        region: 'XXXXXXXX'
    });
    let upload = multer({
        storage: multerS3({
            s3: s3,
            acl: 'public-read',
            bucket: 'XXXXXXXXXXx',
            metadata: (req: any, file: any, cb: any) => {                    
                cb(null, { fieldName: file.fieldname });
            },
            key: (req: any, file: any, cb: any) => {
                cb(null, Date.now().toString() + '-' + file.originalname)
            }
        })
    });

    routes.post(`api/upload-image`, upload.single('photo'), async (req: any, res: Response) => {
        
       return res.json({message:'Image uploaded successfully',uploadLocation:req.file.location});
    });
Firoj Siddiki
  • 1,649
  • 1
  • 20
  • 22
  • Well, at least give some comments or description about the answer. Just posting a bunch of code doesn't help that much. – Tura Sep 23 '22 at 04:16