Skip to content

Commit

Permalink
Upload a stream
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Sep 23, 2024
1 parent c9b1418 commit d57f904
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
8 changes: 5 additions & 3 deletions s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)) {
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
13 changes: 11 additions & 2 deletions test/live.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down

0 comments on commit d57f904

Please sign in to comment.