这个问题通常是因为签名URL的过期时间设置得太短导致的。您可以尝试增加过期时间来解决此问题。
以下是示例代码,用于生成带有更长过期时间的AWS S3签名URL:
import boto3
from datetime import datetime, timedelta
# AWS S3 Bucket Configuration
S3_BUCKET = 'my-bucket'
S3_REGION = 'us-east-1'
# Set the expiration time for the signed URL
expire_seconds = 86400 # 24 hours
# Generate a presigned URL for S3 object access
s3_client = boto3.client('s3', region_name=S3_REGION)
object_key = 'path/to/my/object.txt'
expiration = datetime.now() + timedelta(seconds=expire_seconds)
url = s3_client.generate_presigned_url(
'get_object',
Params={
'Bucket': S3_BUCKET,
'Key': object_key,
},
ExpiresIn=expire_seconds,
)
print(f'Presigned URL for {object_key}: {url}')
此代码将为S3对象访问生成有效期更长的预签名URL。具体而言,将URL的过期时间从一小时增加到了24小时。