Configure AWS S3 for static files | Django 4.0 | Django fundamentals - Part 29
HTML-код
- Опубликовано: 7 сен 2024
- Configure AWS S3 for static files
📢 Python Decouple article (environment variables):
www.cloudwithd...
📑 My tech blog:
www.cloudwithd...
🧑💻 Python Django: Ultimate Beginners Course - 2022:
www.udemy.com/...
🧑💻 Cloud Computing on AWS: Ultimate Beginners Course - 2022:
www.udemy.com/...
📱 Connect with me:
/ cloud_with_django
🎨 Twitch digital art:
payhip.com/Dig...
NB! - To prevent files from being overwritten, be sure to add the following to your settings.py:
AWS_S3_FILE_OVERWRITE = False
That means should we have to turn on bucket versioning? Because if we set overwrite = False then it will create versioning for the same media files that we are uploading.
way better than what I was trying to do. Good lord. I know you made this a year ago, but thank you!
I'm glad it helped you. Of course, my pleasure!
Here is a newer one for you:
ruclips.net/video/JQVQcNN0cXE/видео.html
elegence and clean..thankyou:)
Thank you so much! :)
Of course, my pleasure!
Your tutorials are awesome 👍.
Thank you so much!
Hi
So I want to deploy my Django app with s3 for static files,
I also want to switch SQLite to Postgres and use nginx to keep it running continuously.
Which steps do i start first and second?
Hi Glory,
Thank you for reaching out.
Unfortunately, you will need to do a lot of research on your own for this, but I can give you some help in terms of the direction.
I have video's on my channel that teach you how to integrate a PostgreSQL database within a Django app.
I also have a video on how to utilize Nginx - BUT it does have some gaps, and you will need to experiment with your own unique use case.
Here are the two video links:
ruclips.net/video/RGRB6w-X_a0/видео.html
ruclips.net/video/7O1H9kr1CsA/видео.html
All the best!
perfect .Thank you .
My pleasure!
This worked perfectly locally, but when I try to run my docker container I keep getting "ModuleNotFoundError: No module named 'storages'". I use pipenv and I have django-storages in my pipfile and requirements.txt files repectively.
Hi,
If you are using Docker, then I'd double check that storages has been installed and setup correctly when you build your docker image (at that point) - you should be able to see it in the logs as your docker image is being constructed. I would also recommend perhaps trying to setup your packages with virtualenv as an alternative to see if that may alleviate your problem in the process.
thanks
My pleasure!
After I updated the policy, I am not getting publicly accessible. I checked all the steps that I followed. But I am getting S3 path when I testing website locally.
Hi,
In the newer versions it won't explicitly state publicly accessible with the aforementioned label. It just will be if you followed the steps correctly.
@@CloudWithDjango Thankyou
My pleasure!
Hi, thank you very much for the tutorial it helped me a lot. When I wanted to enter the django administration, it only shows me text, the interface no longer appears. What do I have to do to make it show up?
My pleasure, Emily! I'm glad that my tutorial was of value to you.
Ah, so all you need to do is the following:
1) Add the following to your settings.py:
ADMIN_MEDIA_PREFIX = '/static/admin/'
2) Run the following command in your terminal:
python manage.py collectstatic
3) Restart your server and refresh your admin page!
Your admin page will then look cleaner as it should.
Good luck!
Thank you. As far as I understand we don't re-size our images. What is the right place(moment) to resize images: with js on the client side, in a view , via aws lamda function?
Hi,
It is my pleasure.
Yes, in this tutorial we don't re-size our images.
I believe you have the right idea with the lambda function.
I'm not an expert on the topic, so I can unfortunately only give limited advice. Apologies for that.
That being said, I would assume that you must first upload your images and then utilise a lambda function on the amazon s3 bucket where your images are stored. Then you'll be able to perform actions on these images (objects). I think that would be the right place.
With the js and view, it would depend on your use case. Like if you want to resize your original image with the resized version. I would think that you just need to add some python logic in your django project if that.
I will attach documentation from AWS, which hopefully will be of more value to you and point you in a better direction.
All the best!
aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/
why would you manually be copying the files over and not allowing collectstatic to handle this for you? seems like the bucket would need put permissions from the user with keys only?
Hi,
Permissions are explicitly set in the AWS management console (on your users - and this extends to the access keys). This is part of the permission policies. You would need to either allow administrator access or S3Full access. Nothing else needs to be done on our end whether we copy files manually or via collectstatic.
Since, this is part of a playlist, you would need to watch the previous video (below) for IAM user permissions:
ruclips.net/video/_5ptAPxvTtQ/видео.html
We do it manually since we are only dealing with a small amount of files. If we were in a position where we had a lot of files to deal with, then collectstatic would've been a better alternative.
everything works fine in development mode. But in production I get the following error while trying to save inages to S3
ClientError at /
An error occurred (400) when calling the HeadObject operation: Bad Request
Hi,
If it works in development mode, it should work in production mode as well.
Please make sure that you've configured everything correctly. Especially in terms of your s3 settings.
Your static files are in a static folder including images,but what to do if my images are in a folder called media?
Hi Andrei,
It seems your structure is a bit different, no worries.
Here is some supporting material to assist you:
django-storages.readthedocs.io/en/latest/backends/amazon-S3.html
After carefully evaluating the docs, you can then run: python manage.py collectstatic
This will automatically upload your files to S3 without having to manually upload as I demonstrate in this video.
So if i im using django 4.2.1 then in storages i must write this:
STORAGES = {"staticfiles": {"BACKEND": "storages.backends.s3boto3.S3StaticStorage"}}
And the run the command above?
Hi Andrei,
Yes, that is correct. Also, the current settings demonstrated in the video will still work, but it would be better to use the new setting specified in the docs (if you are using Django 4.2 >), due to future deprecation with the custom file storage settings.
Irrespective of this, the command that you run is the same.
@@CloudWithDjango At least tell me how to put my media folder inside static like yours
Hello. at the moment to deploy appears an error: [ERROR] Runtime.ImportModuleError: Unable to import module 'vc__handler__python': No module named 'django'
Traceback (most recent call last).
Hi,
Please check your Python installation and that you application is working locally.
I don't have any extra files other than the one that came default with django. Do I need to do all this to render the admin dashboard correctly?
Hi,
Yes, you would need to also run python manage.py collectstatic to render your admin dashboard properly. You would also need to set ADMIN_MEDIA_PREFIX = '/static/admin/' in your code as well in your settings.py file.
where can we find the final code for this please ?
Hi,
The final code is not available for this series unfortunately
why is it a better idea to store your static files on s3 rather than directly uploading on a hosting like render or heroku ?
Hi Sameer,
Amazon S3 is more robust and manageable than alternative options
@@CloudWithDjango ok..I deployed a django project on render a few days ago. The pages were not being rendered appropriately. Could the reason be that, I didn't use any service like s3 to store my static files ?
Also, the images being uploaded were not being retained.
Hi,
Yes, you would need to either use whitenoise, S3 or cloudinary for your static files.
Regarding the image issue, you would need to check your source code carefully.
Can you do a tutorial on Django with docker + postgress? And deployment of the container?
Hi! Thanks for reaching out! Speaking of this.. I'm actually currently working on a Django deployment mastery course... More information of this can be found at my blog... in the description below this video. If you are interested of course.
@@CloudWithDjango The course isn’t ready yet on the website, I’d appreciate if you can share that here on RUclips. Understanding docker as a Django developer, redis, postgres, celery etc and deployment to ECS.
@@nicenaija9952 Yes, I'm still busy working on the course. It might be a while before I share the concepts of Docker containers and deployment with Amazon ECS on RUclips, since I'm busy with tutorials on the basic fundamentals of AWS at the moment. Hopefully soon though!