简体   繁体   中英

Uploading Images to S3 Bucket and Storing the URL in Django DB

I have a react native application which uses Django as the backend. REST APIs are used to send and retrieve data from the DB. Everything is smooth till the point where I have text information to be uploaded to DB which is easily handled by REST API. How do I upload images to S3 from the react native application and store the URL in my DB. Do I use an Image Field or is a regular CharField enough ? Do I need to configure S3BOTO in my Django application ?

You can use django-storages to setup the upload backend to use s3, then you just setup a ImageField

class MyModel(models.Model):
    ...
    image = models.ImageField(upload_to='my_image_folder')

The upload_to parameter will use an existing folder on your s3 setup, or create a new folder. Your image will be uploaded, and when you serialize your model, you will get the url for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM