Skip to content

Commit 40632c8

Browse files
committed
review: move from v2 to v3 of AWS SDK
1 parent 4802a72 commit 40632c8

File tree

8 files changed

+59
-25
lines changed

8 files changed

+59
-25
lines changed

src/lib/aws-clients.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { S3 } from '@aws-sdk/client-s3'
2-
import { SNS } from '@aws-sdk/client-sns'
3-
import { SQS } from '@aws-sdk/client-sqs'
1+
import { S3Client } from '@aws-sdk/client-s3'
2+
import { SNSClient } from '@aws-sdk/client-sns'
3+
import { SQSClient } from '@aws-sdk/client-sqs'
44

55
const localStackEndpointEnvVar = 'LOCAL_STACK_ENDPOINT'
66

@@ -28,7 +28,7 @@ export const s3 = (options = {}) => {
2828

2929
const overrides = useLocalStack() ? localStackOverrides : {}
3030

31-
return new S3({
31+
return new S3Client({
3232
...overrides,
3333
...options
3434
})
@@ -37,7 +37,7 @@ export const s3 = (options = {}) => {
3737
export const sns = (options = {}) => {
3838
const overrides = useLocalStack() ? localStackParams() : {}
3939

40-
return new SNS({
40+
return new SNSClient({
4141
...overrides,
4242
...options
4343
})
@@ -46,7 +46,7 @@ export const sns = (options = {}) => {
4646
export const sqs = (options = {}) => {
4747
const overrides = useLocalStack() ? localStackParams() : {}
4848

49-
return new SQS({
49+
return new SQSClient({
5050
...overrides,
5151
...options
5252
})

src/lib/s3-utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { GetObjectCommand } from '@aws-sdk/client-s3'
12
import { s3 } from './aws-clients.js'
23
import logger from './logger.js'
34

45
const getObjectBody = async (s3Location) => {
56
try {
6-
const result = await s3().getObject({
7+
const command = new GetObjectCommand({
78
Bucket: s3Location.bucket,
89
Key: s3Location.key
910
})
11+
const result = await s3().send(command)
1012

1113
if (result.Body === undefined) {
1214
throw new Error(`Body of ${s3Location.url} is undefined`)

src/lib/s3-utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import { GetObjectCommand } from '@aws-sdk/client-s3'
12
import { s3 } from './aws-clients.js'
23

34
const getObjectBody = async (s3Location: {bucket: string, key: string}) => {
45
try {
5-
const result = await s3().getObject({
6+
const command = new GetObjectCommand({
67
Bucket: s3Location.bucket,
78
Key: s3Location.key
89
})
910

11+
const result = await s3().send(command)
12+
1013
if (result.Body === undefined) {
1114
throw new Error(`Body of ${s3Location.bucket}/${s3Location.key} is undefined`)
1215
}

src/lib/sns.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PublishCommand } from '@aws-sdk/client-sns'
12
import { sns } from './aws-clients.js'
23
import logger from './logger.js'
34
import { getBBox, getStartAndEndDates, isCollection, isItem } from './stac-utils.js'
@@ -86,11 +87,12 @@ const attrsFromPayload = function (payload) {
8687
export async function publishRecordToSns(topicArn, record, error) {
8788
const payload = { record, error }
8889
try {
89-
await sns().publish({
90+
const command = new PublishCommand({
9091
Message: JSON.stringify(payload),
9192
TopicArn: topicArn,
9293
MessageAttributes: attrsFromPayload(payload)
9394
})
95+
await sns().send(command)
9496
logger.info(`Wrote record ${record.id} to ${topicArn}`)
9597
} catch (err) {
9698
logger.error(`Failed to write record ${record.id} to ${topicArn}: ${err}`)

tests/helpers/ingest.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PublishCommand } from '@aws-sdk/client-sns'
2+
import { ReceiveMessageCommand } from '@aws-sdk/client-sqs'
13
import { sns, sqs } from '../../src/lib/aws-clients.js'
24
import { handler } from '../../src/lambdas/ingest/index.js'
35
import { sqsTriggerLambda } from './sqs.js'
@@ -16,10 +18,11 @@ import { loadFixture } from './utils.js'
1618
* @returns {Promise<void>}
1719
*/
1820
export const ingestItem = async (params) => {
19-
await sns().publish({
21+
const command = new PublishCommand({
2022
TopicArn: params.ingestTopicArn,
2123
Message: JSON.stringify(params.item)
2224
})
25+
await sns().send(command)
2326

2427
await sqsTriggerLambda(params.ingestQueueUrl, handler)
2528

@@ -78,10 +81,11 @@ export async function testPostIngestSNS(t, record, shouldError = false) {
7881
// @ts-ignore
7982
process.env['POST_INGEST_TOPIC_ARN'] = t.context.postIngestTopicArn
8083

81-
await sns().publish({
84+
const publishCommand = new PublishCommand({
8285
TopicArn: t.context.ingestTopicArn,
8386
Message: JSON.stringify(record)
8487
})
88+
await sns().send(publishCommand)
8589

8690
try {
8791
await sqsTriggerLambda(t.context.ingestQueueUrl, handler)
@@ -91,10 +95,11 @@ export async function testPostIngestSNS(t, record, shouldError = false) {
9195
}
9296
}
9397

94-
const { Messages } = await sqs().receiveMessage({
98+
const receiveCommand = new ReceiveMessageCommand({
9599
QueueUrl: t.context.postIngestQueueUrl,
96100
WaitTimeSeconds: 1
97101
})
102+
const { Messages } = await sqs().send(receiveCommand)
98103

99104
t.truthy(Messages, 'Post-ingest message not found in queue')
100105
t.false(Messages && Messages.length > 1, 'More than one message in post-ingest queue')

tests/helpers/sns.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CreateTopicCommand, SubscribeCommand } from '@aws-sdk/client-sns'
12
import { sns as _sns } from '../../src/lib/aws-clients.js'
23
import { randomId } from './utils.js'
34

@@ -7,9 +8,10 @@ import { randomId } from './utils.js'
78
export const createTopic = async () => {
89
const sns = _sns()
910

10-
const { TopicArn } = await sns.createTopic({
11+
const command = new CreateTopicCommand({
1112
Name: randomId('topic')
1213
})
14+
const { TopicArn } = await sns.send(command)
1315

1416
if (TopicArn) return TopicArn
1517

@@ -22,9 +24,10 @@ export const createTopic = async () => {
2224
* @returns {Promise<void>}
2325
*/
2426
export const addSnsToSqsSubscription = async (topicArn, queueArn) => {
25-
await _sns().subscribe({
27+
const command = new SubscribeCommand({
2628
TopicArn: topicArn,
2729
Protocol: 'sqs',
2830
Endpoint: queueArn
2931
})
32+
await _sns().send(command)
3033
}

tests/helpers/sqs.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// @ts-nocheck
22

33
import { isUndefined } from 'lodash-es'
4+
import {
5+
ReceiveMessageCommand,
6+
PurgeQueueCommand,
7+
CreateQueueCommand,
8+
GetQueueAttributesCommand
9+
} from '@aws-sdk/client-sqs'
410
import { sqs as _sqs } from '../../src/lib/aws-clients.js'
511
import { randomId } from './utils.js'
612

@@ -17,10 +23,11 @@ const sqsMessageToRecord = (message) => ({
1723
})
1824

1925
const eventFromQueue = async (ingestQueueUrl) => {
20-
const { Messages } = await _sqs().receiveMessage({
26+
const command = new ReceiveMessageCommand({
2127
QueueUrl: ingestQueueUrl,
2228
WaitTimeSeconds: 1
2329
})
30+
const { Messages } = await _sqs().send(command)
2431

2532
return {
2633
Records: Messages.map((m) => sqsMessageToRecord(m))
@@ -37,7 +44,8 @@ export const sqsTriggerLambda = async (sqsUrl, handler, _context = {}) => {
3744
* @returns {Promise<void>}
3845
*/
3946
export const purgeQueue = async (url) => {
40-
await _sqs().purgeQueue({ QueueUrl: url })
47+
const command = new PurgeQueueCommand({ QueueUrl: url })
48+
await _sqs().send(command)
4149
}
4250

4351
/**
@@ -46,9 +54,10 @@ export const purgeQueue = async (url) => {
4654
export const createQueue = async () => {
4755
const sqs = _sqs()
4856

49-
const { QueueUrl } = await sqs.createQueue({
57+
const command = new CreateQueueCommand({
5058
QueueName: randomId('queue')
5159
})
60+
const { QueueUrl } = await sqs.send(command)
5261

5362
if (QueueUrl) return QueueUrl
5463

@@ -62,10 +71,11 @@ export const createQueue = async () => {
6271
export const getQueueArn = async (queueUrl) => {
6372
const sqs = _sqs()
6473

65-
const getQueueAttributesResult = await sqs.getQueueAttributes({
74+
const command = new GetQueueAttributesCommand({
6675
QueueUrl: queueUrl,
6776
AttributeNames: ['QueueArn']
6877
})
78+
const getQueueAttributesResult = await sqs.send(command)
6979

7080
if (
7181
isUndefined(getQueueAttributesResult.Attributes)

tests/system/test-ingest.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import url from 'url'
44
import test from 'ava'
55
import nock from 'nock'
66
import { DateTime } from 'luxon'
7+
import { PublishCommand } from '@aws-sdk/client-sns'
8+
import { ReceiveMessageCommand } from '@aws-sdk/client-sqs'
9+
import { CreateBucketCommand, PutObjectCommand } from '@aws-sdk/client-s3'
710
import { getCollectionIds, getItem } from '../helpers/api.js'
811
import { handler, resetAssetProxy } from '../../src/lambdas/ingest/index.js'
912
import { loadFixture, randomId } from '../helpers/utils.js'
@@ -54,10 +57,11 @@ test('The ingest lambda supports ingesting a collection published to SNS', async
5457
{ id: randomId('collection') }
5558
)
5659

57-
await sns().publish({
60+
const publishCommand = new PublishCommand({
5861
TopicArn: ingestTopicArn,
5962
Message: JSON.stringify(collection)
6063
})
64+
await sns().send(publishCommand)
6165

6266
await sqsTriggerLambda(ingestQueueUrl, handler)
6367

@@ -85,23 +89,26 @@ test('The ingest lambda supports ingesting a collection sourced from S3', async
8589
const sourceBucket = randomId('bucket')
8690
const sourceKey = randomId('key')
8791

88-
await s3.createBucket({
92+
const createBucketCommand = new CreateBucketCommand({
8993
Bucket: sourceBucket,
9094
CreateBucketConfiguration: {
9195
LocationConstraint: 'us-west-2'
9296
}
9397
})
98+
await s3.send(createBucketCommand)
9499

95-
await s3.putObject({
100+
const putObjectCommand = new PutObjectCommand({
96101
Bucket: sourceBucket,
97102
Key: sourceKey,
98103
Body: JSON.stringify(collection)
99104
})
105+
await s3.send(putObjectCommand)
100106

101-
await sns().publish({
107+
const publishCommand2 = new PublishCommand({
102108
TopicArn: ingestTopicArn,
103109
Message: JSON.stringify({ href: `s3://${sourceBucket}/${sourceKey}` })
104110
})
111+
await sns().send(publishCommand2)
105112

106113
await sqsTriggerLambda(ingestQueueUrl, handler)
107114

@@ -125,10 +132,11 @@ test('The ingest lambda supports ingesting a collection sourced from http', asyn
125132

126133
nock('http://source.local').get('/my-file.dat').reply(200, collection)
127134

128-
await sns().publish({
135+
const publishCommand3 = new PublishCommand({
129136
TopicArn: ingestTopicArn,
130137
Message: JSON.stringify({ href: 'http://source.local/my-file.dat' })
131138
})
139+
await sns().send(publishCommand3)
132140

133141
await sqsTriggerLambda(ingestQueueUrl, handler)
134142

@@ -417,11 +425,12 @@ async function emptyPostIngestQueue(t) {
417425
// We recommend waiting for 60 seconds regardless of your queue's size."
418426
let result
419427
do {
420-
// eslint-disable-next-line no-await-in-loop
421-
result = await sqs().receiveMessage({
428+
const receiveCommand = new ReceiveMessageCommand({
422429
QueueUrl: t.context.postIngestQueueUrl,
423430
WaitTimeSeconds: 1
424431
})
432+
// eslint-disable-next-line no-await-in-loop
433+
result = await sqs().send(receiveCommand)
425434
} while (result.Message && result.Message.length > 0)
426435
}
427436

0 commit comments

Comments
 (0)