'Get the Image from the Private S3 bucket and convert the image to blob using django to give it to React Js
I having a logo field as Imagefield and i connected the filepath to s3 bucket. Once i upload a image in logo it will be uploaded in s3, if i delete or modify it working. But when i want to get the image to show that image in the frontend(reactjs) iam generating a url method where the aws credentials are shown in the url itself.
But i need to change the image as blob(binary) and give that data to the frontend for the GET call. I dont know how can be it done, refered many sites but still not clear. Please help me solve this issue.
Models.py
class Organisation(models.Model):
"""
Organisation model
"""
org_id = models.AutoField(unique=True, primary_key=True)
org_name = models.CharField(max_length=100)
org_code = models.CharField(max_length=20)
org_mail_id = models.EmailField(max_length=100)
org_phone_number = models.CharField(max_length=20)
org_address = models.JSONField(max_length=500, null=True)
product = models.ManyToManyField(Product, related_name='products')
org_logo = models.ImageField(upload_to=upload_org_logo, null=True, blank=True,)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def remove_on_image_update(self):
try:
# is the object in the database yet?
obj = Organisation.objects.get(org_id=self.org_id)
except Organisation.DoesNotExist:
# object is not in db, nothing to worry about
return
# is the save due to an update of the actual image file?
if obj.org_logo and self.org_logo and obj.org_logo != self.org_logo:
# delete the old image file from the storage in favor of the new file
obj.org_logo.delete()
class Meta:
db_table = "organisation_master"
def __str__(self):
return self.org_name
serializers.py
class Organisation_Serializers(serializers.ModelSerializer):
class Meta:
model = Organisation
fields = ('org_id', 'org_name','org_address', 'org_phone_number', 'org_mail_id','org_logo','org_code','product',)
depth = 1
views.py
class Organisation_Viewset(DestroyWithPayloadMixin,viewsets.ModelViewSet):
renderer_classes = (CustomRenderer, ) #ModelViewSet Provides the list, create, retrieve, update, destroy actions.
queryset=models.Organisation.objects.all()
parser_classes = [parsers.MultiPartParser, parsers.FormParser]
http_method_names = ['get', 'post', 'patch', 'delete']
serializer_class=serializers.Organisation_Serializers
settings.py
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
AWS_STORAGE_BUCKET_NAME = ''
AWS_QUERYSTRING_AUTH = True
AWS_S3_FILE_OVERWRITE = False
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_S3_REGION_NAME = ''
AWS_DEFAULT_ACL = None
AWS_S3_VERIFY = True
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
