-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgost-aws.js
124 lines (113 loc) · 3.96 KB
/
gost-aws.js
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
119
120
121
122
123
124
const { SESClient, SendEmailCommand } = require('@aws-sdk/client-ses');
const { S3Client } = require('@aws-sdk/client-s3');
const { SQSClient } = require('@aws-sdk/client-sqs');
const { getSignedUrl: awsGetSignedUrl } = require('@aws-sdk/s3-request-presigner');
/*
----------------------------------------------------------
AWS S3
----------------------------------------------------------
*/
function getS3Client() {
let s3;
if (process.env.LOCALSTACK_HOSTNAME) {
/*
1. Make sure the local environment has awslocal installed.
2. Use the commands to create a bucket to test with.
- awslocal s3api create-bucket --bucket arpa-audit-reports --region us-west-2 --create-bucket-configuration '{"LocationConstraint": "us-west-2"}'
3. Access bucket resource metadata through the following URL.
- awslocal s3api list-buckets
- awslocal s3api list-objects --bucket arpa-audit-reports
*/
console.log('------------ USING LOCALSTACK ------------');
const endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT || 4566}`;
console.log(`endpoint: ${endpoint}`);
s3 = new S3Client({
endpoint,
forcePathStyle: true,
region: process.env.AWS_DEFAULT_REGION,
});
} else {
s3 = new S3Client();
}
return s3;
}
/**
* This function is a wrapper around the getSignedUrl function from the @aws-sdk/s3-request-presigner package.
* Exists to organize the imports and to make it easier to mock in tests.
*/
async function getSignedUrl(s3, command, options) {
return awsGetSignedUrl(s3, command, options);
}
/*
----------------------------------------------------------
AWS SES
----------------------------------------------------------
*/
function getSESClient() {
const sesOptions = {};
if (process.env.LOCALSTACK_HOSTNAME) {
sesOptions.endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT || 4566}`;
sesOptions.region = process.env.AWS_DEFAULT_REGION;
}
return new SESClient(sesOptions);
}
async function sendEmail(message) {
if (process.env.SUPPRESS_EMAIL) return;
if (!process.env.NOTIFICATIONS_EMAIL) throw new Error('NOTIFICATIONS_EMAIL is not set');
const transport = getSESClient();
const params = {
Destination: {
ToAddresses: [message.toAddress],
},
Source: process.env.NOTIFICATIONS_EMAIL,
Message: {
Subject: {
Charset: 'UTF-8',
Data: message.subject,
},
Body: {
Html: {
Charset: 'UTF-8',
Data: message.body,
},
Text: {
Charset: 'UTF-8',
Data: message.text,
},
},
},
};
if (message.ccAddress) {
params.Destination.CcAddresses = [message.ccAddress];
}
const command = new SendEmailCommand(params);
try {
const data = await transport.send(command);
console.log('Success sending SES email:', JSON.stringify(data));
} catch (err) {
console.error('Error sending SES email:', err, err.stack);
throw err;
}
}
/*
----------------------------------------------------------
AWS SQS
----------------------------------------------------------
*/
function getSQSClient() {
let sqs;
if (process.env.LOCALSTACK_HOSTNAME) {
console.log('------------ USING LOCALSTACK FOR SQS ------------');
const endpoint = `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT || 4566}`;
sqs = new SQSClient({ endpoint, region: process.env.AWS_DEFAULT_REGION });
} else {
sqs = new SQSClient();
}
return sqs;
}
module.exports = {
getS3Client,
getSignedUrl,
sendEmail,
getSQSClient,
};