Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boto3 file_upload does it check if file exists

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
like image 986
chowpay Avatar asked Jul 07 '17 19:07

chowpay


2 Answers

You can test the existence of an object using s3_client.head_object() or s3_service.Object().load():

import boto3
from botocore.exceptions import ClientError

def check(s3_client, bucket, key):
    try:
        s3_client.head_object(Bucket=bucket, Key=key)
    except ClientError as e:
        return int(e.response['Error']['Code']) != 404
    return True

s3_client = boto3.client('s3')
print(check(s3_client, <bucket>, <key>))

With s3 service resource this would achieve the same:

def check(s3_service, bucket, key):
    try:
        s3_service.Object(bucket, key).load()
    except ClientError as e:
        return int(e.response['Error']['Code']) != 404
    return True

s3_service = boto3.resource(service_name='s3')
print(check(s3_service, <bucket>, <key>))
like image 163
AChampion Avatar answered Oct 16 '22 20:10

AChampion


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")
like image 20
Alex Montoya Avatar answered Oct 16 '22 20:10

Alex Montoya