import json
import re
from boto3.session import Session
AWS_ACCESS_KEY_ID = 'AWS_ACCESS_KEY_ID...'
AWS_SECRET_ACCESS_KEY = 'AWS_SECRET_ACCESS_KEY...'
BUCKET_NAME = 'hoge'
session = Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name='ap-northeast-1',
)
s3 = session.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
s3client = session.client('s3')
response = s3client.list_objects(
Bucket=BUCKET_NAME,
Prefix='fuga_prefix'
)
keys = []
if 'Contents' in response:
keys = [content['Key'] for content in response['Contents']]
image_keys = []
for key in keys:
if re.findall(r'^images/.*?\.jp[e]?g$', key, flags=re.I):
image_keys.append(key)
records = {
"Records": [{
"bucket": BUCKET_NAME,
"keys": image_keys
}]
}
print(json.dumps(records, indent=2))
参考