Skip to content

Instantly share code, notes, and snippets.

@squarepegsys
Created September 20, 2019 12:53
Show Gist options
  • Save squarepegsys/3a90e472e48e7d749ccbaa3c85a07073 to your computer and use it in GitHub Desktop.
Save squarepegsys/3a90e472e48e7d749ccbaa3c85a07073 to your computer and use it in GitHub Desktop.
wrapper around s3cmd
#!/usr/bin/env python
import sys
import subprocess
def get_profile_credentials(profile_name):
# stolen from https://gist.github.com/wjimenez5271/defeede8eb4a63afc9d8
from configparser import ConfigParser
from configparser import ParsingError
from configparser import NoOptionError
from configparser import NoSectionError
from os import path
config = ConfigParser()
config.read([path.join(path.expanduser("~"),'.aws/credentials')])
try:
aws_access_key_id = config.get(profile_name, 'aws_access_key_id')
aws_secret_access_key = config.get(profile_name, 'aws_secret_access_key')
aws_session_token = config.get(profile_name, 'aws_session_token')
except ParsingError:
print('Error parsing config file')
raise
except (NoSectionError, NoOptionError):
try:
aws_access_key_id = config.get('default', 'aws_access_key_id')
aws_secret_access_key = config.get('default', 'aws_secret_access_key')
aws_session_token = config.get('default', 'aws_session_token')
except (NoSectionError, NoOptionError):
print('Unable to find valid AWS credentials')
raise
return aws_access_key_id, aws_secret_access_key, aws_session_token
if __name__=='__main__':
"""
--access_key=ACCESS_KEY
AWS Access Key
--secret_key=SECRET_KEY
AWS Secret Key
--access_token=ACCESS_TOKEN
"""
profile, cmd_args = sys.argv[1], sys.argv[2:]
aws_args = get_profile_credentials(profile)
cmd = [
'/path/to/s3cmd',
f'--access_key={aws_args[0]}',
f'--secret_key={aws_args[1]}',
f'--access_token={aws_args[2]}'
] + cmd_args
subprocess.run(cmd)
@squarepegsys
Copy link
Author

Usage: s3ctl.py <profile_name> <s3cmd command>

So if you are putting fancy-file into s3://my-fancy-bucket/some-folder using your development AWS profile, it's:

s3ctl.py development put fancy-file s3://my-fancy-bucket/some-folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment