简体   繁体   English

将文件上传到特定文件夹 - AWS S3

[英]Upload file to a specific folder - AWS S3

I am trying to upload a file from Node.js application to AWS S3 Bucket.我正在尝试将文件从 Node.js 应用程序上传到 AWS S3 存储桶。

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?说,如果我在存储桶xyz中有一个名为/images的文件夹,我在哪里指定这个路径?

I want the uploaded file to into /images directory rather than at the root of the bucket directory.我希望上传的文件进入/images目录而不是存储桶目录的根目录。

The Key of an Amazon S3 object includes the full path of the object. Amazon S3 object 的Key包括 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.事实上,亚马逊不使用文件夹/目录,所以你可以简单地上传一个 object 并且Key='images/foo.jpg'它会“出现”在images目录中。

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,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM