From fdb27bdc0c696788250134b20520e910ccd72f72 Mon Sep 17 00:00:00 2001 From: Sreevatsank Kadaveru Date: Wed, 24 May 2023 03:10:20 -0700 Subject: [PATCH] Reading endpoint url from `~/.s3cfg` when present. --- s4cmd.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/s4cmd.py b/s4cmd.py index bcdf982..1df8ced 100755 --- a/s4cmd.py +++ b/s4cmd.py @@ -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. ''' @@ -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) @@ -620,7 +620,8 @@ 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.''' @@ -628,7 +629,7 @@ def s3_keys_from_env(): 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 @@ -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 @@ -655,8 +656,9 @@ 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 @@ -664,7 +666,7 @@ def s3_keys_from_s3cfg(opt): @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): @@ -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: