-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
s3.ts
43 lines (39 loc) · 1.03 KB
/
s3.ts
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
import aws from 'aws-sdk'
import s3 from 's3-blob-store'
export interface S3Options {
accessKey?: string
secretKey?: string
region?: string
endpoint?: string
bucket: string
}
// AWS_ envs are default for aws-sdk
export function createS3({
accessKey = process.env.AWS_ACCESS_KEY_ID || process.env.S3_ACCESS_KEY,
secretKey = process.env.AWS_SECRET_ACCESS_KEY || process.env.S3_SECRET_KEY,
bucket,
region = process.env.AWS_REGION || process.env.S3_REGION,
endpoint,
}: S3Options) {
const client = new aws.S3({
...(accessKey && secretKey
? {
credentials: {
accessKeyId: accessKey,
secretAccessKey: secretKey,
sessionToken: process.env.AWS_SESSION_TOKEN,
},
}
: {}),
...(region ? { region } : {}),
...(endpoint ? { endpoint: new aws.Endpoint(endpoint) } : {}),
...(process.env.NODE_ENV === 'test'
? { sslEnabled: false, s3ForcePathStyle: true }
: {}),
})
const location = s3({
client,
bucket,
})
return location
}