Skip to content

Commit 55f0500

Browse files
committed
adding query for headObject query
1 parent e45aebd commit 55f0500

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/queries/s3.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,32 @@ export const pageObjectsFromS3 = ({
193193
.map(listObjects)
194194
.parallel(parallel);
195195
};
196+
197+
export const headS3Object= ({
198+
id: pipelineId,
199+
debug = d('s3'),
200+
bucketName = process.env.BUCKET_NAME,
201+
headRequestField = 'headRequest',
202+
headResponseField = 'headResponse',
203+
parallel = Number(process.env.S3_PARALLEL) || Number(process.env.PARALLEL) || 8,
204+
step = 'get',
205+
...opt
206+
} = {}) => {
207+
const connector = new Connector({
208+
pipelineId, debug, bucketName, ...opt,
209+
});
210+
211+
const headObject = (uow) => {
212+
if (!uow[headRequestField]) return _(Promise.resolve(uow));
213+
214+
const p = () => connector.headObject(uow[headRequestField], uow)
215+
.then((headResponse) => ({ ...uow, [headResponseField]: headResponse }))
216+
.catch(rejectWithFault(uow));
217+
218+
return _(uow.metrics?.w(p, step) || p()); // wrap promise in a stream
219+
};
220+
221+
return (s) => s
222+
.map(headObject)
223+
.parallel(parallel);
224+
};

test/unit/queries/s3.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
toGetObjectRequest,
1313
toGetObjectRequest2,
1414
getObjectFromS3AsStream,
15+
headS3Object,
1516
} from '../../../src/queries/s3';
1617

1718
import Connector from '../../../src/connectors/s3';
@@ -329,6 +330,68 @@ describe('queries/s3.js', () => {
329330
})
330331
.done(done);
331332
});
333+
it('should head object', (done) => {
334+
const stub = sinon.stub(Connector.prototype, 'headObject').resolves({
335+
Metadata: {
336+
testkey: '1',
337+
},
338+
});
339+
340+
const uows = [{
341+
headRequest: {
342+
Key: 'k1',
343+
},
344+
}];
345+
346+
_(uows)
347+
.through(headS3Object())
348+
.collect()
349+
.tap((collected) => {
350+
console.log(JSON.stringify(collected, null, 2));
351+
352+
expect(stub).to.have.been.calledWith({
353+
Key: 'k1',
354+
});
355+
356+
expect(collected.length).to.equal(1);
357+
expect(collected[0]).to.deep.equal({
358+
headRequest: {
359+
Key: 'k1',
360+
},
361+
headResponse: {
362+
Metadata: {
363+
testkey: '1',
364+
},
365+
},
366+
});
367+
})
368+
.done(done);
369+
});
370+
it('should head object missing headRequestField', (done) => {
371+
const stub = sinon.stub(Connector.prototype, 'headObject').resolves({
372+
Metadata: {
373+
testkey: '1',
374+
},
375+
});
376+
377+
const uows = [{
378+
// headRequest: {
379+
// Key: 'k1',
380+
// },
381+
}];
382+
383+
_(uows)
384+
.through(headS3Object())
385+
.collect()
386+
.tap((collected) => {
387+
// console.log(JSON.stringify(collected, null, 2));
388+
389+
expect(collected[0]).to.deep.equal({});
390+
391+
expect(collected.length).to.equal(1);
392+
})
393+
.done(done);
394+
});
332395
});
333396

334397
const GET_RESPONSE = {

0 commit comments

Comments
 (0)