Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between uploading a file to S3 using boto3.resource.put_object() and boto3.s3.transfer.upload_file()

While I was referring to the sample codes to upload a file to S3 I found the following two ways.

Using boto3.resource.put_object():

s3_resource = boto3.resource('s3')
s3_resource.put_object(Bucket = BUCKET,  Key = 'test', Body= b'some data')

Using boto3.s3.transfer.upload_file():

client = boto3.client('s3')
transfer = S3Transfer(client) 
transfer.upload_file('/my_file', BUCKET, 'test') 

I could not figure out the difference between the two ways. Are there any advantages of using one over another in any specific use cases. Can anyone please elaborate. Thank you.

like image 596
Mandeep Vratesh Avatar asked Sep 30 '17 20:09

Mandeep Vratesh


People also ask

What is the difference between Boto3 client and resource?

00:00 Boto3's primary function is to make AWS API calls for you. It extracts these APIs in two main ways: clients and resources. Clients give you low-level service access, while resources provide an object-oriented way of working with these services.

What is Boto3 resource (' S3 ')?

Resources are a higher-level abstraction compared to clients. They are generated from a JSON resource description that is present in the boto library itself. E.g. this is the resource definition for S3.


2 Answers

  • The put_object method maps directly to the low-level S3 API request. It will attempt to send the entire body in one request. No multipart support boto3 docs
  • The upload_file method is handled by the S3 Transfer Manager, this means that it will automatically handle multipart uploads behind the scenes for you, if necessary.
    • Automatically switching to multipart transfers when a file is over a specific size threshold
    • Uploading/downloading a file in parallel
    • Progress callbacks to monitor transfers
    • Retries. While botocore handles retries for streaming uploads, it is not possible for it to handle retries for streaming downloads. This module handles retries for both cases so you don't need to implement any retry logic yourself.
    • This module has a reasonable set of defaults. It also allows you to configure many aspects of the transfer process including: Multipart threshold size, Max parallel downloads, Socket timeouts, Retry amounts.boto3 docs
like image 128
amittn Avatar answered Nov 14 '22 23:11

amittn


There is likely no difference - boto3 sometimes has multiple ways to achieve the same thing. See http://boto3.readthedocs.io/en/latest/guide/s3.html#uploads for more details on uploading files.

like image 23
Snorri Sturluson Avatar answered Nov 15 '22 00:11

Snorri Sturluson