Files
atc-agents/infra/hadoop/s3util.py
T

45 lines
1.4 KiB
Python
Raw Normal View History

"""List/delete objects under a prefix in the Dell ECS S3 bucket.
Credentials are read from the environment so no secrets are committed:
S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY, S3_BUCKET (default "data").
ECS rejects bulk delete (Content-MD5), so deletes are done one object at a time.
"""
import os
import sys
import boto3
from botocore.config import Config
s3 = boto3.client(
"s3",
endpoint_url=os.environ["S3_ENDPOINT"],
aws_access_key_id=os.environ["S3_ACCESS_KEY"],
aws_secret_access_key=os.environ["S3_SECRET_KEY"],
config=Config(s3={"addressing_style": "path"}, signature_version="s3v4"),
)
BUCKET = os.environ.get("S3_BUCKET", "data")
def listp(prefix):
p = s3.get_paginator("list_objects_v2")
n = 0
for page in p.paginate(Bucket=BUCKET, Prefix=prefix):
for o in page.get("Contents", []):
print(f"{o['Size']:>12} {o['Key']}")
n += 1
print(f"# total objects under '{prefix}': {n}")
def delp(prefix):
p = s3.get_paginator("list_objects_v2")
n = 0
for page in p.paginate(Bucket=BUCKET, Prefix=prefix):
for o in page.get("Contents", []):
s3.delete_object(Bucket=BUCKET, Key=o["Key"])
n += 1
print(f"# deleted {n} objects under '{prefix}'")
if __name__ == "__main__":
cmd, prefix = sys.argv[1], sys.argv[2]
(listp if cmd == "ls" else delp)(prefix)