Duplicate in Object storage

Viewed 298

I use a Minio-backend in the Django app. I have a feature that users can use the object storage(user online gallery), and Also, users can upload a new image from the device to create a post. But when I use the object storage images and create an image, images duplicate in object storage(because each time I create a post, I want to upload new images( images that upload locally) in object storage). What should I do to prevent these duplicates?

It is my model:

        class MediaStorage(models.Model):   
                    file = models.FileField(verbose_name="Object Upload",
                                storage=MinioBackend(bucket_name='django-backend-dev-private'),
                                upload_to=iso_date_prefix)

this is my create new post :

class CreatePostView(generics.CreateAPIView):


 ...
    def post(self, request, *args, **kwargs):
            user = request.user
            data = request.data
    
            ...
        for media_file in post_files:
            file_team=post_team
            f = MediaStorage.objects.create(team=file_team,owner=user)
            f.media=media_file
            f.save()
            post.multimedia.add(f)

        return Response(post_serializer.PostSerializer(post).data, status=status.HTTP_201_CREATED)

Thank you so much.

1 Answers

I think you shouldn't upload directly,u could get image md5 first,and a attr to MediaStorage to save md5 value,before upload check is there same md5 in DB,If has that may you already upload this image before

Related