getmoto / moto

A library that allows you to easily mock out tests based on AWS infrastructure.

Home Page:http://docs.getmoto.org/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No BotoCoreError to generate_presigned_url without an existing bucket

Lewiscowles1986 opened this issue · comments

Reporting Bugs

Python version: 3.11.6
Moto version: 5.0.9
OS Version: OSX
Boto3 version: 1.34.122

Code

import boto3
from botocore.exceptions import BotoCoreError
from mypy_boto3_s3 import S3Client


def get_presigned_upload(bucket: str, key: str) -> str:
    s3_client: S3Client = boto3.client('s3')
    try:
        presigned_response = s3_client.generate_presigned_url(
            "put_object",
            Params={"Bucket": "some-bucket", "Key": "Some Object"},
            ExpiresIn=3600,
            HttpMethod="PUT"
        )
        return presigned_response
    except BotoCoreError:
        print("oh good an error")

called (lets assume I imported directly

from wherever import get_presigned_upload
from moto import mock_aws


with mock_aws():
    get_presigned_upload("bucket", "hello")

If using AWS I get errors trying to run this. NoCredentialsError is the specific, but any boto error will do. If I don't, regardless of the bucket existing or having access; code passes. Right now this means I'm calling list_objects_v2 which successfully errors when I attempt; with a ClientError.

😊

with mock_aws(config={"core": {"mock_credentials": False}}):

Ah, so it just needs to mock credentials to get the same error as stock AWS