
Overview
在本地添加完新博文后,双击以下 .sh 脚本文件,自动运行代码,将 public 文件夹同步到 S3,并刷新 Cloudfront。如果网页没有刷新,可在 Chrome 中 Ctrl+Shift+R 刷新页面并清理缓存。
脚本 hugo.sh
#!/bin/bash
# Set variables
BUCKET_NAME="your-s3-bucket-name"
DISTRIBUTION_ID="your-cloudfront-distribution-id"
# Step 1: Generate the Hugo site
cd "your_hugo_folder"
echo "Building Hugo site..."
hugo --minify
# Step 2: Sync files to S3
echo "Uploading to S3..."
aws s3 sync public/ s3://$BUCKET_NAME --delete
# Step 3: Invalidate CloudFront cache (optional but recommended)
echo "Invalidating CloudFront cache..."
aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths "/*"
echo "Deployment complete!"
Setup
安装 aws
- 下载 AWS CLI
并安装下载了的
.msi文件 - 在 powershell 中运行
aws --version确认是否安装成功
创建 policy
- IAM Dashboard → Policies → Create policy → JSON 粘贴以下内容
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation",
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::your-s3-bucket-name",
"arn:aws:s3:::your-s3-bucket-name/*"
]
},
{
"Effect": "Allow",
"Action": "cloudfront:CreateInvalidation",
"Resource": "arn:aws:cloudfront::your-account-id:distribution/your-cloudfront-distribution-id"
}
]
}
创建 User
- IAM Dashboard -> Users → Add users
- Attach policies directly -> 搜索上一步创建的 policy 名字 -> Next -> Create user
获取 Access Key ID 和 Secret Access Key
- user -> your_user_name -> Create access key -> Command Line Interface (CLI) -> 复制或下载 csv 文件
在本地设置 aws
- 在 powershell 中输入
aws configure - 输入以下信息
- AWS Access Key ID → (从上一步复制)
- AWS Secret Access Key → (从上一步复制)
- Default region → e.g.,
us-east-1,us-east-2 - Output format -> 回车
- 运行
aws s3 ls以验证是否设置正确
运行
成功配置 aws 后,双击 hugo.sh 文件就能自动更新博客啦。