简体   繁体   English

在 localstack 上自动创建 S3 Buckets

[英]Auto create S3 Buckets on localstack

Using localstack in my docker-compose mainly to mimic S3.在我的 docker-compose 中使用 localstack 主要是为了模仿 S3。

I know I can create buckets, that's not the issue.我知道我可以创建存储桶,这不是问题所在。 What I would like to do is automatically create the buckets when I run a docker-compose up.我想做的是在运行 docker-compose up 时自动创建存储桶。

Is there something built in already for localstack?是否已经为 localstack 内置了一些东西?

A change that came in with this commit since version 0.10.0 .自版本0.10.0以来,此提交带来的更改。

When a container is started for the first time, it will execute files with extensions .sh that are found in /docker-entrypoint-initaws.d .首次启动容器时,它将执行/docker-entrypoint-initaws.d中的扩展名为 .sh 的文件。 Files will be executed in alphabetical order.文件将按字母顺序执行。 You can easily create aws resources on localstack using awslocal (or aws) cli tool in the initialization scripts.您可以在初始化脚本中使用 awslocal(或 aws)cli 工具轻松地在 localstack 上创建 aws 资源。

version: '3.7'
services:
  localstack:
    image: localstack/localstack
    environment:
      - SERVICES=s3
    ports:
      - "4566:4566"
      # - "4572:4572" Old S3 port
    volumes:
      - ./aws:/docker-entrypoint-initaws.d

With a script in directory ./aws/buckets.sh :使用目录./aws/buckets.sh中的脚本:

#!/usr/bin/env bash
set -x
awslocal s3 mb s3://bucket
set +x

Note: the set [-/+] x is purely there to turn on and off outputting of the commands being executed.注意: set [-/+] x纯粹是用来打开和关闭正在执行的命令的输出。

Will produce this output:将产生这个输出:

...
localstack_1  | Starting mock S3 (http port 4572)...
localstack_1  | Waiting for all LocalStack services to be ready
localstack_1  | Ready.
localstack_1  | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initaws.d/buckets.sh
localstack_1  | ++ awslocal s3 mb s3://bucket
localstack_1  | make_bucket: bucket
localstack_1  | ++ set +x
localstack_1  |

I was been able to achieve this with Localstack with kind of "workaround":我能够通过 Localstack 用一种“解决方法”来实现这一点:

  1. Start Localstack启动本地堆栈
  2. Create expected buckets, eg:创建预期的存储桶,例如:

     aws --endpoint-url=http://localhost:4572 s3 mb s3://test1
  3. Above line will update the s3_api_calls.json file in the Localstack directory (by default on Linux it's /tmp/localstack/data上一行将更新s3_api_calls.json目录中的 s3_api_calls.json 文件(在 Linux 上默认为/tmp/localstack/data
  4. Backup the file备份文件
  5. Put the copied file in the Localstack directory ( /tmp/localstack/data by default) before starting the stack again在再次启动堆栈之前,将复制的文件放入 Localstack 目录(默认为/tmp/localstack/data
  6. You should be able to see something like 2019-03-21T08:38:28:INFO:localstack.utils.persistence: Restored 2 API calls from persistent file: /tmp/localstack/data/s3_api_calls.json the in startup log after you start Localstack again and the bucket should be available: aws --endpoint-url=http://localhost:4572 s3 ls s3://test1您应该能够在启动日志中看到类似2019-03-21T08:38:28:INFO:localstack.utils.persistence: Restored 2 API calls from persistent file: /tmp/localstack/data/s3_api_calls.json的内容再次启动 Localstack,存储桶应该可用: aws --endpoint-url=http://localhost:4572 s3 ls s3://test1

Important notice for users of LocalStack v1.1.3 and above LocalStack v1.1.3 及以上版本用户的重要通知

Since the 1st of December 2022, LocalStack has announced the deprecation of legacy init scripts ( /docker-entrypoint-initaws.d ) with the release of v1.3.0 .自 2022 年 12 月 1 日起,LocalStack 宣布随着v1.3.0的发布弃用旧版初始化脚本 ( /docker-entrypoint-initaws.d )。 The replacement - plugggable initialisation hooks - was introduced in v1.1.0.替代品 -可插入初始化挂钩- 在 v1.1.0 中引入。

They will be fully removed in v2.0.0, posing a risk for anyone that would be using /docker-entrypoint-initaws.d with the latest version of LocalStack.它们将在 v2.0.0 中被完全删除,对任何将使用/docker-entrypoint-initaws.d和最新版本的 LocalStack 的人构成风险。

This would be a breaking change.这将是一个突破性的变化。

For future-proofing, the accepted answer is still valid but I would recommend replacing /docker-entrypoint-initaws.d with /etc/localstack/init/ready.d .为了面向未来,接受的答案仍然有效,但我建议将/docker-entrypoint-initaws.d替换为/etc/localstack/init/ready.d

This would mimic the previous intended behaviour, as it would be hooking into the READY stage where LocalStack can actually create your S3 buckets, while making sure you can still continue to update LocalStack.这将模仿之前的预期行为,因为它将挂接到 LocalStack 实际创建您的 S3 存储桶的READY阶段,同时确保您仍然可以继续更新 LocalStack。

This would be the best current Dockerfile :这将是当前最好的Dockerfile

version: '3.8'
services:
  localstack:
    image: localstack/localstack:latest
    environment:
      - SERVICES=s3
    ports:
      - "4566:4566"
    volumes:
      - ./aws:/etc/localstack/init/ready.d

DATA_DIR:用于保存持久数据的本地目录(目前仅支持这些服务:Kinesis、DynamoDB、Elasticsearch、S3)

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

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