【问题标题】:localstack docker-compose - The AWS Access Key Id you provided does not exist in our recordslocalstack docker-compose - 您提供的 AWS 访问密钥 ID 在我们的记录中不存在
【发布时间】:2023-02-10 19:19:57
【问题描述】:

通过 docker-compose 运行 localstack 和 app 来虚拟一个 s3 bucket,但是得到错误:

"msg":"Failed to upload file /test-data/test.txt: The AWS Access Key Id you provided does not exist in our records."

如果我使用有效的 AWS 凭据通过 docker run 运行该应用程序,它将成功运行并连接。我做错了什么想法?

version: '3.8'

services:
  postbox:
    build: .
    ports: 
      - "8000:8000"
    environment:
      - S3_BUCKET=localstack-postbox-s3
      - MESSAGE_BUS_ENDPOINT=http://localhost
      - AWS_REGION=eu-west-2
      - AWS_ACCESS_KEY_ID=xyz
      - AWS_SECRET_ACCESS_KEY=aaa
      - NODE_ENV=test
    depends_on:
      - localstack
    
  localstack:
    container_name: localstack
    image: localstack/localstack:0.14.2
    ports:
      - "4566-4599:4566-4599"
    environment:
      - SERVICES=s3
    volumes:
      - ./infra/localstack:/docker-entrypoint-initaws.d

  aws:
    container_name: aws-cli
    image: amazon/aws-cli:latest
    environment:
      - AWS_REGION=eu-west-2
      - AWS_ACCESS_KEY_ID=xyz
      - AWS_SECRET_ACCESS_KEY=aaa
    entrypoint: tail -f /dev/null
    depends_on:
      - localstack
      - postbox

【问题讨论】:

  • 在您的应用程序代码中实例化 aws 客户端时,您是否使用 endpoint_url?使用http://localhost:4566指向localstack
  • 嗨,是的,我们已经编写了本地运行并添加端点的代码,但它仍然无法看到它,所以运行:```
  • 这对我来说似乎是一个网络问题,你能发布你的应用程序代码的 sn-p 吗?
  • 有一些游戏时间并添加了一个 aws-cli 图像,如果我从工作站运行以下命令,我会返回结果。如果我从 docker aws-cli 运行它失败,所以假设它是 docker-compose 的网络问题,据我所知,默认网络允许访问工作站所有图像等``` aws s3api list-buckets --endpoint_url=localhost:4566 ``` 更新了 root ticket 中的 docker-compose,因为这里没有足够的空间。
  • 在 aws-cli 服务上添加 network_mode: "host" 使其能够成功运行 s3api 命令。但是如果我将它添加到邮箱服务,它会阻止我使用 localhost:8000 连接到该服务

标签: amazon-web-services docker-compose localstack


【解决方案1】:

得到这一切工作!

首先确保为任何 localstack 调用添加了 endpoint_url

其次,在 docker 应用程序中确保任何本地主机调用都更新为 host.docker.internal

【讨论】:

    【解决方案2】:

    如果你来自 Java 领域,我发现在我的 lambda 中使用以下 Java 11 代码对我有用:

      /**
       * @return an instance of S3Client
       */
      public static S3Client s3Client() {
        final S3ClientBuilder builder = S3Client.builder();
        getEndpointOverride().ifPresent(builder::endpointOverride);
        return builder
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .region(Region.of(getEnvironmentVariable("AWS_REGION").orElse("us-east-1")))
                .httpClientBuilder(UrlConnectionHttpClient.builder())
                .build();
      }
    
      /**
       * Determine any endpoint override.
       *
       * @return Optional endpoint override value.
       */
      private static Optional<URI> getEndpointOverride() {
        Optional<URI> result = Optional.empty();
        final Optional<String> localstackHostname = getEnvironmentVariable("LOCALSTACK_HOSTNAME");
        if (localstackHostname.isPresent()) {
          final Optional<String> port = getEnvironmentVariable("EDGE_PORT");
          try {
            result = Optional.of(new URI("http://" + localstackHostname.get() + ":" + port.orElse("4566")));
          } catch (final URISyntaxException e) {
            log.error("Failed to create URI", e);
          }
        }
        return result;
      }
    
      /**
       * Read environment variable.
       *
       * @param environmentVariable Variable to read.
       * @return Optional.empty() if value is not present, empty or blank.
       */
      private static Optional<String> getEnvironmentVariable(final String environmentVariable) {
        Optional<String> result = Optional.empty();
        final String value = System.getenv(environmentVariable);
        if (!Objects.isNull(value) && !value.isBlank()) {
          result = Optional.of(value);
        }
        return result;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多