forked from Durgaprasad-Budhwani/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
86 lines (78 loc) · 1.81 KB
/
utils.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
const { utils } = require('@serverless/core')
const configureBucketForHosting = async (s3, bucketName) => {
const publicAccessBlockConfig = {
BlockPublicAcls: false,
BlockPublicPolicy: false,
IgnorePublicAcls: false,
RestrictPublicBuckets: false
}
const s3BucketPolicy = {
Version: '2012-10-17',
Statement: [
{
Sid: 'PublicReadGetObject',
Effect: 'Allow',
Principal: {
AWS: '*'
},
Action: ['s3:GetObject'],
Resource: [`arn:aws:s3:::${bucketName}/*`]
}
]
}
const staticHostParams = {
Bucket: bucketName,
WebsiteConfiguration: {
ErrorDocument: {
Key: 'index.html'
},
IndexDocument: {
Suffix: 'index.html'
}
}
}
const putPostDeleteHeadRule = {
AllowedMethods: ['PUT', 'POST', 'DELETE', 'HEAD'],
AllowedOrigins: ['https://*.amazonaws.com'],
AllowedHeaders: ['*'],
MaxAgeSeconds: 0
}
const getRule = {
AllowedMethods: ['GET'],
AllowedOrigins: ['*'],
AllowedHeaders: ['*'],
MaxAgeSeconds: 0
}
try {
await s3
.putPublicAccessBlock({
Bucket: bucketName,
PublicAccessBlockConfiguration: publicAccessBlockConfig
})
.promise()
await s3
.putBucketPolicy({
Bucket: bucketName,
Policy: JSON.stringify(s3BucketPolicy)
})
.promise()
await s3
.putBucketCors({
Bucket: bucketName,
CORSConfiguration: {
CORSRules: [putPostDeleteHeadRule, getRule]
}
})
.promise()
await s3.putBucketWebsite(staticHostParams).promise()
} catch (e) {
if (e.code === 'NoSuchBucket') {
await utils.sleep(2000)
return configureBucketForHosting(s3, bucketName)
}
throw e
}
}
module.exports = {
configureBucketForHosting
}