# Buggy code
def initialize_client(self) -> None:
    """
    Initializes an S3 Client.
    """
    import boto3

    aws_access_key = None
    aws_secret_access_key = None

    if self.aws_credentials_secret:
        aws_credentials = Secret(self.aws_credentials_secret).get()
        if isinstance(aws_credentials, str):
            aws_credentials = json.loads(aws_credentials)

        aws_access_key = aws_credentials["ACCESS_KEY"]
        aws_secret_access_key = aws_credentials["SECRET_ACCESS_KEY"]

    s3_client = boto3.client(
        "s3",
        aws_access_key_id=aws_access_key,
        aws_secret_access_key=aws_secret_access_key,
    )
    self.client = s3_client


# Traceback (KeyError)
February 29th 2020 at 8:09:43am | prefect.CloudTaskRunner
ERROR
Failed to set task state with error: KeyError('credential_provider')
Traceback (most recent call last):
File "/opt/conda/lib/python3.7/site-packages/prefect/engine/cloud/task_runner.py", line 117, in call_runner_target_handlers
cloud_state = prepare_state_for_cloud(new_state)
File "/opt/conda/lib/python3.7/site-packages/prefect/engine/cloud/utilities.py", line 21, in prepare_state_for_cloud
res.store_safe_value()
File "/opt/conda/lib/python3.7/site-packages/prefect/engine/result.py", line 93, in store_safe_value
value = self.result_handler.write(self.value)
File "/opt/conda/lib/python3.7/site-packages/prefect/engine/result_handlers/s3_result_handler.py", line 103, in write
self.client.upload_fileobj(stream, Bucket=self.bucket, Key=uri)
File "/opt/conda/lib/python3.7/site-packages/prefect/engine/result_handlers/s3_result_handler.py", line 67, in client
self.initialize_client()
File "/opt/conda/lib/python3.7/site-packages/prefect/engine/result_handlers/s3_result_handler.py", line 60, in initialize_client
aws_secret_access_key=aws_secret_access_key,
File "/opt/conda/lib/python3.7/site-packages/boto3/__init__.py", line 91, in client
return _get_default_session().client(*args, **kwargs)
File "/opt/conda/lib/python3.7/site-packages/boto3/session.py", line 263, in client
aws_session_token=aws_session_token, config=config)
File "/opt/conda/lib/python3.7/site-packages/botocore/session.py", line 823, in create_client
credentials = self.get_credentials()
File "/opt/conda/lib/python3.7/site-packages/botocore/session.py", line 428, in get_credentials
'credential_provider').load_credentials()
File "/opt/conda/lib/python3.7/site-packages/botocore/session.py", line 923, in get_component
del self._deferred[name]
KeyError: 'credential_provider'

# Fixed code
def initialize_client(self) -> None:
    """
    Initializes an S3 Client.
    """
    import boto3

    aws_access_key = None
    aws_secret_access_key = None

    if self.aws_credentials_secret:
        aws_credentials = Secret(self.aws_credentials_secret).get()
        if isinstance(aws_credentials, str):
            aws_credentials = json.loads(aws_credentials)

        aws_access_key = aws_credentials["ACCESS_KEY"]
        aws_secret_access_key = aws_credentials["SECRET_ACCESS_KEY"]

    # use a new boto session when initializing in case we are in a new thread
    # see https://boto3.amazonaws.com/v1/documentation/api/latest/guide/resources.html?#multithreading-multiprocessing
    session = boto3.session.Session()
    s3_client = session.client(
        "s3",
        aws_access_key_id=aws_access_key,
        aws_secret_access_key=aws_secret_access_key,
    )
    self.client = s3_client