get url on item in AWS s3

Advertisements

hello i am using a django app and host my media files on a AWS S3 bucket saving and showing in my app its fine but i need to generate a url of image to put in on a email i use django boto3

and this code to get the url :

import boto3
from decouple import config

AWS_ACCESS_KEY_ID = config("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = config("AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = 'wiini'
AWS_S3_REGION_NAME = 'ca-central-1'
AWS_S3_SIGNATURE_NAME = 's3v4'
AWS_S3_FILE_OVERWRITE = False


def get_s3_favicon_url():
    s3_client = boto3.client(
        's3',
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
        region_name=AWS_S3_REGION_NAME,
        config=boto3.session.Config(signature_version=AWS_S3_SIGNATURE_NAME)
    )

    favicon_key = 'favicon-32x32.png'

    url = s3_client.generate_presigned_url(
        'get_object',
        Params={'Bucket': AWS_STORAGE_BUCKET_NAME, 'Key': favicon_key},
        ExpiresIn=3600,  # Set an appropriate expiration time in seconds
        HttpMethod='GET',  # Specify the HTTP method
    )

    print("Generated URL:", url)
    return url

but when i click on the generated url i get :

<Code>AuthorizationQueryParametersError</Code>

compelete error is here :

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AuthorizationQueryParametersError</Code>
<Message>Query-string authentication version 4 requires the X-Amz-Algorithm, X-Amz-Credential, X-Amz-Signature, X-Amz-Date, X-Amz-SignedHeaders, and X-Amz-Expires parameters.</Message>
<RequestId>MBZZHKZF2ZJEGNJ2</RequestId>
<HostId>1u9Jg6UQWPsdbmN2Qh88HYhsVNILR3CY58RzIMu3Be6FALR6D8qu+GnLEND/Mwee4Mkx0p7zoR8=</HostId>
</Error>

can someone help me to fix this problem ?

>Solution :

Your example signed URL is missing a number of URL parameters that must be present for it to work.

An example should look more like https://wiini.s3.amazonaws.com/favicon-32x32.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA.....%2F20240104%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240104T132824Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=b4533.....a663

My guess would be print("Generated URL:", url) prints the correct URL and its getting truncated somewhere after get_s3_favicon_url returns.

Leave a ReplyCancel reply