Error code for try/catch with boto3 S3 upload

Viewed 790

I have a function that I use to upload files/folders to S3 using boto3. I want to have a try/catch that handles errors if something fails to upload.:

import os
import boto3
import botocore

def upload_files(key, secret, bucket, s3_path, source_path):

    session = boto3.Session(
        aws_access_key_id=key,
        aws_secret_access_key=secret,
        region_name='us-east-1'
    )

    s3 = session.resource('s3')
    bucket = s3.Bucket(bucket)
    
    try:
        for subdir, dirs, files in os.walk(source_path):
            for file in files:
                full_path = os.path.join(subdir, file)
                with open(full_path, 'rb') as data:
                    bucket.put_object(Key=s3_path + full_path, Body=data, ServerSideEncryption='AES256')
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            print("The object does not exist.")
        else:
            raise

This I believe is the wrong error code that I'm looking for. I checked all the possible ones:

import botocore
import boto3
[e for e in dir(botocore.exceptions) if e.endswith('Error')]

['AliasConflictParameterError',
 'ApiVersionNotFoundError',
 'BaseEndpointResolverError',
 'BotoCoreError',
 'CapacityNotAvailableError',
 'ChecksumError',
 'ClientError',
 'ConfigParseError',
 'ConnectTimeoutError',
 'ConnectionClosedError',
 'ConnectionError',
 'CredentialRetrievalError',
 'DataNotFoundError',
 'EndpointConnectionError',
 'EventStreamError',
 'HTTPClientError',
 'IncompleteReadError',
 'InfiniteLoopConfigError',
 'InvalidConfigError',
 'InvalidDNSNameError',
 'InvalidEndpointDiscoveryConfigurationError',
 'InvalidExpressionError',
 'InvalidHostLabelError',
 'InvalidIMDSEndpointError',
 'InvalidMaxRetryAttemptsError',
 'InvalidProxiesConfigError',
 'InvalidRegionError',
 'InvalidRetryConfigurationError',
 'InvalidRetryModeError',
 'InvalidS3AddressingStyleError',
 'InvalidS3UsEast1RegionalEndpointConfigError',
 'InvalidSTSRegionalEndpointsConfigError',
 'MD5UnavailableError',
 'MetadataRetrievalError',
 'MissingParametersError',
 'MissingServiceIdError',
 'NoCredentialsError',
 'NoRegionError',
 'OperationNotPageableError',
 'PaginationError',
 'ParamValidationError',
 'PartialCredentialsError',
 'ProxyConnectionError',
 'RangeError',
 'ReadTimeoutError',
 'RefreshWithMFAUnsupportedError',
 'SSLError',
 'SSOError',
 'SSOTokenLoadError',
 'ServiceNotInRegionError',
 'StubAssertionError',
 'StubResponseError',
 'UnStubbedResponseError',
 'UnauthorizedSSOTokenError',
 'UndefinedModelAttributeError',
 'UnknownClientMethodError',
 'UnknownCredentialError',
 'UnknownEndpointError',
 'UnknownKeyError',
 'UnknownParameterError',
 'UnknownServiceError',
 'UnknownSignatureVersionError',
 'UnseekableStreamError',
 'UnsupportedOutpostResourceError',
 'UnsupportedS3AccesspointConfigurationError',
 'UnsupportedS3ArnError',
 'UnsupportedS3ControlArnError',
 'UnsupportedS3ControlConfigurationError',
 'UnsupportedSignatureVersionError',
 'ValidationError',
 'WaiterConfigError',
 'WaiterError']

Given this, I'm not sure what to use from that list in my case and how to add that to the function that I have right now. Any help is appreciated!

0 Answers
Related