boto3 file_upload does it check if file exists

Viewed 27362

I was looking through the boto3 documentation and could not find if it natively supports a check to see if the file already exists in s3 and if not do not try and re-upload.

Here's what I have working:

import boto3

s3_client = boto3.client('s3')
s3_bucket = 'bucketName'
s3_folder = 'folder1234/'
temp_log_dir = "tempLogs/"


s3_client.upload_file(temp_log_dir + file_name, s3_bucket, s3_folder + file_name)

What I'm noticing is that if the file exits in S3 already , the .upload_file() from boto3 still transfers the file. I did this check by looking at iftop.

Is there some arg i'm missing to disable "auto overwrite if file exists"?

Thanks

edit trying the head method

s3 = boto3.resource('s3')
file_name = 'somelogfile.gz'

try:
    s3.Object(s3_bucket+ s3_folder+s3_filename).load()
    print 'success'
except botocore.exceptions.ClientError as e:
    print e
2 Answers

You can use this code

import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
key = 'dootdoot.jpg'
objs = list(bucket.objects.filter(Prefix=key))
if len(objs) > 0 and objs[0].key == key:
 print("Exists!")
else:
 print("Doesn't exist")
Related