Difference between upload() and putObject() for uploading a file to S3?

前端 未结 3 1509
梦如初夏
梦如初夏 2020-12-07 22:35

In the aws-sdk\'s S3 class, what is the difference between upload() and putObject()? They seem to do the same thing. Why might I prefer one over the other?

相关标签:
3条回答
  • 2020-12-07 22:57

    The difference between using AWS SDK upload() and putObject() param is as below:

    • If the reported MD5 upon upload completion does not match, it retries.
    • If the file size is large enough, it uses multipart upload to upload parts in parallel.
    • Retry based on the client's retry settings.
    • You can use for Progress reporting.
    • Sets the ContentType based on file extension if you do not provide it.
    0 讨论(0)
  • 2020-12-07 23:06

    upload() allows you to control how your object is uploaded. For example you can define concurrency and part size.

    From their docs: Uploads an arbitrarily sized buffer, blob, or stream, using intelligent concurrent handling of parts if the payload is large enough.

    One specific benefit I've discovered is that upload() will accept a stream without a content length defined whereas putObject() does not.

    This was useful as I had an API endpoint that allowed users to upload a file. The framework delivered the file to my controller in the form of a readable stream without a content length. Instead of having to measure the file size, all I had to do was pass it straight through to the upload() call.

    0 讨论(0)
  • 2020-12-07 23:08

    When looking for the same information, I came across: https://aws.amazon.com/blogs/developer/uploading-files-to-amazon-s3/

    This source is a little dated (referencing instead upload_file() and put() -- or maybe it is the Ruby SDK?), but it looks like the putObject() is intended for smaller objects than the upload().

    It recommends upload() and specifies why:

    This is the recommended method of using the SDK to upload files to a bucket. Using this approach has the following benefits:

    • Manages multipart uploads for objects larger than 15MB.
    • Correctly opens files in binary mode to avoid encoding issues.
    • Uses multiple threads for uploading parts of large objects in parallel.

    Then covers the putObject() operation:

    For smaller objects, you may choose to use #put instead.

    EDIT: I was having problems with the .abort() operation on my .upload() and found this helpful: abort/stop amazon aws s3 upload, aws sdk javascript

    Now my various other events from https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Request.html are firing as well! With .upload() I only had 'httpUploadProgress'.

    0 讨论(0)
提交回复
热议问题