Using Boto3 with freezegun results in 403

时间:2019-05-01 08:41:15

标签: python python-3.x boto3 freezegun

I'm using boto3 to load some file from an AWS S3 bucket. Which is working fine. However for my unittests i'm calling freeze_time and then the function returns the error: botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden.

Is there a solution, which enables me to keep using freeze_time and which will retreive the file from S3?

The code works without @freeze_time("2019-01-30") and doesn't when added.

import boto3
from freezegun import freeze_time
import io

bucket = 'bucket'
key = 'key'

@freeze_time("2019-01-30")
def test_x():
    s3 = boto3.client('s3')
    f = io.BytesIO()
    s3.download_fileobj(bucket, key, f)

test_x()

1 个答案:

答案 0 :(得分:0)

否-您不能使用freeze_time。您需要找到另一种方法。

AWS api调用需要时间来在客户端和服务器之间达成“在合理范围内”的协议。一个客户说一月实际上是五月,被认为是“不合理的”。根据aws.amazon.com上的信息,允许的最大时钟偏差为15分钟。

对于某些SDK,Amazon嵌入了自动时钟偏斜校正。这样可以完成通话,但可能无法给您所需的时间。