LoginSignup
1
1

More than 1 year has passed since last update.

S3APIを使用してlocalstackにデータをPUTしてみる

Last updated at Posted at 2022-06-13

前提

localstackを構築してみたので、実際にデータをPUTしてみたいと思います。
AWSのS3APIが使用できるようなので、そちらを利用します。
前回の記事はこちら。

環境

  • macOS 11.6.4
  • docker-compose: 2.0.0
  • localstack: 0.14.3
  • S3API

手順

localstackが立ち上がっているか確認する

XXX % docker ps           
CONTAINER ID   IMAGE                          COMMAND                  CREATED          STATUS          PORTS                                             NAMES
205ec2df7163   localstack/localstack:0.14.3   "docker-entrypoint.sh"   20 minutes ago   Up 20 minutes   4510-4559/tcp, 5678/tcp, 0.0.0.0:4566->4566/tcp   localstack

バケットの一覧を表示する

aws s3api list-buckets --endpoint-url=http://localhost:${localstackのポート番号} --profile ${プロファイル}

バケットは未作成なので空になっています。

XXX % aws s3api list-buckets --endpoint-url=http://localhost:4566 --profile localstack
{
    "Buckets": [],
    "Owner": {
        "DisplayName": "webfile",
        "ID": "XXXXXXXXXXXXXXXXXXXXXXX"
    }
}

バケットを作成する

aws s3api create-bucket 
 --bucket=${バケット名} 
 --endpoint-url=http://localhost:${localstackのポート番号} 
 --profile ${プロファイル} 
 --create-bucket-configuration LocationConstraint=${リージョン}

--create-bucket-configurationでLocationConstraintを指定することが重要です。

XXX % aws s3api create-bucket 
 --bucket=sample-backet 
 --endpoint-url=http://localhost:4566 
 --profile localstack 
 --create-bucket-configuration LocationConstraint=ap-northeast-1
{
    "Location": "http://sample-backet.s3.localhost.localstack.cloud:4566/"
}

LocationConstraintを指定しなかった場合、以下のエラーが発生します。

An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.

任意の場所にファイルを作成する

touch ${ファイル名} | ls -l

ファイル名はなんでも良いです。

XXX % touch sample.txt | ls -l
-rw-r--r--  1 mm  staff    0  6 13 14:45 sample.txt

ファイルをlocalstackにPUTする

aws s3api put-object 
 --bucket=${バケット名} 
 --key=${ファイル名} 
 --endpoint-url=http://localhost:${localstackのポート番号} 
 --profile ${プロファイル} 
XXX % aws s3api put-object 
 --bucket=sample-backet 
 --key=sample.txt 
 --endpoint-url=http://localhost:4566 
 --profile localstack
{
    "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\""
}

先ほど作成したファイルを削除する

rm ${ファイル名} | ls -l

sample.txtがなくなっていますね。

XXX % rm sample.txt | ls -l

ファイルをlocalstackからGETしてくる

aws s3api get-object 
 --bucket=${バケット名}  
 --key=${ファイル名} 
 --endpoint-url=http://localhost:${localstackのポート番号} 
 --profile ${プロファイル} 
 ${ファイル名(outfile)}

一番最後のファイル名(outfile) は必須項目です。ローカルにgetしてくるときのファイル名を指定します。

XXX % aws s3api get-object 
 --bucket=sample-backet 
 --key=sample.txt 
 --endpoint-url=http://localhost:4566 
 --profile localstack 
 sample.txt
{
    "AcceptRanges": "bytes",
    "LastModified": "2022-06-13T05:48:23+00:00",
    "ContentLength": 0,
    "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
    "VersionId": "null",
    "ContentLanguage": "en-US",
    "ContentType": "binary/octet-stream",
    "Metadata": {}
}

sample.txtをGETしてこれています。

XXX % ls -l
-rw-r--r--  1 mm  staff    0  6 13 14:53 sample.txt
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1