Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Sep 23, 2019

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
You can’t perform that action at this time.