Skip to content

Commit

Permalink
feat: fix s3 get object
Browse files Browse the repository at this point in the history
  • Loading branch information
dipratap committed Aug 29, 2024
1 parent e6fd210 commit 6eefe49
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/utils/s3-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { ListObjectsV2Command } from '@aws-sdk/client-s3';
import { GetObjectCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';

export async function getObjectKeysUsingPrefix(s3Client, bucketName, prefix, log) {
const objectKeys = [];
Expand All @@ -31,12 +31,12 @@ export async function getObjectKeysUsingPrefix(s3Client, bucketName, prefix, log
}

export async function getObjectFromKey(s3Client, bucketName, key, log) {
const params = {
const command = new GetObjectCommand({
Bucket: bucketName,
Key: key,
};
});
try {
return await s3Client.getObject(params).promise();
return await s3Client.send(command);
} catch (err) {
log.error(`Error while fetching S3 object from bucket ${bucketName} using key ${key}`, err);
return null;
Expand Down
76 changes: 36 additions & 40 deletions test/audits/metatags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
notFound,
internalServerError,
} from '@adobe/spacecat-shared-http-utils';
import { ListObjectsV2Command } from '@aws-sdk/client-s3';
import { GetObjectCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
import {
TITLE,
DESCRIPTION,
Expand Down Expand Up @@ -250,24 +250,23 @@ describe('Meta Tags', () => {
],
});

s3ClientStub.getObject.withArgs({
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page1.json',
}).returns({
promise: sinon.stub().resolves({
s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page1.json',
}))).returns({
Body: {
tags: {
title: 'Test Page',
description: '',
},
},
}),
});
s3ClientStub.getObject.withArgs({
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page2.json',
}).returns({
promise: sinon.stub().resolves({
});
s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page2.json',
}))).returns({
Body: {
tags: {
title: 'Test Page',
Expand All @@ -276,8 +275,7 @@ describe('Meta Tags', () => {
],
},
},
}),
});
});
const addAuditStub = sinon.stub().resolves();
dataAccessStub.addAudit = addAuditStub;

Expand Down Expand Up @@ -369,11 +367,12 @@ describe('Meta Tags', () => {
],
});

s3ClientStub.getObject.withArgs({
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page1.json',
}).returns({
promise: sinon.stub().resolves({
s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page1.json',
}))).returns({

Body: {
tags: {
title: 'This is an SEO optimal page1 valid title.',
Expand All @@ -384,13 +383,12 @@ describe('Meta Tags', () => {
],
},
},
}),
});
s3ClientStub.getObject.withArgs({
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page2.json',
}).returns({
promise: sinon.stub().resolves({
});
s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page2.json',
}))).returns({
Body: {
tags: {
title: 'This is a SEO wise optimised page2 title.',
Expand All @@ -400,8 +398,7 @@ describe('Meta Tags', () => {
],
},
},
}),
});
});
const addAuditStub = sinon.stub().resolves();
dataAccessStub.addAudit = addAuditStub;

Expand Down Expand Up @@ -475,15 +472,14 @@ describe('Meta Tags', () => {
],
});

s3ClientStub.getObject.withArgs({
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page1.json',
}).returns({
promise: sinon.stub().resolves({
s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page1.json',
}))).returns({
Body: {
},
}),
});
});
const addAuditStub = sinon.stub().resolves();
dataAccessStub.addAudit = addAuditStub;

Expand Down Expand Up @@ -517,13 +513,13 @@ describe('Meta Tags', () => {
],
});

s3ClientStub.getObject.returns({
promise: sinon.stub().resolves({
s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand))
.returns({
Body: {
tags: 5,
},
}),
});
});
const result = await auditMetaTags(message, context);

expect(JSON.stringify(result)).to.equal(JSON.stringify(notFound('Site tags data not available')));
Expand Down
12 changes: 4 additions & 8 deletions test/utils/s3-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ describe('S3 Utility Functions', () => {
const expectedObject = { Body: 'file contents' };

const s3ClientMock = {
getObject: () => ({
promise: async () => expectedObject,
}),
send: () => expectedObject,
};

const result = await getObjectFromKey(s3ClientMock, bucketName, key, logMock);
Expand All @@ -92,11 +90,9 @@ describe('S3 Utility Functions', () => {
const key = 'test-key';

const s3ClientMock = {
getObject: () => ({
promise: async () => {
throw new Error('S3 getObject error');
},
}),
send: () => {
throw new Error('S3 getObject error');
},
};
const logMock2 = {
error: (msg, err) => {
Expand Down

0 comments on commit 6eefe49

Please sign in to comment.