Django multiple image upload to S3 - python

I am struggling to find a solution for a photo gallery in django. I've checkout out every django photo package (photologue, imagekit, etc.) I could find an none of them are really helping me in my quest.
I am trying to build a simple image gallery that will allow me to upload multiple photos from the django admin and store them directly in Amazon S3. Can someone please point me to a simple photo app capable of achieving this or what would be the simplest approach?
Thank you so much

This can be accomplished with django-storages configured to use S3. After setting up django-storages with S3, you can simply use add the image field to your model:
some_image = models.ImageField(upload_to='images')
where images is the subkey/directory in the bucket specified in your setup.
The process is outlined here and here.
In order to setup multi-image uploading, you can use: django-admin-multiupload or django-photologue (built-in S3 support)
or see one of the similar questions: Uploading multiple images in Django admin

Related

Flask - Serving user-uploaded images to the webpage

So I am working on a Flask application which is pretty much a property manager that involves allowing users to upload images of their properties. I am new to Flask and have never had to deal with images before. From a lot of Googling I understand that there are various ways to manage static files like images.
One way is to allow users to upload images directly to the file system, and then displaying it by retrieving the file location in the static folder using something like:
<img src="static/images/filename.jpg">
However, is this really an efficient way since this means storing generating and storing the location of each image URL in the database? Especially when it comes to deploying the application? Another way I discovered was using base64 encoding and storing the image directly into the database, which also doesn't sound very efficient either.
Another way, which I think might be the best to go about this, is to use an AWS S3 bucket. The user would then be able to upload an image directly to that bucket and be assigned a URL to that image. This URL is stored in the database and can then be used to display the image similarly to the file system method. Is my understanding of this correct? Is there a better way to go about this? And is there something similar to django-storages that can be used to connect Flask to S3?
Any input or pointing me in the right direction would be much appreciated. Thank you!
If you want to store the images in the web server then the best approach for you is to use nginx as proxy in front of flask and let nginx serve the static folder for all the images.
Nginx is pretty much enough for a small website. Don't try to serve the file using flask. It is too slow.
If you want to store the images in s3 ,then you just need to store the name of image in bucket in the database. You can tell flask to use s3 bucket as the static folder. You can use boto3 library in python to access s3.
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html
If you are concerned of exposing s3 bucket to users, then you can use cloudfront distribution. It is cheaper in terms of price to serve and also hides your bucket.

Setting Up S3 with Heroku and Django with Images

so I currently have my static files (js and css) just being stored on Heroku which is no biggie. However, I have objects that I need to store multiple images too and be able to get those images on request. How would I store a reference to those images?
I was planning to use a S3 Direct File Upload using these steps on Heroku here. Is this also going to be the best way for me to do so?
Thank you in advance.
I don't think setting up static (css,js,etc..) or media (images, videos) to be stored on S3 has anything to do with Heroku or where you deploy. Rather, its just making sure Django knows where to save the files, and where to fetch them. I would definitely not follow that link, because it seems confusing and not helpful when working with Django.
This tutorial has really helped me, as it will show you how to set all of that up. I have gone through these steps and can confirm it does the trick. https://simpleisbetterthancomplex.com/tutorial/2017/08/01/how-to-setup-amazon-s3-in-a-django-project.html
While I've gone this route in the past, I've recently opted to use Digital Ocean's one-click app - Dokku. It's based on Herokuish. I then use Dokku's persistent storage to take advantage of the 25 gigs of storage on DO's smallest, $5/month, plan. I wrote a guide to this here.

Uploading contents of Django FileField to a remote server

In a Django project of mine, users upload video files. Initially, I was uploading them directly to Azure Blob Storage (equivalent to storing it on Amazon S3). I.e. in models.py I had:
class Video(models.Model):
video_file = models.FileField(upload_to=upload_path, storage=OverwriteStorage())
Where OverwriteStorage overrides Storage in django.core.files.storage, and essentially uploads the file onto Azure.
Now I need to upload this file to a separate Linux server (not the same one that serves my Django web application). In this separate server, I'll perform some operations on the video file (compression, format change), and then I'll upload it to Azure Storage like before.
My question is: given my goal, how do I change the way I'm uploading the file in models.py? An illustrative example would be nice. I'm thinking I'll need to change FileField.upload_to, but all the examples I've seen indicate it's only to define a local filesystem path. Moreover, I don't want to let the user upload the content normally and then run a process to upload the file to another server. Doing it directly is my preference. Any ideas?
I've solved a similar issue with Amazon's S3, but the concept should be the same.
First, I use django-storages, and by default, upload my media files to S3 (django-storages also supports Azure). Then, my team set up an NFS share mount on our Django web servers from the destination server we occasional need to write user uploads to. Then we simply override django-storages by using "upload_to" to the local path that is a mount from the other server.
This answer has a quick example of how to set up an NFS share from one server on another: https://superuser.com/questions/300662/how-to-mount-a-folder-from-a-linux-machine-on-another-linux-machine
There are a few ways to skin the cat, but this one seemed easiest to our team. Good luck!

Serving private images through S3 in django

I am developing a django application that allows users to upload photos and view them and these photos are stored as private in S3. Now everytime I have to show them the thumbnails, i generate a url and give it to the template. This process is really very slow and takes very long time.
I am hoping there is some other way that i havent explored, please help me out. I was hoping for something like x-sendfile, where i authenticate the user and than redirect it to S3. Please let me know if I am missing out anything
I forked sorl-thumbnail to make it fast with S3. My code is here sorl_thumbnail-async
But I came to know easy_thumbnails does exactly what I was trying to do, so I am using it in my current project. Sorl is not updated since last year, use easy_thumbnails with remote storages like S3. You might find useful my post on the topic here
[Edit]: sorl-thumbnail now has new maintainers and is updated with latest django releases.
You can use sorl-thumbnail to serve thumbnails with pluggable S3 backend support and memcached or redis for caching.
You might find this question helpful: Storing images and thumbnails on s3 in django

Management of photos for Django applications

My application uses a photo slideshow(diaporama). In the back-end management I would provide the path to the folder that contains the photos. How can I handle the management of photos in Django?
I would use Photologue:
Features:
Complete photo gallery solution
Manipulation functionality
Fully inlcuded in Django Admin interface
What about using the django filebrowser and reference your photos from the folders you upload to?
django-filebrowser

Categories

Resources