From d57f904147655d654a19e6228e1c79ab1f5c3d2f Mon Sep 17 00:00:00 2001 From: Lauri Rooden Date: Mon, 23 Sep 2024 11:19:46 +0300 Subject: [PATCH] Upload a stream --- s3.js | 8 +++++--- test/index.js | 4 ++++ test/live.js | 13 +++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/s3.js b/s3.js index 8a465b5..929ec66 100644 --- a/s3.js +++ b/s3.js @@ -103,12 +103,12 @@ function req(method, data, path, opts, next) { } var s3 = this , longDate = awsDate() - , contentHash = data ? hash(data) : "UNSIGNED-PAYLOAD" + , contentHash = data && !isStream(data) ? hash(data) : "UNSIGNED-PAYLOAD" , headers = { "x-amz-date": longDate, "x-amz-content-sha256": contentHash } - if (data) { + if (data && data.length) { headers["Content-Length"] = data.length } var signed = awsSig(s3, method, path, opts, "", headers, longDate, contentHash) @@ -118,7 +118,9 @@ function req(method, data, path, opts, next) { if (!isFn(next)) return new Promise(makeReq) makeReq(next.bind(null, null), next) function makeReq(resolve, reject) { - s3.client.request(signed.url, { method: method, headers: headers }, handle).end(data) + var req = s3.client.request(signed.url, { method: method, headers: headers }, handle) + if (isStream(data)) data.pipe(req) + else req.end(data) function handle(res) { res.on("error", reject) if (res.statusCode === 200 && isStream(next)) { diff --git a/test/index.js b/test/index.js index b91378a..5f774de 100644 --- a/test/index.js +++ b/test/index.js @@ -127,10 +127,14 @@ describe("S3 Mock", function() { it("should get a stream from bucket", function(assert, mock) { var s3client = mockedClient(mock, { bucket: "buck-1", userAgent: "Dummy/1.0" }) + , streamReadable = { pipe: mock.fn(), read: mock.fn("Hello") } , stream = { pipe: mock.fn(), write: mock.fn() } + + s3client.put("file1.txt", streamReadable) s3client.get("file1.txt", stream) mock.tick() + assert.equal(streamReadable.pipe.called, 1) assert.equal(stream.write.calls[0].args[0], "Hello") assert.end() }) diff --git a/test/live.js b/test/live.js index ba13e36..811c6e3 100644 --- a/test/live.js +++ b/test/live.js @@ -97,12 +97,21 @@ describe("S3 live on {0} {1}", [ }) }) - it("should stream a file from bucket", async function(assert) { + it("should stream a file", async function(assert) { assert.setTimeout(5000) - var name = "./" + fileName + var name = "./stream-" + fileName , writeTo = fs.createWriteStream(name) await s3client.get(fileName, writeTo) assert.equal(fs.readFileSync(name, "utf8"), content) + + var readFrom = fs.createReadStream(name) + , stat = fs.statSync(name) + readFrom.length = stat.size + + await s3client.put("streamed-" + fileName, readFrom) + + assert.equal(await s3client.get("streamed-" + fileName), content) + fs.unlinkSync(name) })