vai*_*eep 2 amazon-s3 amazon-web-services node.js aws-sdk
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 };
}
}
}
Run Code Online (Sandbox Code Playgroud)
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.
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,
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2897 次 |
| 最近记录: |