matpie
matpie

Reputation: 17522

How do I find the total size of a stream that I just uploaded to S3?

On a Node.js server I am uploading a stream to S3. Here is a simplified example:

import S3 from "aws-sdk/clients/s3"

const s3 = new S3({ accessKeyId, secretAccessKey, region })

async function uploadStream(key, stream) {
  const upload = s3.upload({
    Body: stream,
    Bucket: clientFiles,
    Key: key,
  })

  const result = await upload.promise()
  return result // { Bucket, ETag, Key, Location }
}

Since I am using a stream, I don't know the total size when the upload is initiated, and the upload result doesn't give me anything more than Bucket, ETag, Key, & Location values.

How can I get the total size of the uploaded stream?

Upvotes: 1

Views: 199

Answers (1)

RyanKim
RyanKim

Reputation: 1795

How about use ManagedUpload and httpUploadProgress?
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3/ManagedUpload.html#httpUploadProgress-event

async function uploadStream(key, stream) {
  const upload = new AWS.S3.ManagedUpload({
    params: {
      Body: stream,
      Bucket: clientFiles,
      Key: key
    }
  });
  upload.on("httpUploadProgress", function(progress) {
    console.log(progress.total);
  });
  ...

Upvotes: 1

Related Questions