# /// script # dependencies = ["boto3"] # /// """Check an S3 endpoint end to end with boto3. Lists buckets, writes, reads and deletes an object, does a 12 MB multipart upload, and fetches a presigned URL. Reads AWS_ENDPOINT_URL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and S3_BUCKET (as in ~/seaweedfs/app.env). If EXEDEV_TOKEN is set, every request also carries it as X-Exedev-Authorization, for an endpoint behind exe.dev login. Run: set -a; . ~/seaweedfs/app.env; set +a; uv run s3-check.py """ import hashlib, os, sys, urllib.request import boto3 from boto3.s3.transfer import TransferConfig from botocore.config import Config endpoint = os.environ["AWS_ENDPOINT_URL"] bucket = os.environ["S3_BUCKET"] token = os.environ.get("EXEDEV_TOKEN") s3 = boto3.client("s3", endpoint_url=endpoint, region_name=os.environ.get("AWS_REGION", "us-east-1"), config=Config(signature_version="s3v4", s3={"addressing_style": "path"})) if token: def add_token(request, **kwargs): request.headers["X-Exedev-Authorization"] = "Bearer " + token s3.meta.events.register("before-send.s3", add_token) key = "s3-check/hello.txt" s3.put_object(Bucket=bucket, Key=key, Body=b"hello\n") assert s3.get_object(Bucket=bucket, Key=key)["Body"].read() == b"hello\n" print("put and get: ok") listed = [o["Key"] for o in s3.list_objects_v2(Bucket=bucket, Prefix="s3-check/").get("Contents", [])] assert key in listed print("list: ok") data = os.urandom(12 * 1024 * 1024) path = "/tmp/s3-check.bin" with open(path, "wb") as f: f.write(data) mb = 5 * 1024 * 1024 s3.upload_file(path, bucket, "s3-check/big.bin", Config=TransferConfig(multipart_threshold=mb, multipart_chunksize=mb)) etag = s3.head_object(Bucket=bucket, Key="s3-check/big.bin")["ETag"] s3.download_file(bucket, "s3-check/big.bin", path + ".down") with open(path + ".down", "rb") as f: assert hashlib.sha256(f.read()).digest() == hashlib.sha256(data).digest() print("multipart upload (%s parts) and download: ok" % etag.strip('"').split("-")[-1]) url = s3.generate_presigned_url("get_object", Params={"Bucket": bucket, "Key": key}, ExpiresIn=300) req = urllib.request.Request(url) if token: req.add_header("X-Exedev-Authorization", "Bearer " + token) assert urllib.request.urlopen(req).read() == b"hello\n" print("presigned URL: ok") s3.delete_objects(Bucket=bucket, Delete={"Objects": [{"Key": key}, {"Key": "s3-check/big.bin"}]}) os.remove(path); os.remove(path + ".down") print("delete: ok") print("All checks passed against", endpoint)