使用docker compose将nodejs应用程序连接到localstack s3

时间:2020-07-27 20:30:52

标签: node.js docker amazon-s3 docker-compose localstack

我无法启动我的应用程序。它总是会因缺少凭据而失败。如何将localstack s3连接到我的应用程序。我尝试在dockerfile中设置args并运行aws configure,但由于缺少凭据而仍然失败。 我通过从.aws/credential文件复制本地凭据来挂载该卷,但这并不理想,因为我希望设置localstack凭据。 总是失败,并显示错误unable to download: CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Dockerfile

FROM node:9.2

#install AWS CLI
RUN apt-get update && apt-get install -y python python-dev python-pip python-setuptools groff less && pip install awscli


WORKDIR /migration-ui

COPY migration-ui/package.json /migration-ui

RUN npm install

COPY migration-ui /migration-ui

EXPOSE 8080

CMD ["npm","start"]

docker compose

version: '3.7'
services:
  s3:
    image: localstack/localstack:latest
    container_name: 'localstack'
    ports:
      - '4563-4599:4563-4599'
      - '8082:8081'
    environment:
      - SERVICES=s3
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - './.localstack:/tmp/localstack'
      - '/var/run/docker.sock:/var/run/docker.sock'

  bmo-ui:
    depends_on:
      - s3
    build: .

s3.js

const s3Params = {
  Bucket: process.env.BMO_BUCKET || 'dev-csi-assets',
  Key: 'bmo-migration/bmo-migration-db.json'
}

const awsConfig = require('aws-config')
const AWS = require('aws-sdk')
const s3 = require('s3')

const awsContainerCredentialsRelativeUri = !!process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
console.log("-----ENVIRONMENTS----", awsContainerCredentialsRelativeUri)
console.log("VALUES-----", process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI)
const s3Options = {
  region: 'us-east-1',                 // explicitly set AWS region
  sslEnabled: true,                    // override whether SSL is enabled
  maxRetries: 3,                       // override the number of retries for a request
  profile: 'assumed_role',             // name of profile from ~/.aws/credentials
  timeout: 15000                       // optional timeout in ms. Will use AWS_TIMEOUT
}

let s3Client = new AWS.S3(awsConfig(s3Options))

if (awsContainerCredentialsRelativeUri) {
  AWS.config.credentials = new AWS.ECSCredentials()
  s3Client = new AWS.S3()
}

const client = s3.createClient({s3Client})

const download = (path, cb = () => {}) => {
  try {
    const params = {
      localFile: path,
      s3Params: s3Params
    }
    const downloader = client.downloadFile(params)
    downloader.on('end', () => {
      console.log('done downloading')
      cb()
    })
    downloader.on('error', err => {
      console.error('unable to download:', err.stack)
      cb(err)
    })
  } catch (e) {
    console.error(e)
    cb(e)
  }
}

const upload = (path, cb = () => {}) => {
  try {
    const params = {
      localFile: path,
      s3Params: s3Params
    }
    const uploader = client.uploadFile(params)
    uploader.on('error', err => {
      console.log('unable to upload:', err.stack)
      cb(err)
    })
    uploader.on('progress', () => {
      console.log('progress', uploader.progressMd5Amount, uploader.progressAmount, uploader.progressTotal)
    })
    uploader.on('end', () => {
      console.log('done uploading')
      cb()
    })
  } catch (e) {
    console.error(e)
    cb(e)
  }
}

module.exports = { download, upload }

1 个答案:

答案 0 :(得分:1)

请尝试使用环境变量运行图像

docker run \
        -e AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
        -e AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
        -e AWS_DEFAULT_REGION="$(REGION)" \
        "<Docker-Image>"

您可以在本地运行容器。您需要设置AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY和AWS_DEFAULT_REGION环境变量

它在Makefile

中对我有用

https://github.com/harsh4870/cloud-custodian/blob/master/Makefile

相关问题