Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support putObject to upload an empty file. #658

Merged
merged 7 commits into from
Jan 4, 2018
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/main/object-uploader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Minio Javascript Library for Amazon S3 Compatible Cloud Storage, (C) 2016 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,7 +22,7 @@ import * as querystring from 'querystring'
export default class ObjectUploader extends Transform {
constructor(client, bucketName, objectName, partSize, contentType, callback) {
super()

this.emptyStream = true
this.client = client
this.bucketName = bucketName
this.objectName = objectName
Expand Down Expand Up @@ -58,6 +58,7 @@ export default class ObjectUploader extends Transform {
}

_transform(chunk, encoding, callback) {
this.emptyStream = false
let method = 'PUT'
let headers = {
'Content-Length': chunk.length,
Expand Down Expand Up @@ -207,6 +208,41 @@ export default class ObjectUploader extends Transform {
}

_flush(callback) {
if (this.emptyStream) {
let method = 'PUT'
let headers = {
'Content-Length': 0,
'Content-Type': this.contentType
}
let options = {
method, headers,
query: '',
bucketName: this.bucketName,
objectName: this.objectName
}

this.client.makeRequest(options, '', 200, '', true, (err, response) => {
if (err) return callback(err)

let etag = response.headers.etag
if (etag) {
etag = etag.replace(/^"/, '').replace(/"$/, '')
}

// Ignore the 'data' event so that the stream closes. (nodejs stream requirement)
response.on('data', () => {})

// Give the etag back, we're done!
process.nextTick(() => {
this.callback(null, etag)
})

// Because we're sure the stream has ended, allow it to flush and end.
callback()
})

return
}
// If it has been uploaded in a single packet, we don't have to do anything.
if (this.id === null) {
return
Expand All @@ -227,5 +263,4 @@ export default class ObjectUploader extends Transform {
callback()
})
}

}