kitty333
kitty333

Reputation: 11

Images don't show up and can't be added via admin (django) to S3 (aws). Debug=False

I am working on my portfolio project. Django Ecom Website Everything worked well when Debug was True but after changing it to False I started to have problems with my images. The images which I add via Admin Page to my Products (model) don't show up, they don't even get added to AWS S3 (which I want to use as my storage).

This is the repository: repository

I think my bucket in S3 is set correctly. I have also added this to CORS:

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD"
        ],
        "AllowedOrigins": [
            "https://django-ecom-candles-production.up.railway.app"
        ],
        "ExposeHeaders": []
    }
]

and this to Bucket Policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::djangoecomproject/*"
        }
    ]
}

When I'm adding an image to my product via admin page, the image doesn't show up in a bucket and also doesn't display on the website. When I click on the field: Image / currently: ...(the path) in admin page, this shows up:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>3JB289J6QBJ36AXA</RequestId>
<HostId>6qStcpcxTYHZkYimbgbZcAaueIz05495Zr08Dn8Ne7zxReKYerh7s/CAt2hRkqYw6QvltC5aaMdZ1glouj0KVTtZBQkJLhOZnCtJKrnkFvA=</HostId>
</Error>

I tried to add a default image (I have added an image directly to a bucket and it is there and I can access it via its own path) but it doesn't show up on the website.

In Django shell I have also tested uploading a text file to S3 and it worked ( it printed in the shell what I wanted) but the file does not exist in the bucket (on aws s3 website) or I can't find it.

I run this code in my shell:

import boto3

AWS_ACCESS_KEY_ID = 'your-access-key'  # with actual key
AWS_SECRET_ACCESS_KEY = 'your-secret-key'  # with actual secret
BUCKET_NAME = 'djangoecomproject'

# Try connecting to AWS S3 and list contents
try:
    s3 = boto3.client(
        's3',
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
    )

    # List the contents of the S3 bucket
    response = s3.list_objects_v2(Bucket=BUCKET_NAME)
    
    # Check if there are objects in the bucket
    if 'Contents' in response:
        for obj in response['Contents']:
            print(f"Found object: {obj['Key']}")
    else:
        print("No objects found in the bucket.")

except Exception as e:
    print(f"Error: {e}")

DEBUG:root:Initializing S3 client...
DEBUG:botocore.hooks:Event choose-service-name: calling handler <function handle_service_name_alias at 0x0000024C938BB7E0>
DEBUG:botocore.hooks:Event creating-client-class.s3: calling handler <function add_generate_presigned_post at 0x0000024C937FF560>  
DEBUG:botocore.hooks:Event creating-client-class.s3: calling handler <function lazy_call.<locals>._handler at 0x0000024C94919EE0>  
DEBUG:botocore.hooks:Event creating-client-class.s3: calling handler <function add_generate_presigned_url at 0x0000024C937FF2E0>   
DEBUG:botocore.configprovider:Looking for endpoint for s3 via: environment_service
DEBUG:botocore.configprovider:Looking for endpoint for s3 via: environment_global
DEBUG:botocore.configprovider:Looking for endpoint for s3 via: config_service
DEBUG:botocore.configprovider:Looking for endpoint for s3 via: config_global
...     logging.error(f"Error listing bucket contents: {e}")

I would really appreciate some help here. I honestly have no idea what can I do more and how to solve it. I am still learning programming so please be understanding.

Upvotes: 1

Views: 46

Answers (0)

Related Questions