要缓存特定的对象,可以使用AWS CloudFront提供的缓存行为和缓存键。下面是一个使用AWS CLI进行配置的示例:
{
"Version": "2012-10-17",
"DefaultTTL": 86400,
"MinTTL": 3600,
"MaxTTL": 86400,
"CacheBehaviors": [
{
"PathPattern": "/path/to/object",
"TargetOriginId": "your-origin-id",
"ForwardedValues": {
"QueryString": false,
"Cookies": {
"Forward": "none"
}
},
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"ViewerProtocolPolicy": "allow-all",
"AllowedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"],
"CachedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"]
}
},
"SmoothStreaming": false,
"Compress": true
}
]
}
aws cloudfront create-cache-policy --cache-policy-config file://policy.json --name "MyCachePolicy" --profile your-profile
aws cloudfront list-cache-policies --query "CachePolicyList.Items[?Name=='MyCachePolicy'].Id" --profile your-profile --output text
{
"CacheBehaviors": [
{
"PathPattern": "/path/to/object",
"AllowedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"],
"CachedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"]
}
},
"CachePolicyId": "your-cache-policy-id",
"TargetOriginId": "your-origin-id",
"ViewerProtocolPolicy": "redirect-to-https"
}
]
}
aws cloudfront update-distribution --id your-distribution-id --distribution-config file://config.json --profile your-profile
请注意替换示例中的“your-origin-id”,“your-distribution-id”和“your-profile”为你自己的值。
这样配置后,CloudFront将缓存特定路径模式下的对象,并根据缓存策略的配置设置对象的TTL和缓存行为。