1+ #! /bin/bash
2+
3+ # Script to set up S3 bucket for static assets with proper configuration
4+ # Run this once to configure the bucket for static asset hosting
5+
6+ set -e
7+
8+ # Environment variables required:
9+ # S3_BUCKET - S3 bucket name for static assets
10+ # AWS_REGION - AWS region (optional, defaults to us-east-1)
11+
12+ BUCKET=${S3_BUCKET:- " latitude-static-assets" }
13+ REGION=${AWS_REGION:- " us-east-1" }
14+
15+ echo " Setting up S3 bucket: $BUCKET in region: $REGION "
16+
17+ # Create bucket if it doesn't exist
18+ if ! aws s3api head-bucket --bucket " $BUCKET " 2> /dev/null; then
19+ echo " Creating bucket $BUCKET ..."
20+ if [ " $REGION " = " us-east-1" ]; then
21+ aws s3api create-bucket --bucket " $BUCKET "
22+ else
23+ aws s3api create-bucket --bucket " $BUCKET " --region " $REGION " --create-bucket-configuration LocationConstraint=" $REGION "
24+ fi
25+ else
26+ echo " Bucket $BUCKET already exists"
27+ fi
28+
29+ # Enable versioning (optional but recommended for backup)
30+ echo " Enabling versioning..."
31+ aws s3api put-bucket-versioning \
32+ --bucket " $BUCKET " \
33+ --versioning-configuration Status=Enabled
34+
35+ # Set up CORS configuration for web access
36+ echo " Configuring CORS..."
37+ cat > /tmp/cors-config.json << EOF
38+ {
39+ "CORSRules": [
40+ {
41+ "AllowedHeaders": ["*"],
42+ "AllowedMethods": ["GET"],
43+ "AllowedOrigins": ["*"],
44+ "MaxAgeSeconds": 3000
45+ }
46+ ]
47+ }
48+ EOF
49+
50+ aws s3api put-bucket-cors \
51+ --bucket " $BUCKET " \
52+ --cors-configuration file:///tmp/cors-config.json
53+
54+ # Set up bucket policy for public read access to static assets
55+ echo " Configuring bucket policy..."
56+ cat > /tmp/bucket-policy.json << EOF
57+ {
58+ "Version": "2012-10-17",
59+ "Statement": [
60+ {
61+ "Sid": "PublicReadGetObject",
62+ "Effect": "Allow",
63+ "Principal": "*",
64+ "Action": "s3:GetObject",
65+ "Resource": "arn:aws:s3:::$BUCKET /static-assets/*"
66+ }
67+ ]
68+ }
69+ EOF
70+
71+ aws s3api put-bucket-policy \
72+ --bucket " $BUCKET " \
73+ --policy file:///tmp/bucket-policy.json
74+
75+ # Enable static website hosting (optional, for direct access)
76+ echo " Configuring static website hosting..."
77+ aws s3 website " s3://$BUCKET " \
78+ --index-document index.html \
79+ --error-document error.html
80+
81+ echo " S3 bucket setup complete!"
82+ echo " Bucket: $BUCKET "
83+ echo " Website URL: http://$BUCKET .s3-website-$REGION .amazonaws.com"
84+ echo " Static assets will be served from: https://$BUCKET .s3.$REGION .amazonaws.com/static-assets/"
85+
86+ # Clean up
87+ rm -f /tmp/cors-config.json /tmp/bucket-policy.json
0 commit comments