-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrigir-portfolio.py
More file actions
81 lines (69 loc) · 2.05 KB
/
Copy pathcorrigir-portfolio.py
File metadata and controls
81 lines (69 loc) · 2.05 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
import boto3
import json
s3 = boto3.client('s3')
cf = boto3.client('cloudfront')
bucket = "portfolio-sergio-sena"
cf_id = "E23SOSSVGQ2NOD"
print("="*60)
print("CORRIGINDO PORTFOLIO")
print("="*60)
# 1. Verificar bucket policy atual
print(f"\n[1] Verificando bucket: {bucket}")
try:
policy = s3.get_bucket_policy(Bucket=bucket)
print("[OK] Bucket policy existe")
except:
print("[AVISO] Sem bucket policy")
# 2. Aplicar policy correta para CloudFront OAC
print(f"\n[2] Aplicando bucket policy correta...")
policy = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCloudFrontServicePrincipal",
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Action": "s3:GetObject",
"Resource": f"arn:aws:s3:::{bucket}/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": f"arn:aws:cloudfront::969430605054:distribution/{cf_id}"
}
}
}
]
}
try:
s3.put_bucket_policy(Bucket=bucket, Policy=json.dumps(policy))
print("[OK] Bucket policy atualizada")
except Exception as e:
print(f"[ERRO] {str(e)}")
# 3. Sincronizar conteúdo
print(f"\n[3] Sincronizando conteudo...")
import subprocess
cmd = f'aws s3 sync "C:\\Projetos Git\\dev-cloud" s3://{bucket}/ --delete'
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.returncode == 0:
print("[OK] Sincronizacao concluida")
else:
print(f"[ERRO] {result.stderr}")
# 4. Invalidar cache
print(f"\n[4] Invalidando cache CloudFront...")
try:
cf.create_invalidation(
DistributionId=cf_id,
InvalidationBatch={
'Paths': {'Quantity': 1, 'Items': ['/*']},
'CallerReference': str(hash(bucket))
}
)
print("[OK] Cache invalidado")
except Exception as e:
print(f"[ERRO] {str(e)}")
print("\n" + "="*60)
print("CONCLUIDO!")
print("Aguarde 2-3 minutos e teste:")
print("https://dev-cloud.sstechnologies-cloud.com/")
print("="*60)