/
aws-s3-folder.ts
35 lines (34 loc) 路 1.26 KB
/
aws-s3-folder.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
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as fs from "fs";
// Create a bucket and expose a website index document
const siteBucket = new aws.s3.Bucket("siteBucket", {website: {
indexDocument: "index.html",
}});
const siteDir = "www";
// For each file in the directory, create an S3 object stored in `siteBucket`
const files: aws.s3.BucketObject[] = [];
for (const range of fs.readdirSync(siteDir).map((v, k) => ({key: k, value: v}))) {
files.push(new aws.s3.BucketObject(`files-${range.key}`, {
bucket: siteBucket.id,
key: range.value,
source: new pulumi.asset.FileAsset(`${siteDir}/${range.value}`),
contentType: range.value,
}));
}
// set the MIME type of the file
// Set the access policy for the bucket so all objects are readable
const bucketPolicy = new aws.s3.BucketPolicy("bucketPolicy", {
bucket: siteBucket.id,
policy: siteBucket.id.apply(id => JSON.stringify({
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Principal: "*",
Action: ["s3:GetObject"],
Resource: [`arn:aws:s3:::${id}/*`],
}],
})),
});
export const bucketName = siteBucket.bucket;
export const websiteUrl = siteBucket.websiteEndpoint;