Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is boto3.Bucket.upload_file blocking or non-blocking?

Is boto3.Bucket.upload_file blocking or non-blocking?

i.e. if I were to run the following

bucket = session.Bucket(bucket_name)
bucket.upload_file(Key=s3_key, Filename=source_path)
os.remove(source_path)

Do I have a race condition, depending on the size of the file? Or is upload guaranteed to complete before file deletion?

like image 875
Daniel Kats Avatar asked Jun 02 '16 22:06

Daniel Kats


People also ask

What is Boto3 bucket?

An Amazon S3 bucket is a storage location to hold files. S3 files are referred to as objects. This section describes how to use the AWS SDK for Python to perform common operations on S3 buckets.

What is Boto3 client (' S3 ')?

​Boto3 is the official AWS SDK for Python, used to create, configure, and manage AWS services. The following are examples of defining a resource/client in boto3 for the Weka S3 service, managing credentials, and pre-signed URLs, generating secure temporary tokens, and using those to run S3 API calls.

Does Boto3 use https?

The boto3 client includes a use_ssl parameter: use_ssl (boolean) -- Whether or not to use SSL. By default, SSL is used.

What is Boto3 in AWS?

The AWS SDK for Python (Boto3) provides a Python API for AWS infrastructure services. Using the SDK for Python, you can build applications on top of Amazon S3, Amazon EC2, Amazon DynamoDB, and more.


4 Answers

The current boto3 upload_file is blocking. As mootmoot said, you should definitely implement some error handling to be safe if you delete the file.

like image 124
Jordon Phillips Avatar answered Oct 05 '22 09:10

Jordon Phillips


Whether blocking or unblocking, you SHOULD NOT rely on the API alone when things went bad. You MUST add exception handling if the upload fail in the middle for any reason(e.g. admin decide to restart the router when you doing the upload).

bucket = session.Bucket(bucket_name)
try :
  bucket.upload_file(Key=s3_key, Filename=source_path)
  os.remove(source_path)
except : 
  raise

Another good practice to upload file to S3 is adding additional Metadata.

bucket.upload_file(
     Key=s3_key, 
     Filename=source_path, 
     extra_args={'Metadata': {'source_path': source_path}}
) 

Adding event to S3 Bucket to act on success PUT action also let you create cleanup process if there is success upload but failure on local file removal.(imagine the file is locked or the file is given Read-only access).

like image 36
mootmoot Avatar answered Oct 05 '22 11:10

mootmoot


Boto3 does not have support for async calls, so the function is blocking.

See conversations regarding async + boto3 here:

https://github.com/boto/boto3/issues/648

https://github.com/boto/boto3/issues/746

like image 30
Christopher Peter Avatar answered Oct 05 '22 09:10

Christopher Peter


I've made an asynchronous object to upload to S3 and download on your computer. To be sure that in your case for example, file will be deleted after upload, you can use the callback: on_success.

Check it out: https://gist.github.com/fherbine/0d4aa473e5cc2c5f6f8a0e1b35a62625

There's still enhancements to make, but it's working.

like image 31
Félix Herbinet Avatar answered Oct 05 '22 09:10

Félix Herbinet