django and aws s3 (static files and media)

Hi,

I have configured AWS S3 to store the static and media files but for some reason it is not working. when i load my site i don’t see css implemented or any photos or videos. That said, if i insepect the page i can see that it is poiting to my S3 bucket with correct url from S3.

settings.py

     AWS_ACCESS_KEY_ID = ''
     AWS_SECRET_ACCESS_KEY = ''
     AWS_STORAGE_BUCKE_NAME = 'maha2022'
     AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazoneaws.com' % AWS_STORAGE_BUCKET_NAME
     AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
     #AWS_DEFAULT_ACL = ''

     AWS_LOCATION = 'static'
     STATICFILES_DIRS = [

       os.path.join(BASE_DIR, 'static'),
     ]

     STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
     STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)

     MEDIA_URL = 'cart/images/'
     MEDIA_ROOT = os.path.join(BASE_DIR, 'static/cart/images')

I also included “storages” in the “INSTALLED_APPS”
when running locally i get

ModuleNotFoundError: No module named ‘storages’

and yes i did install with pip and inside the virtual env.

Thanks.

The error indicates that the library doesn’t exist. My hunch is that you’re installing dependencies in a different manner than what’s used when you run the app locally. What’s your process for running the application locally? And how did you install storages? Please also verify with pip freeze or your package manager’s equivalent.

running locally i get the error that no module named storages as you mentioned. but how i am getting the right urls when looking at inspect online. I have been trying to solve the issue with no module named storages eventhough i did install it in the same virtual env. how can i see if it is done correctly!

How do you run the application locally? What was the flow for installing storages?

pip install django-storages
i am using git bash and sublime
can i share my setting.py?

Your settings file isn’t necessary at this point. When you run your app locally, likely via manage.py runserver you don’t have the virtualenv activated where storages is installed.

What command do you run to run the application locally? And where is your virtualenv located?

the structure is below:

here i will activate the virtual:
/d/projects/maha (this is where i installed django-storages)
and i then go one more to be in app
cd maha (again) - (here where the manage.py is located)

locally i am getting no module named storages but it is there online in aws s3 site.

So your commands look like?

$ source venv/bin/activate
$ cd maha
$ pip install -r requirements.txt
$ python manage.py runserver

Here what I do:

$ source venv/bin/activate
$ cd maha
$ pip install -r requirements.txt (is this to create the requirments.txt?)
$ pip install django-storages
$ python manage.py runserver (here it says no module named storages)

I have found solution from a sample code that i searched for on the internet to make the photos and css files work in the site.

code sample

Now i can see the css files and photos but still have the error that storages is not definded locally and i think that is why when i tried to add a product (production) that needs to add photo and viedo it is giving me server error 500 because of the storages that is not defined.

Please post a full stack trace of the error as well as the output from pip freeze.

This is the error:

and this is pip freeze:

Does it matter that there’s no AWS_QUERYSTRING_AUTH in settings.py?

https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html

Setting AWS_QUERYSTRING_AUTH to False to remove query parameter authentication from generated URLs. This can be useful if your S3 buckets are public.

I wasn’t going to comment since I’m not very knowledgeable on this topic but I just noticed this setting is missing. I’ve but doing my own research to implement s3 I found some issues with the AWS_QUERYSTRING_AUTH depending on if you want your media files public, I haven’t implemented it for static files yet so not sure on the exact config but for public media files AWS_QUERYSTRING_AUTH = False is working for me.

Also here is some useful links on the AWS_QUERYSTRING_AUTH issues with various workarounds:

Edit:
Also I’m not using this setting:

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

I have:

DEFAULT_FILE_STORAGE = 'myproject.storage_backends.MediaStorage'

As well as a storage_backends.py file in the same folder as settings.py which contains:

from storages.backends.s3boto3 import S3Boto3Storage

class MediaStorage(S3Boto3Storage):
    location = 'media'
    file_overwrite = True

It looks like you have a editable installation of django-storages. Is that on purpose?

I was missing up with imstalling django storages. Is there away to delete all libraries and start over with pip install?

You can delete your virtualenv folder and create a new one.

there was something wrong with my app. I deleted the vitual folder and created new one and everything went well.