简体   繁体   English

使用NodeJ将文件上传到AWS S3

[英]Upload files to aws s3 using NodeJs

I am working on a project (based on MEAN Stack) which shows different images according to different logged in users. 我正在开发一个项目(基于MEAN Stack),该项目根据不同的登录用户显示不同的图像。 I am using amazon S3 for storing those images. 我正在使用Amazon S3存储这些图像。

Currently I have creatde a different route for the admin panel where the admin can sign in and upload the images on amazon s3 for different users.(Also,is this the correct flow of the application?) 目前,我已经为管理面板创建了一条不同的路由,管理员可以登录并为不同的用户在Amazon s3上上传图像(这是否是正确的应用程序流程?)

I have the below line of code in my js file: 我的js文件中有以下代码行:

AWS.config.update({ accessKeyId: xxxxxx, secretAccessKey: xxxxxx });

I have read that this should only be for development purposes and I should not be having my accesskeyId and secretacesskey in the code like this. 我已经读到,这仅是出于开发目的,而不应该在这样的代码中使用accesskeyId和secretacesskey。

I want to know that for production what should be done? 我想知道对于生产应该做什么?

For production you need to store these keys in environment. 对于生产,您需要将这些密钥存储在环境中。 The Aws module will itself pick these from environment. Aws模块本身将从环境中选择这些。 AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY keys should be set. 应该设置AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY密钥。 For example on local machine you can test these with terminal commands export AWS_ACCESS_KEY_ID=XXXXX and export AWS_ACCESS_KEY_ID=XXXXX respectively. 例如,在本地计算机上,您可以使用终端命令分别对它们进行测试,分别export AWS_ACCESS_KEY_ID=XXXXX和导出AWS_ACCESS_KEY_ID=XXXXX

For production you will need to do the same. 对于生产,您将需要执行相同的操作。 Except you will need to do this via the node process manager you are using. 除非您需要通过所使用的节点流程管理器执行此操作。 Here is an example of pm2 process manager doing it. 这是pm2流程管理器执行此操作的示例。

module.exports = {
  apps : [
      {
        name: "myapp",
        script: "./app.js",
        watch: true,
        instance_var: 'INSTANCE_ID',
        env: {
            "PORT": 3000,
            "NODE_ENV": "development",
            "AWS_ACCESS_KEY_ID": "XXXXX",
            "AWS_SECRET_ACCESS_KEY": "XXXXX" 
        }
      }
  ]
}

http://pm2.keymetrics.io/docs/usage/environment/#specific-environment-variables The flow is similar even if you are using a different process manager. http://pm2.keymetrics.io/docs/usage/environment/#specific-environment-variables即使使用其他流程管理器,流程也相似。

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

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