getmoto / moto

A library that allows you to easily mock out tests based on AWS infrastructure.
http://docs.getmoto.org/en/latest/
Apache License 2.0
7.52k stars 2.01k forks source link

No BotoCoreError to generate_presigned_url without an existing bucket #7793

Closed Lewiscowles1986 closed 1 month ago

Lewiscowles1986 commented 1 month ago

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.

Lewiscowles1986 commented 1 month ago

😊

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

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