Support metadata when uploading objects

Add contentType and contentEncoding options to sendStream(). These
options will set the corresponding metadata on the object.

This changes the API. The fourth argument to sendStream() used to be the
source md5. Now, it's an options object and the source md5 is a property
of that object.
This commit is contained in:
Eric Mc Sween
2020-07-08 08:13:53 -04:00
parent cd8c8b2b7f
commit 523ff9c4cd
10 changed files with 95 additions and 41 deletions
@@ -113,7 +113,9 @@ describe('FSPersistorTests', function () {
describe('when the md5 hash does not match', function () {
it('should return a write error', async function () {
await expect(
FSPersistor.sendStream(location, files[0], remoteStream, '00000000')
FSPersistor.sendStream(location, files[0], remoteStream, {
sourceMd5: '00000000'
})
)
.to.eventually.be.rejected.and.be.an.instanceOf(Errors.WriteError)
.and.have.property('message', 'md5 hash mismatch')
@@ -121,12 +123,9 @@ describe('FSPersistorTests', function () {
it('deletes the copied file', async function () {
try {
await FSPersistor.sendStream(
location,
files[0],
remoteStream,
'00000000'
)
await FSPersistor.sendStream(location, files[0], remoteStream, {
sourceMd5: '00000000'
})
} catch (_) {}
expect(fs.unlink).to.have.been.calledWith(
`${location}/${filteredFilenames[0]}`
@@ -361,12 +361,9 @@ describe('GcsPersistorTests', function () {
describe('when a hash is supplied', function () {
beforeEach(async function () {
return GcsPersistor.sendStream(
bucket,
key,
ReadStream,
'aaaaaaaabbbbbbbbaaaaaaaabbbbbbbb'
)
return GcsPersistor.sendStream(bucket, key, ReadStream, {
sourceMd5: 'aaaaaaaabbbbbbbbaaaaaaaabbbbbbbb'
})
})
it('should not calculate the md5 hash of the file', function () {
@@ -388,6 +385,25 @@ describe('GcsPersistorTests', function () {
})
})
describe('when metadata is supplied', function () {
const contentType = 'text/csv'
const contentEncoding = 'gzip'
beforeEach(async function () {
return GcsPersistor.sendStream(bucket, key, ReadStream, {
contentType,
contentEncoding
})
})
it('should send the metadata to GCS', function () {
expect(GcsFile.createWriteStream).to.have.been.calledWith({
metadata: { contentType, contentEncoding },
resumable: false
})
})
})
describe('when the upload fails', function () {
let error
beforeEach(async function () {
@@ -195,11 +195,13 @@ describe('MigrationPersistorTests', function () {
})
it('should send a stream to the primary', function () {
expect(primaryPersistor.sendStream).to.have.been.calledWithExactly(
expect(
primaryPersistor.sendStream
).to.have.been.calledWithExactly(
bucket,
key,
sinon.match.instanceOf(Stream.PassThrough),
md5
{ sourceMd5: md5 }
)
})
@@ -474,11 +476,13 @@ describe('MigrationPersistorTests', function () {
})
it('should send the file to the primary', function () {
expect(primaryPersistor.sendStream).to.have.been.calledWithExactly(
expect(
primaryPersistor.sendStream
).to.have.been.calledWithExactly(
bucket,
destKey,
sinon.match.instanceOf(Stream.PassThrough),
md5
{ sourceMd5: md5 }
)
})
})
@@ -448,14 +448,11 @@ describe('S3PersistorTests', function () {
})
})
describe('when a hash is supploed', function () {
describe('when a hash is supplied', function () {
beforeEach(async function () {
return S3Persistor.sendStream(
bucket,
key,
ReadStream,
'aaaaaaaabbbbbbbbaaaaaaaabbbbbbbb'
)
return S3Persistor.sendStream(bucket, key, ReadStream, {
sourceMd5: 'aaaaaaaabbbbbbbbaaaaaaaabbbbbbbb'
})
})
it('sends the hash in base64', function () {
@@ -468,6 +465,28 @@ describe('S3PersistorTests', function () {
})
})
describe('when metadata is supplied', function () {
const contentType = 'text/csv'
const contentEncoding = 'gzip'
beforeEach(async function () {
return S3Persistor.sendStream(bucket, key, ReadStream, {
contentType,
contentEncoding
})
})
it('sends the metadata to S3', function () {
expect(S3Client.upload).to.have.been.calledWith({
Bucket: bucket,
Key: key,
Body: sinon.match.instanceOf(Transform),
ContentType: contentType,
ContentEncoding: contentEncoding
})
})
})
describe('when the upload fails', function () {
let error
beforeEach(async function () {