Created
March 9, 2023 20:09
-
-
Save demiurg/9b1dd011cdfc72e76268ee17bb353e62 to your computer and use it in GitHub Desktop.
presign s3 urls based on prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import boto3 | |
import argparse | |
def list_objects(bucket, prefix="", suffix=""): | |
s3 = boto3.client("s3") | |
paginator = s3.get_paginator("list_objects_v2") | |
for page in paginator.paginate(Prefix=prefix, Bucket=bucket): | |
try: | |
contents = page["Contents"] | |
except KeyError: | |
return | |
for obj in contents: | |
key = obj["Key"] | |
if key.endswith(suffix): | |
yield obj | |
def create_presigned_url(bucket_name, key, expiration): | |
# Generate a presigned URL for the S3 object | |
s3_client = s3_client = boto3.client("s3") | |
response = s3_client.generate_presigned_url( | |
"get_object", | |
Params={"Bucket": bucket_name, "Key": key}, | |
ExpiresIn=expiration, | |
) | |
# The response contains the presigned URL | |
return response | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-u", | |
"--unit", | |
default="hour", | |
choices=["second", "minute", "hour"], | |
help="Choose a time unit", | |
) | |
parser.add_argument("-t", "--time", default=4, help="Specify length of time.") | |
parser.add_argument( | |
"--suffix", default="", help="Output only keys ending with suffix, ie .tif" | |
) | |
parser.add_argument("S3URL") | |
unit_seconds = { | |
"second": 1, | |
"minute": 60, | |
"hour": 3600, | |
} | |
args = parser.parse_args() | |
proto, _, bucket, prefix = args.S3URL.split("/", 3) | |
ttl = args.time * unit_seconds[args.unit] | |
for obj in list_objects(bucket, prefix, args.suffix): | |
response = create_presigned_url(bucket, obj["Key"], ttl) | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment