'What is the Difference between file_upload() and put_object() when uploading files to S3 using boto3

I'm using boto3 and trying to upload files. It will be helpful if anyone will explain exact difference between file_upload() and put_object() s3 bucket methods in boto3 ?

  • Is there any performance difference?
  • Does anyone among these handles multipart upload feature in behind the scenes?
  • What are the best use cases for both?


Solution 1:[1]

One other difference I feel might be worth noticing is upload_file() API allows you to track upload using callback function. You can check about it here.

Also as already mentioned by boto's creater @garnaat that upload_file() uses multipart behind the scenes so its not straight forward to check end to end file integrity (there exists a way) but put_object() uploads whole file at one shot (capped at 5GB though) making it easier to check integrity by passing Content-MD5 which is already provided as a parameter in put_object() API.

Solution 2:[2]

One other thing to mention is that put_object() requires a file object whereas upload_file() requires the path of the file to upload. For example, if I have a json file already stored locally then I would use upload_file(Filename='/tmp/my_file.json', Bucket=my_bucket, Key='my_file.json').

Whereas if I had a dict within in my job, I could transform the dict into json and use put_object() like so:

records_to_update = {'Name': 'Sally'}
records_to_update_json = json.dumps(records_to_update, default=str)
put_object(Body=records_to_update_json, Bucket=my_bucket, Key='my_records')

Solution 3:[3]

The react setState hook overwrites the value of the state variable present inside of it so you need to make sure that e.target.value returns an array and not 1 value.

If it does return 1 value then you can simply do this:

onChange={(e) => {
    const arr = [...hrDtlsData]; // assuming you want to save it in index 0
    arr[0] = e.target.value;
    setHRDtlsData(arr)
}}

Edit: Keeping in mind your comment, you can create a function in which the new value and the index of the input are passed and then you update the state variable.

onChange={(e) => {
    updateValues(e.target.value, indexPos) // check the event object if it has a variable keeping track of index of the input
}}

And the function:

function updateValues(newVal, index) {
    const arr = [...hrDtlsData];
    arr[index] = e.target.value;
    setHRDtlsData(arr)
}

Do let me know if this worked!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 deesolie
Solution 3