localstack + docker-compose + S3

localstack + docker-compose + S3

我在 Java 获得了一项服务 Spring-boot + spring-cloud-aws-messaging ... 将文件上传到 S3 ...

尝试将文件上传到 S3 存储桶时失败(仅当我 运行 在 docker-compose 中时)。

这是我的代码

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-aws-messaging</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

S3 客户端

 public AmazonS3 amazonS3Client() {
        return AmazonS3ClientBuilder
                .standard()
                .withEndpointConfiguration(
                        new AwsClientBuilder.EndpointConfiguration(appConfig.getAwsS3Endpoint(), appConfig.getRegion()))
                .withCredentials(credentialsProvider)
                .build();
    }

docker-编写yaml

version: '3.7'
services:

  dumb-service:
    build: ../dumb-service/.
    image: dumb-service:latest
    hostname: dumb-service
    ports:
      - '8080:8080'
      - '5006:5006'
    environment:
      JAVA_OPTS: '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5006' 
      AWS_S3_ENDPOINT: 'http://localstack:4566/'
      AWS_S3_BUCKET: 'dumb-bucket'
      AWS_SQS_ENDPOINT: 'http://localstack:4566'
      PDF_REQUEST_QUEUE_URL: 'http://localstack:4566/000000000000/dumb-inbound.fifo'
      PDF_REQUEST_QUEUE_NAME: 'dumb-inbound.fifo'
      AWS_REGION: "${AWS_REGION}"
      AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}"
      AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}"
    depends_on:
      - "localstack"

  # localstack home: https://github.com/localstack/localstack
  localstack:
    hostname: localstack
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack
    ports:
      - '4566:4566'
      - '4571:4571'
    environment:
      - SERVICES=${SERVICES-}
      - DEBUG=${DEBUG-}
      - DATA_DIR=${DATA_DIR-}
      - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
      - HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp}/localstack:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

错误是:

无法执行 HTTP 请求:dumb-bucket.localstack

(此代码适用于 AWS 服务或 运行ning code native + localstack)

关于如何连接到 localstack S3 有什么想法吗?

如果您尝试通过 aws api 访问带有 localstack 的 s3 存储桶;您需要 withPathStyleEnabled 标志打开。

例如:

    AmazonS3 createLocalStackS3WithEndpointConfig(String endpointUrl, String awsRegion) {
    return createAwsBeanWithEndpointConfig(
        endpointUrl, awsRegion,
        epConf -> AmazonS3ClientBuilder.standard().withEndpointConfiguration(epConf).withPathStyleAccessEnabled(true).build(), 
        AmazonS3ClientBuilder::defaultClient
    );
} `

参考:https://pythonrepo.com/repo/localstack-localstack

的故障排除部分