Last active
November 10, 2021 16:26
-
-
Save crabba/256278efc17cd19e2cb772b5cf01fe7d to your computer and use it in GitHub Desktop.
S3 test if an object exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
import boto3 | |
import sys | |
s3client = boto3.client('s3') | |
def s3_object_exists(bucket, key): | |
ret = False | |
try: | |
metadata = s3client.head_object(Bucket=bucket, Key=key) | |
ret = True | |
except s3client.exceptions.NoSuchKey as e: | |
print(f"nosuchkey: {e}") | |
pass | |
except s3client.exceptions.NoSuchBucket as e: | |
print(f"nosuchbucket: {e}") | |
pass | |
except Exception as e: | |
print(f"Exception {type(e).__name__}: {e}") | |
return ret | |
(bucket, key) = sys.argv[1:3] | |
ret = s3_object_exists(bucket, key) | |
print(f"{ret}: {bucket}, {key}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment