How to pull or read image in Django from AWS s3 bucket for OpenCV to process?

Question:

When user uploads images from (react as frontend) and django receives the images from static folder and begins to process those images and later user can download.

Below is my django python code

imagePath = os.path.commonprefix(['images'])  #<------coming from static files when user            
                                                 #      upload via react axios.post. instead 
                                                 #     of "imagePath" i want django to read 
                                                 #    from s3 bucket 'images' folder.
#other process

for img in imagePath:
    image = cv2.imread(img)
    ##other process

but now i’m using amzazon s3 instead of local static files.

AWS_ACCESS_KEY_ID = '********'
AWS_SECRET_ACCESS_KEY = '***************'
AWS_STORAGE_BUCKET_NAME = '********'
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

my question is :

How to make django to read images directly from s3 bucket? so that instead of "imagePath" i can use s3 files?

Any related answer will be really appreciated thank you 🙂
I hope i get response ! i’am even ready to shift to any server if my code plan works there ! so give me any idea how to work my plan.

Hope i get some response !

Asked By: Epic Gamers

||

Answers:

Here Is the Best Solution. Link Image read by presignedurl and in Opencv.

#You are missing the url settings for static and media. Here is what to you can do, based on my days of struggle on the matter of S3 – django – static files.

1. Make sure you have the right S3 Permissions and Policy in place. Access ‘Policy’ via permissions tab in AWS S3 console for your specific bucket.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Statement1",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<your_bucket_name>/*"
        },
        {
            "Sid": "Statement2",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::<your_bucket_name>/images/"
        }
    ]
}

2. Install WhiteNoiseMiddleware & django-storages in your project environment.

pip install whitenoise
pip install django-storages

3. Add the following to MIDDLEWARE= in settings.py

'whitenoise.middleware.WhiteNoiseMiddleware',

4. Following additions in settings.py are required to handle URLs from S3 correctly. The handling is done by django middleware & django-storages automatically

STATICFILES_LOCATION = 'static'
MEDIAFILES_LOCATION = 'media'

AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % os.environ['BUCKET_NAME']
AWS_ACCESS_KEY_ID = os.environ['AWS_KEY']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_ACC_KEY']
AWS_STORAGE_BUCKET_NAME = os.environ['BUCKET_NAME']
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'

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

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)

5. For aiming uploads into a precise S3 Bucket folder. (Additional)

In setting.py set media root:

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

In models.py use ImageFiled and add upload_to= takes in a folder name and creates it with the first upload!:

image_variable = models.ImageField(null=True, default="{default_filename)", upload_to='uploads/') 

Reference: django-storages , whiteNoiseMiddelware, S3 Access Troubleshooting

Answered By: zora