johannesboyne / gofakes3

A simple fake AWS S3 object storage (used for local test-runs against AWS S3 APIs)
MIT License
361 stars 84 forks source link

Support multipart uploads from browser #43

Closed korya closed 4 years ago

korya commented 4 years ago

This PR adds support for browser multipart uploads using Temporary Security Credentials.

I am uploading to S3 from the browser using the official AWS JS SDK, https://github.com/aws/aws-sdk-js, and temporary Temporary Security Credentials issued by the back-end, https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html. The code sample is below:

export function basicUploadToS3(
  uploadPermit,
  file,
  onProgress,
  uploadParams = { queueSize: 4, partSize: 10 * 1024 * 1024 },
) {
  const {
    region, bucket, key,
    accessKeyId, secretAccessKey, sessionToken, // issued by the back-end
  } = uploadPermit;

  return new Promise((resolve, reject) => {
    new S3({
      apiVersion: '2006-03-01',
      endpoint: config.upload.s3endpoint,
      correctClockSkew: true,
      maxRetries: 3,
      accessKeyId,
      secretAccessKey,
      sessionToken,
      region,
    }).upload({
      Bucket: bucket,
      Key: key,
      ACL: 'bucket-owner-full-control',
      ContentType: file.type,
      Body: file,
    }, uploadParams, (err, data) => {
      return err ? reject(err) : resolve(data);
    }).on('httpUploadProgress', e => {
      onProgress(e.loaded / e.total);
    });
  });
}

This PR makes it possible.