Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
add aws v2 lib and rework upload to use it
Browse files Browse the repository at this point in the history
Multipart upload in v3 doesn't work with UoM storage bu V2 does...
  • Loading branch information
marcolarosa committed Mar 25, 2021
1 parent fad7b93 commit d3503de
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 18 deletions.
68 changes: 57 additions & 11 deletions lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ const {
createWriteStream,
readdir,
ensureDir,
stat,
} = require("fs-extra");
const AWS = require("aws-sdk");
const path = require("path");
const MB = 1024 * 1024;

// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
const maxFileNameLength = 1024;
Expand Down Expand Up @@ -69,6 +72,7 @@ class Bucket {
}) {
let configuration = {
forcePathStyle,
s3ForcePathStyle: forcePathStyle,
};

if (!bucket) {
Expand All @@ -84,6 +88,7 @@ class Bucket {
}
if (region) configuration.region = region;
this.client = new S3Client(configuration);
this.configuration = configuration;
}

async stat({ path }) {
Expand Down Expand Up @@ -137,23 +142,64 @@ class Bucket {
);
}

const uploader = new Upload({
client: this.client,
queueSize: 4, // optional concurrency configuration
leavePartsOnError: false, // optional manually handle dropped parts
let chunkSize = 5 * MB;
if (localPath) {
let fileStat = await stat(localPath);
const desiredChunkSize = await Math.ceil(fileStat.size / 10000);
// at least 5MB per request, at most 10k requests
const minChunkSize = Math.max(5 * MB, Math.ceil(fileStat.size / 10000));
chunkSize = Math.max(desiredChunkSize, minChunkSize);
}

let uploader = V2MultipartUpload.bind(this);
let response = await uploader({
params: uploadParams,
partSize: chunkSize,
});
return response;

async function V3SinglePartUpload({ params }) {
// Straight up V3 upload - not multipart
const command = new PutObjectCommand(uploadParams);
let response = await this.client.send(command);
return response.$metadata;
}

async function V3MultipartUpload({ params, partSize }) {
// V3 multipart uploader - doesn't work
// https://github.com/aws/aws-sdk-js-v3/issues/1814
const uploader = new Upload({
client: this.client,
partSize,
queueSize: 4, // optional concurrency configuration
leavePartsOnError: false, // optional manually handle dropped parts
params,
});

// uploader.on("httpUploadProgress", (progress) => {
// console.log(progress);
// });
uploader.on("httpUploadProgress", (progress) => {
console.log(progress);
});

const response = await uploader.done();
const response = await uploader.done();
return response.$metadata;
}

// const command = new PutObjectCommand(uploadParams);
// let response = await this.client.send(command);
async function V2MultipartUpload({ params, partSize }) {
// V2 multipart uploader
AWS.config = this.configuration;
const uploader = new AWS.S3.ManagedUpload({
partSize,
params,
});
// uploader.on("httpUploadProgress", (progress) => {
// console.log(progress);
// });

return response.$metadata;
let response = await uploader.promise();
return {
httpStatusCode: 200,
};
}
}

async download({ target, localPath }) {
Expand Down
173 changes: 166 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@aws-sdk/client-s3": "^3.9.0",
"@aws-sdk/lib-storage": "^3.9.0",
"@aws-sdk/s3-request-presigner": "^3.9.0",
"aws-sdk": "^2.872.0",
"fs-extra": "^9.1.0",
"hasha": "^5.2.2",
"lodash": "^4.17.21",
Expand Down

0 comments on commit d3503de

Please sign in to comment.