S3 Boto3 Stubber 没有下载文件的映射?

S3 Boto3 Stubber doesn't have mapping for download file?

目前正在编写测试并尝试使用 botocore 提供的Stubber

我正在尝试:

client = boto3.client("s3")
response = {'Body': 'content'}
expected_params = {'Bucket': 'a_bucket_name', 'Key': 'a_path', 'Filename': 'a_target'}

with Stubber(client) as stubber:
    stubber.add_response('download_file', response, expected_params)
    download_file(client, "a_bucket_name", "a_path", "a_target")

下载文件是我自己的函数,它只是包装了客户端 download_file 调用。它在实践中有效。

但是,由于 'OperationNotFound' 错误,stubber.add_response 上的测试失败。我逐步使用调试器,问题出现在存根 API:

if not hasattr(self.client, method):
    raise ValueError(
        "Client %s does not have method: %s"
        % (self.client.meta.service_model.service_name, method))

# Create a successful http response
http_response = AWSResponse(None, 200, {}, None)

operation_name = self.client.meta.method_to_api_mapping.get(method) <------- Error here
self._validate_response(operation_name, service_response)

字典里好像没有这两者的对应关系,这是存根的问题API还是我遗漏了什么?

我刚刚发现这个问题,所以看起来这一次确实是图书馆而不是我:

https://github.com/boto/botocore/issues/974

That's because download_file and upload_file are customizations which live in boto3. They call out to one or many requests under the hood. Right now there's not a great story for supporting customizations other than recording underlying commands they use and adding them to the stubber. There's an external library that can handle that for you, though we don't support it ourselves.