Skip to content

Instantly share code, notes, and snippets.

@Esl1h
Created April 11, 2023 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Esl1h/8766f88f905bc1523ac0fc687383014b to your computer and use it in GitHub Desktop.
Save Esl1h/8766f88f905bc1523ac0fc687383014b to your computer and use it in GitHub Desktop.
Generate a URL from S3 objects to download (A bucket to storage logs case example)
############################################################################################################
## This script run with python3 and is able to generate pre-signed URLs from files (objects) on a s3. ##
## It is possible with it to generate URLs of several days within the same month and year for several ##
## instances without the need to provide access to the bucket for the user ##
############################################################################################################
import boto3
from colorama import Fore, Style
APP = ''
AWS_PROFILE = ''
INSTANCES = ['', ''] # Instances that generated logs
DAY = ['11', '25'] # Define days to log
BUCKET_NAME = '' # Log bucket name
YEARMONTH = '2021-11'.split('-') # Define this Year and Month for the search
for i in INSTANCES:
for d in DAY:
Key = f'{APP}/{i}/{YEARMONTH[0]}/{YEARMONTH[1]}/{APP}-api.log/{APP}-api.log.{YEARMONTH[0]}-{YEARMONTH[1]}-{d}.bz2'
boto3.setup_default_session(profile_name=AWS_PROFILE)
url = boto3.client('s3').generate_presigned_url(
ClientMethod='get_object',
Params={'Bucket': BUCKET_NAME, 'Key': Key},
ExpiresIn=3600)
print(f'{Fore.RED}Generated Logfile Download URL for {i.upper()} is:{Style.RESET_ALL}\n{url}\n\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment