这是由于AWS在预签名URL创建后需要一定时间来确保URL与所需的权限一致。解决方法是等待一段时间或使用以下代码中的等待函数,在发出请求之前等待一段时间。以下是在使用Python的Boto3库时的示例代码:
import boto3
import time
def get_object_url(bucket, key, expires_in):
s3 = boto3.client('s3')
url = s3.generate_presigned_url(
'get_object',
Params={
'Bucket': bucket,
'Key': key
},
ExpiresIn=expires_in
)
time.sleep(15) # Wait for URL to become active
return url
在上面的代码中,get_object_url函数将返回一个预签名URL,但会在发出请求之前等待15秒以确保URL已准备就绪。你可以根据需要调整等待时间。