-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
95 lines (78 loc) · 2.38 KB
/
Copy pathdeploy.py
File metadata and controls
95 lines (78 loc) · 2.38 KB
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
import boto3
import os
import json
import mimetypes
# Create S3 client
s3 = boto3.client('s3')
# Unique bucket name
bucket_name = "nitisha-static-site-12345" # change if needed
region = "ap-south-1"
# 1️⃣ Create Bucket
try:
s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': region
}
)
print("✅ Bucket created")
except Exception as e:
print("⚠️ Bucket may already exist:", e)
# 2️⃣ Disable Block Public Access
s3.put_public_access_block(
Bucket=bucket_name,
PublicAccessBlockConfiguration={
"BlockPublicAcls": False,
"IgnorePublicAcls": False,
"BlockPublicPolicy": False,
"RestrictPublicBuckets": False
}
)
print("✅ Public access settings updated")
# 3️⃣ Add Bucket Policy
bucket_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
"Resource": f"arn:aws:s3:::{bucket_name}/*"
}
]
}
s3.put_bucket_policy(
Bucket=bucket_name,
Policy=json.dumps(bucket_policy)
)
print("✅ Bucket policy added")
# 4️⃣ Enable Static Website Hosting
s3.put_bucket_website(
Bucket=bucket_name,
WebsiteConfiguration={
'IndexDocument': {'Suffix': 'index.html'},
'ErrorDocument': {'Key': 'error.html'}
}
)
print("✅ Website hosting enabled")
# 5️⃣ Upload Files
folder_path = "website"
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
s3_path = os.path.relpath(file_path, folder_path)
content_type, _ = mimetypes.guess_type(file_path)
if content_type is None:
content_type = 'binary/octet-stream'
s3.upload_file(
file_path,
bucket_name,
s3_path,
ExtraArgs={'ContentType': content_type}
)
print(f"📤 Uploaded: {s3_path}")
# 6️⃣ Website URL
website_url = f"http://{bucket_name}.s3-website-{region}.amazonaws.com"
print("\n🎉 Deployment Complete!")
print("🌍 Website URL:", website_url)