使用 python moto lib 模拟 s3 时出现 ExpiredToken 错误

问题描述 投票:0回答:1

我是 Python 新手。任何人都知道 moto 模拟测试是如何工作的?我将测试将从 S3 获取数据并将数据转换并上传到 S3 的代码。我使用 moto lib 来模拟 s3,但是当从我的代码调用 S3 时它显示“调用 ListObjects 操作时发生错误(ExpiredToken)”。

我需要模拟 aws 凭证吗?我应该如何嘲笑它? (我检查了 moto 库,没有 mock_arn/mock_credential 这样的东西)

提前谢谢你。

这是我的代码:在 parse_requests 方法中,它将从 S3 获取数据并转换数据,然后上传到 S3。

Class Test_processing_data(TestCase):
    def setUp(self):
        self.mock_s3 = mock_s3()
        self.mock_s3.start()
        self.mock_logs = mock_logs()
        self.mock_logs.start()
        self.bucket_region = "us-east-1"
        self.bucket_name = "test-bucket"
        self.s3_client = boto3.client("s3", region_name=self.bucket_region)
        self.s3_client.create_bucket(Bucket=self.bucket_name)

    @mock_s3
    def test_parse_requests(self):
        bucket_name = "test-bucket"
        prefix = "test/model/main/"
        execution_date = "2022/09/13/12"

        parse_requests(execution_date, bucket_name, prefix)

这里是错误信息:

self = <botocore.client.S3 object at 0x10abf18e0>
operation_name = 'ListObjects'
api_params = {'Bucket': 'campaign-performance-forecasting-offline', 'EncodingType': 'url', 'Prefix': 'test/model/main/2022/09/13/12'}

...

       if http.status_code >= 300:
            error_code = parsed_response.get("Error", {}).get("Code")
            error_class = self.exceptions.from_code(error_code)
>           raise error_class(parsed_response, operation_name)
E           botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the ListObjects operation: The provided token has expired.

../../venv/lib/python3.8/site-packages/botocore/client.py:914: ClientError
python amazon-s3 python-unittest moto
1个回答
0
投票

模拟应该在客户建立之前建立。您似乎已经在外部模拟中建立了

s3_client
,这可能是导致此问题的原因。

尝试在函数或类之前设置模拟并尝试一下。我希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.