def key_existing(client, bucket, key):
"""return a tuple of (
key's size if it exists or 0,
S3 key metadata
)
If the file doesn't exist, return None for the metadata.
"""
# Return 0 if the key can't be found so the memoize cache can cope
try:
response = client.head_object(
Bucket=bucket,
Key=key,
)
return response['ContentLength'], response.get('Metadata')
except ClientError as exception:
if exception.response['Error']['Code'] == '404':
return 0, None
raise
except (ReadTimeout, socket.timeout) as exception:
logger.info(
f'ReadTimeout trying to list_objects_v2 for {bucket}:'
f'{key} ({exception})'
)
return 0, None
评论列表
文章目录