Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge first working iteration to main #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To install dependencies, use the included requirements file with python pip, i.e.
pip3 install -r requirements.txt

3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
boto3==1.26.99
botocore==1.29.99
requests==2.28.2
47 changes: 47 additions & 0 deletions s3share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3

import logging
import boto3
from botocore.exceptions import ClientError
import requests
import argparse
from botocore.client import Config

region = None

parser = argparse.ArgumentParser(description="Generate expiring S3 URL")
parser.add_argument("-b", "--bucket", help = "S3 bucket name", required=True, type=str)
parser.add_argument("-o", "--object", help = "S3 object name", required=True, type=str)
parser.add_argument("-r", "--region", help = "S3 region name", required=False, default='eu-west-1', type=str)
parser.add_argument("-e", "--expiry", help = "Expiry time in seconds", required=True, type=int)
args = parser.parse_args()

def create_presigned_url(bucket_name, object_name, expiration=args.expiry):
client_config = Config(
signature_version = 'v4',
retries = {
'max_attempts': 10,
'mode': 'standard'
}
)

s3_client = boto3.session.Session(
region_name=boto3.client('s3').get_bucket_location(Bucket=bucket_name)["LocationConstraint"]
).client("s3", config=client_config)

try:
response = s3_client.generate_presigned_url('get_object', Params={
'Bucket': bucket_name,
'Key': object_name
},
ExpiresIn=expiration
)
except ClientError as e:
logging.error(e)
return None

return response

url = create_presigned_url(args.bucket, args.object, args.expiry)
if url is not None:
print(url)