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...

Комментарии • 48

  • @CloudWithDjango
    @CloudWithDjango  2 года назад +3

    NB! - To prevent files from being overwritten, be sure to add the following to your settings.py:
    AWS_S3_FILE_OVERWRITE = False

    • @rajarajan1338
      @rajarajan1338 13 дней назад

      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.

  • @OG_Space_Viking
    @OG_Space_Viking 4 месяца назад

    way better than what I was trying to do. Good lord. I know you made this a year ago, but thank you!

    • @CloudWithDjango
      @CloudWithDjango  4 месяца назад

      I'm glad it helped you. Of course, my pleasure!
      Here is a newer one for you:
      ruclips.net/video/JQVQcNN0cXE/видео.html

  • @dhirajalate3168
    @dhirajalate3168 10 месяцев назад +1

    elegence and clean..thankyou:)

    • @CloudWithDjango
      @CloudWithDjango  10 месяцев назад

      Thank you so much! :)
      Of course, my pleasure!

  • @santanu_04
    @santanu_04 Год назад +2

    Your tutorials are awesome 👍.

  • @gloryeziani
    @gloryeziani Год назад +1

    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?

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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!

  • @s.nazeri2009
    @s.nazeri2009 10 месяцев назад

    perfect .Thank you .

  • @stentechy3346
    @stentechy3346 Год назад +1

    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.

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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.

  • @sheraziskt
    @sheraziskt Год назад

    thanks

  • @rajarajan1338
    @rajarajan1338 13 дней назад

    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.

    • @CloudWithDjango
      @CloudWithDjango  13 дней назад +1

      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.

    • @rajarajan1338
      @rajarajan1338 13 дней назад

      @@CloudWithDjango Thankyou

    • @CloudWithDjango
      @CloudWithDjango  12 дней назад

      My pleasure!

  • @emilyalva2169
    @emilyalva2169 Год назад

    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?

    • @CloudWithDjango
      @CloudWithDjango  Год назад +1

      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!

  • @zakchips
    @zakchips Год назад

    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?

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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!

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/

  • @DirtySouth33T
    @DirtySouth33T Год назад

    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?

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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.

  • @user-ts3ew6vl9n
    @user-ts3ew6vl9n Год назад

    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

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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.

  • @andreiarseni362
    @andreiarseni362 Год назад

    Your static files are in a static folder including images,but what to do if my images are in a folder called media?

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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.

    • @andreiarseni362
      @andreiarseni362 Год назад

      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?

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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.

    • @andreiarseni362
      @andreiarseni362 Год назад

      @@CloudWithDjango At least tell me how to put my media folder inside static like yours

  • @wiz3379
    @wiz3379 Год назад

    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).

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      Hi,
      Please check your Python installation and that you application is working locally.

  • @ritik1857
    @ritik1857 Год назад

    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?

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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.

  • @cmacompilation4649
    @cmacompilation4649 4 месяца назад

    where can we find the final code for this please ?

    • @CloudWithDjango
      @CloudWithDjango  4 месяца назад

      Hi,
      The final code is not available for this series unfortunately

  • @sameerbajaj8927
    @sameerbajaj8927 Год назад

    why is it a better idea to store your static files on s3 rather than directly uploading on a hosting like render or heroku ?

    • @CloudWithDjango
      @CloudWithDjango  Год назад +1

      Hi Sameer,
      Amazon S3 is more robust and manageable than alternative options

    • @sameerbajaj8927
      @sameerbajaj8927 Год назад

      @@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.

    • @CloudWithDjango
      @CloudWithDjango  Год назад

      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.

  • @nicenaija9952
    @nicenaija9952 2 года назад

    Can you do a tutorial on Django with docker + postgress? And deployment of the container?

    • @CloudWithDjango
      @CloudWithDjango  2 года назад +1

      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.

    • @nicenaija9952
      @nicenaija9952 2 года назад

      @@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.

    • @CloudWithDjango
      @CloudWithDjango  2 года назад

      @@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!