AWS签名URL在浏览器中是可以被缓存的。浏览器会根据URL的缓存策略来决定是否缓存该URL。
如果你希望禁止浏览器缓存AWS签名URL,可以通过添加缓存控制头来实现。以下是一个示例代码,可以在AWS Lambda函数中生成带有缓存控制头的签名URL:
import boto3
from botocore.client import Config
import datetime
def generate_presigned_url(bucket_name, object_name, expiration=3600):
s3_client = boto3.client('s3', config=Config(signature_version='s3v4'))
# 生成过期时间
expiration_time = datetime.datetime.now() + datetime.timedelta(seconds=expiration)
# 设置缓存控制头
response = s3_client.generate_presigned_url(
'get_object',
Params={
'Bucket': bucket_name,
'Key': object_name,
'ResponseCacheControl': 'no-cache, no-store, must-revalidate',
'ResponseExpires': expiration_time.strftime('%a, %d %b %Y %H:%M:%S GMT')
},
ExpiresIn=expiration
)
return response
在上述代码中,ResponseCacheControl
参数用于设置缓存控制头,将其值设置为no-cache, no-store, must-revalidate
可以禁止浏览器缓存该URL。ResponseExpires
参数用于设置过期时间,确保浏览器每次访问该URL时都会重新请求。
使用上述代码生成的签名URL将包含缓存控制头,并且浏览器将不会缓存该URL。