-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3CreateEventSqsPoller.js
More file actions
118 lines (100 loc) · 3.66 KB
/
s3CreateEventSqsPoller.js
File metadata and controls
118 lines (100 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict'
const {Consumer} = require('sqs-consumer');
const { APIClient } = require('./clientVars.js')
const {idpCredentials} = require('./idpStuff')
const AWS = require('aws-sdk');
AWS.config.region = 'us-east-1'
async function refresh() {
let access_token
const loginAddress = APIClient.keycloakAddress
try {
access_token = await idpCredentials()
AWS.config.credentials.params.Logins[loginAddress] = access_token
console.log('Retrieve STEVE access token.')
} catch(e) {
console.log('ERROR in REFRESH',e)
}
return access_token
}
async function setAWSCredentials() {
const loginAddress = APIClient.keycloakAddress
const idp = APIClient.cognitoIdentityPoolID
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: idp,
Logins: {
[loginAddress]:'',
}
})
await refresh()
const STS = new AWS.STS()
let data1
try {
data1 = await STS.getCallerIdentity().promise()
} catch(e) {
console.log('getCallerIdentity error',e)
}
//console.log('Swap access token for AWS Cognito Credentials:',data1)
console.log('Swap access token for AWS Cognito Credentials.')
console.log('Monitor SQS Queue...')
}
async function getFileInfo(getParams) {
const S3 = new AWS.S3()
let fileSummary;
let filename;
try {
fileSummary = await S3.headObject(getParams).promise()
filename = fileSummary.Metadata.filename
} catch (error) {
if (error.statusCode === 404) {
console.log('File Not Found')
}
}
return filename
}
async function main() {
await setAWSCredentials()
const app = Consumer.create({
queueUrl: APIClient.steveOutputBucketQueueUrl,
handleMessage: async (message) => {
//console.log('message',message)
if (typeof message.Body !== "undefined") {
const deliveryMessage = JSON.parse(JSON.parse(message.Body).Message)
if (!(deliveryMessage.Records && deliveryMessage.Records.length && deliveryMessage.Records[0].s3)) {
console.dir(deliveryMessage, { depth: null, colors: true })
return
}
const objectKey = deliveryMessage.Records[0].s3.object.key
const bucketName = deliveryMessage.Records[0].s3.bucket.name
const clientName = APIClient.clientName
const fileKey = objectKey.replace(`${clientName}/inbound/`,'')
const getParams = {
Bucket: bucketName,
Key: objectKey,
}
const fileDetails = await getFileInfo(getParams)
console.log('############### New SQS Message ##################')
console.log(`New incoming file "${fileDetails}" detected. To download, use command:`)
console.log(`node getClientFile -k ${fileKey}`)
console.log('##################################################')
console.log('Monitor SQS Queue...')
} else {
console.log('Unknown error with file message read')
}
}
});
app.on('error', async (err) => {
if (err.message.includes('socket hang up')) {
console.log('Refresh Credentials')
await setAWSCredentials()
} else {
console.error(err.message)
}
});
app.on('processing_error', (err) => {
console.log('PROCESSING ERROR!')
console.log('queueUrl', APIClient.steveOutputBucketQueueUrl)
console.error(err.message);
});
app.start();
}
main()