From 1a793a9153286e9327ef90895715edfbb116b447 Mon Sep 17 00:00:00 2001 From: dogisgreat Date: Sun, 19 Jan 2025 22:42:00 -0500 Subject: [PATCH] Fixes #1361 and #1027 - fix python 3 compat for configparser and add AWS shared credential env var (#1365) --- S3/Config.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/S3/Config.py b/S3/Config.py index 58ecafc3..b2af74a1 100644 --- a/S3/Config.py +++ b/S3/Config.py @@ -73,6 +73,8 @@ # In python 3, unicode -> str, and str -> bytes unicode = str +PY3 = (sys.version_info >= (3, 0)) + def is_bool_true(value): """Check to see if a string is true, yes, on, or 1 @@ -268,7 +270,7 @@ def __init__(self, configfile = None, access_key=None, secret_key=None, access_t try: self.read_config_file(configfile) except IOError: - if 'AWS_CREDENTIAL_FILE' in os.environ or 'AWS_PROFILE' in os.environ: + if 'AWS_SHARED_CREDENTIALS_FILE' in os.environ or 'AWS_CREDENTIAL_FILE' in os.environ or 'AWS_PROFILE' in os.environ: self.aws_credential_file() # override these if passed on the command-line @@ -440,7 +442,8 @@ def role_refresh(self): def aws_credential_file(self): try: aws_credential_file = os.path.expanduser('~/.aws/credentials') - credential_file_from_env = os.environ.get('AWS_CREDENTIAL_FILE') + credential_file_from_env = os.environ.get('AWS_SHARED_CREDENTIALS_FILE') \ + or os.environ.get('AWS_CREDENTIAL_FILE') if credential_file_from_env and \ os.path.isfile(credential_file_from_env): aws_credential_file = base_unicodise(credential_file_from_env) @@ -455,9 +458,11 @@ def aws_credential_file(self): config_string = fp.read() try: try: - # readfp is replaced by read_file in python3, - # but so far readfp it is still available. - config.readfp(io.StringIO(config_string)) + buf = io.StringIO(config_string) + if PY3: + config.read_file(buf) + else: + config.readfp(buf) except MissingSectionHeaderError: # if header is missing, this could be deprecated # credentials file format as described here: @@ -465,7 +470,11 @@ def aws_credential_file(self): # then do the hacky-hack and add default header # to be able to read the file with PyConfigParser() config_string = u'[default]\n' + config_string - config.readfp(io.StringIO(config_string)) + buf = io.StringIO(config_string) + if PY3: + config.read_file(buf) + else: + config.readfp(buf) except ParsingError as exc: raise ValueError( "Error reading aws_credential_file "