Skip to content

Instantly share code, notes, and snippets.

@TakesxiSximada
Last active July 9, 2020 10:40
Show Gist options
  • Save TakesxiSximada/a6925f099e599535e10ae7aabf2f2b30 to your computer and use it in GitHub Desktop.
Save TakesxiSximada/a6925f099e599535e10ae7aabf2f2b30 to your computer and use it in GitHub Desktop.
s3とs3のClientとs3のダミーサーバー

s3とs3のClientとs3のダミーサーバー

CLI Clients

AWS CLI

https://aws.amazon.com/jp/cli/

コマンド内容
s3 lsオブジェクトを一覧表示する
s3 cpオブジェクトをコピーする
s3 mbバケットを作成する
s3 mvオブジェクトに一時的にアクセスできるURLを発行する
s3 rbバケットを削除する
s3 rmオブジェクトを削除する
s3 syncディレクトリとバケット(もしくはその反対)を同期する
s3 websiteバケットのwebsite設定を更新する

Makefileを参照。

Python Clients

botocore

https://github.com/boto/botocore

boto3

https://github.com/boto/boto3

使用例: example_boto3.py

aiobotocore

https://github.com/aio-libs/aiobotocore

aioboto3

https://github.com/terrycain/aioboto3/

使用例: example_aioboto3.py

Dummy Server

fake-s3

https://github.com/jubos/fake-s3

S3Mock

https://github.com/adobe/S3Mock

s3proxy

https://github.com/gaul/s3proxy

minio

https://github.com/minio/minio

localstack

https://github.com/localstack/localstack

Amazon S3 API

https://docs.aws.amazon.com/AmazonS3/latest/API/Type_API_Reference.html

Actions

Amazon Simple Storage Service

https://docs.aws.amazon.com/AmazonS3/latest/API/API_Operations_Amazon_Simple_Storage_Service.html

AWS S3 Control

https://docs.aws.amazon.com/AmazonS3/latest/API/API_Operations_AWS_S3_Control.html

Data Type

Amazon Simple Storage Service

https://docs.aws.amazon.com/AmazonS3/latest/API/API_Types_Amazon_Simple_Storage_Service.html

AWS S3 Control

https://docs.aws.amazon.com/AmazonS3/latest/API/API_Types_AWS_S3_Control.html

aioboto3==8.0.3
aiobotocore==1.0.4
aiohttp==3.6.2
aioitertools==0.7.0
async-timeout==3.0.1
attrs==19.3.0
autopep8==1.5.3
boto3==1.12.32
botocore==1.15.32
chardet==3.0.4
docutils==0.15.2
idna==2.10
isort==4.3.21
jmespath==0.10.0
multidict==4.7.6
pycodestyle==2.6.0
python-dateutil==2.8.1
s3transfer==0.3.3
six==1.15.0
toml==0.10.1
typing-extensions==3.7.4.2
urllib3==1.25.9
wrapt==1.12.1
yarl==1.4.2
version: "3.8"
services:
fake_s3:
image: instructure/fake-s3
ports:
- "4569:4569"
s3mock:
image: adobe/s3mock
ports:
- "9090:9090"
- "9191:9191"
s3proxy:
image: andrewgaul/s3proxy
ports:
- "80:80"
minio:
image: minio/minio
command: server /data
ports:
- "9000:9000"
localstack:
image: localstack/localstack
ports:
- "4566:4566"
- "8080:8080"
"""aioboto3を使ったs3へのアクセスの例"""
import asyncio
import aioboto3
s3_endpoint_url = "http://127.0.0.1:9000"
aioboto3.setup_default_session(profile_name="local")
async def main_async(bucket_name):
async with aioboto3.resource("s3", endpoint_url=s3_endpoint_url) as s3_resource:
async with aioboto3.client("s3", endpoint_url=s3_endpoint_url) as s3_client:
bucket = await s3_resource.Bucket(bucket_name)
await bucket.create()
await bucket.upload_file("./README.org", "README.org")
async for s3_obj in bucket.objects.all():
print(s3_obj)
url = await s3_client.generate_presigned_url(
"get_object", Params={
"Bucket": bucket.name,
"Key": "README.org",
},
ExpiresIn=30
)
print(url)
async for s3_obj in bucket.objects.filter(Prefix="R"):
print(s3_obj)
async for s3_obj in bucket.objects.filter(Prefix="R"):
await s3_obj.delete()
await bucket.delete()
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(main_async("testing1"))
if __name__ == "__main__":
main()
"""
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#bucket
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#object
"""
import boto3
boto3.setup_default_session(profile_name="local")
# resource: Object指向っぽくデータを扱える。ただしclientにはできるがresourceではサポートされていない操作もある。
s3_resource = boto3.resource("s3", endpoint_url="http://127.0.0.1:4566")
# client: 実際のAPIに対応している。ただし記述が冗長になったり、結果がJSONで返されるので自分でparseする必要がある。
s3_client = boto3.client("s3", endpoint_url="http://127.0.0.1:4566")
# resourceとclientはどのみち両方必要になるので同じタイミング/近くで生成するのが良さそう。
# BUCKET_NAME = "mybucket"
# bucket = s3_resource.Bucket(BUCKET_NAME)
def create_bucket(bucket_name):
bucket = s3_resource.Bucket(bucket_name)
bucket.create()
return bucket
def remove_bucket(bucket):
bucket.delete()
def upload_obj(bucket):
bucket.upload_file("./README.org", "README.org")
def list_obj(bucket):
"Get list of all s3 objects"
for s3_obj in bucket.objects.all():
print(s3_obj)
def filter_obj(bucket):
"Search s3 objects"
for s3_obj in bucket.objects.filter(Prefix="R"):
print(s3_obj)
def remove_obj(bucket):
"Remove s3 object"
for s3_obj in bucket.objects.filter(Prefix="R"):
s3_obj.delete()
def get_presigned_url(bucket):
"Get presigned URL"
url = s3_client.generate_presigned_url(
"get_object", Params={
"Bucket": bucket.name,
"Key": "README.org",
},
ExpiresIn=30)
print(url)
def main():
bucket = create_bucket("testbucket2")
upload_obj(bucket)
list_obj(bucket)
get_presigned_url(bucket)
filter_obj(bucket)
remove_obj(bucket)
remove_bucket(bucket)
AWS_CLI_OPTIONS := --profile local --no-paginate --no-verify-ssl --endpoint-url http://127.0.0.1:9000
.PHONY: deps
deps:
pip install -c constraints.txt -r requirements.txt
.PHONY: constraints.txt
constraints.txt:
pip freeze > constraints.txt
.PHONY: ls
ls:
@# オブジェクトを一覧表示する
aws $(AWS_CLI_OPTIONS) s3 ls \
--recursive \
--page-size 1000 \
--human-readable \
--summarize \
s3://mybucket
.PHONY: cp
cp:
@## オブジェクトをコピーする
aws $(AWS_CLI_OPTIONS) s3 cp \
--content-type image/jpg \
./README.org s3://mybucket/foo
.PHONY: mb
mb:
@## バケットを作成する
aws $(AWS_CLI_OPTIONS) s3 mb \
s3://testing
.PHONY: mv
mv:
@## オブジェクトを移動する
aws $(AWS_CLI_OPTIONS) s3 mv \
--content-type image/jpg \
./test.http s3://testing/baz
.PHONY: presign
presign:
@## オブジェクトに一時的にアクセスできるURLを発行する
aws $(AWS_CLI_OPTIONS) s3 presign \
--expires-in 60 \
s3://testing/baz
.PHONY: rb
rb:
@## バケットを削除する
aws $(AWS_CLI_OPTIONS) s3 rb \
--force \
s3://testing
.PHONY: rm
rm:
@## オブジェクトを削除する
aws $(AWS_CLI_OPTIONS) s3 rm \
s3://mybucket/foo
.PHONY: sync
sync:
@## ディレクトリとバケット(もしくはその反対)を同期する
aws $(AWS_CLI_OPTIONS) s3 sync \
--exclude '.git/*' \
. s3://mybucket/test/
.PHONY: website
website:
@## バケットのwebsite設定を更新する
aws $(AWS_CLI_OPTIONS) s3 website \
--index-document s3://mybucket/index.html \
--error-document s3://mybucket/error.html \
s3://mybucket
aioboto3
autopep8
boto3
isort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment