如何解决使用 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 举报,一经查实,本站将立刻删除。