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

Reading endpoint_url from ~/.s3cfg when present. #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 10 additions & 8 deletions s4cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class BotoClient(object):
"If the bucket is configured as a website, redirects requests for this object to another object in the same bucket or to an external URL. Amazon S3 stores the value of this header in the object metadata."),
]

def __init__(self, opt, aws_access_key_id=None, aws_secret_access_key=None):
def __init__(self, opt, aws_access_key_id=None, aws_secret_access_key=None, endpoint_url=None):
'''Initialize boto3 API bridge class. Calculate and cache all legal parameters
for each method we are going to call.
'''
Expand All @@ -381,7 +381,7 @@ def __init__(self, opt, aws_access_key_id=None, aws_secret_access_key=None):
self.client = self.boto3.client('s3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
endpoint_url=opt.endpoint_url)
endpoint_url=endpoint_url if endpoint_url is not None else opt.endpoint_url)
else:
self.client = self.boto3.client('s3', endpoint_url=opt.endpoint_url)

Expand Down Expand Up @@ -620,15 +620,16 @@ class S3Handler(object):
'''

S3_KEYS = None

S3_ENDPOINT = None

@staticmethod
def s3_keys_from_env():
'''Retrieve S3 access keys from the environment, or None if not present.'''
env = os.environ
if S3_ACCESS_KEY_NAME in env and S3_SECRET_KEY_NAME in env:
keys = (env[S3_ACCESS_KEY_NAME], env[S3_SECRET_KEY_NAME])
debug("read S3 keys from environment")
return keys
return keys,None
else:
return None

Expand All @@ -638,7 +639,7 @@ def s3_keys_from_cmdline(opt):
if opt.access_key != None and opt.secret_key != None:
keys = (opt.access_key, opt.secret_key)
debug("read S3 keys from commandline")
return keys
return keys,opt.endpoint_url
else:
return None

Expand All @@ -655,16 +656,17 @@ def s3_keys_from_s3cfg(opt):
config = ConfigParser.ConfigParser()
config.read(s3cfg_path)
keys = config.get("default", "access_key"), config.get("default", "secret_key")
endpoint_url = config.get("default", "host_base")
debug("read S3 keys from %s file", s3cfg_path)
return keys
return keys,endpoint_url
except Exception as e:
info("could not read S3 keys from %s file; skipping (%s)", s3cfg_path, e)
return None

@staticmethod
def init_s3_keys(opt):
'''Initialize s3 access keys from environment variable or s3cfg config file.'''
S3Handler.S3_KEYS = S3Handler.s3_keys_from_cmdline(opt) or S3Handler.s3_keys_from_env() \
S3Handler.S3_KEYS,S3Handler.S3_ENDPOINT = S3Handler.s3_keys_from_cmdline(opt) or S3Handler.s3_keys_from_env() \
or S3Handler.s3_keys_from_s3cfg(opt)

def __init__(self, opt):
Expand All @@ -681,7 +683,7 @@ def connect(self):
'''Connect to S3 storage'''
try:
if S3Handler.S3_KEYS:
self.s3 = BotoClient(self.opt, S3Handler.S3_KEYS[0], S3Handler.S3_KEYS[1])
self.s3 = BotoClient(self.opt, S3Handler.S3_KEYS[0], S3Handler.S3_KEYS[1], S3Handler.S3_ENDPOINT)
else:
self.s3 = BotoClient(self.opt)
except Exception as e:
Expand Down