I am trying to upload a file from Node.js application to AWS S3 Bucket.
async uploadFile(file: any) {
try {
console.log(file);
const s3Params = {
Bucket: xyz,
Key: String(file.originalname),
Body: file.buffer,
};
await s3.upload(s3Params, (err, data) => {
if (err) return console.log({ err });
else console.log({ data });
});
} catch (error) {
return { data: null, error, status: 500 };
}
}
}
Say, if I have a folder inside bucket xyz
called /images
, where do I specify this path?
I want the uploaded file to into /images
directory rather than at the root of the bucket directory.
CodePudding user response:
The Key
of an Amazon S3 object includes the full path of the object.
In fact, Amazon does not use folders/directories, so you can simply upload an object with Key='images/foo.jpg'
and it will 'appear' to be in the images
directory.
The directory will 'appear' automatically. Then, if all objects in that directory are deleted, then the directory will 'disappear' (because it never actually existed).
In your code, you could use:
Key: 'images/' file.originalname,