使用 boto3 S3 上传 try/catch 的错误代码

问题描述

我有一个函数,用于使用 boto3文件/文件上传到 S3。我想要一个 try/catch 来处理上传失败的错误。:

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

我认为这是我正在寻找的错误代码。我检查了所有可能的:

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']

鉴于此,我不确定在我的情况下该列表中的内容以及如何将其添加到我现在拥有的功能中。任何帮助表示赞赏!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)