diff --git a/lib/Open/index.js b/lib/Open/index.js index 83c349b..84d6343 100644 --- a/lib/Open/index.js +++ b/lib/Open/index.js @@ -93,7 +93,43 @@ module.exports = { return directory(source, options); }, + s3_v3: function (client, params, options) { + const { GetObjectCommand, HeadObjectCommand } = require('@aws-sdk/client-s3'); + const source = { + size: async () => { + const head = await client.send( + new HeadObjectCommand({ + Bucket: params.Bucket, + Key: params.Key, + }) + ); + + return head.ContentLength || 0; + }, + stream: (offset, length) => { + const stream = Stream.PassThrough(); + const end = length ? offset + length : ""; + client + .send( + new GetObjectCommand({ + Bucket: params.Bucket, + Key: params.Key, + Range: `bytes=${offset}-${end}`, + }) + ) + .then((response) => { + response.Body.pipe(stream); + }) + .catch((error) => { + stream.emit("error", error); + }); + return stream; + }, + }; + + return directory(source, options); + }, custom: function(source, options) { return directory(source, options); } diff --git a/package.json b/package.json index 528f510..ea7eba0 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "node-int64": "^0.4.0" }, "devDependencies": { + "@aws-sdk/client-s3": "^3.0.0", "@eslint/js": "^9.2.0", "aws-sdk": "^2.1636.0", "dirdiff": ">= 0.0.1 < 1",