django-storages icon indicating copy to clipboard operation
django-storages copied to clipboard

Adding a Custom Storage backend for Supabase

Open J0 opened this issue 3 years ago • 6 comments
trafficstars

Hey,

Thanks for creating this project! I am currently maintaining(as part of a team) the python client library for Supabase which is (loosely speaking) an Open Source Firebase. One of the components of Supabase is Supabase Storage which is an S3 like file storage. To allow users the use the Storage system easily we were hoping to add a custom backend for Django in this repository.

We are working on the custom backend in a separate repo but I was hoping that we could file a PR to this repo to integrate it as a custom backend once it is done. Given the widespread adoption of django-storages I think that Supabase integration would make it easier for users to use Supabase Storage and/or choose Supabase Storage in tandem with other storage providers.

Would love to hear if the django-storages team has any thoughts/concerns. Lmk!

Thanks :) Joel

J0 avatar Dec 25 '21 11:12 J0

@J0 is there any chance supabase can work with Django's ORM?

hector97i avatar Feb 10 '22 01:02 hector97i

Hey @hector97i,

Apologies for the delayed reply. I'm hoping that the package above will help use use Supabase Storage with the ORM. Right now, we should be able to use the rest of Supabase with the Django ORM directly as the Supabase is backed by Postgres which is supported by the Django ORM.

J0 avatar Feb 19 '22 02:02 J0

Hey @hector97i,

Apologies for the delayed reply. I'm hoping that the package above will help use use Supabase Storage with the ORM. Right now, we should be able to use the rest of Supabase with the Django ORM directly as the Supabase is backed by Postgres which is supported by the Django ORM.

Any tutorial on how to integrate supabase's storage with Django so that uploaded files through Django apps can be stored in supabase?

rukshar69 avatar Apr 26 '23 06:04 rukshar69

Hey! @rukshar69 I faced a similar problem, did you find any solution?

NBKRTM avatar Nov 07 '23 15:11 NBKRTM

Hey @NBKRTM and @rukshar69,

Sorry it's been a bit busy. What would you like to see in a tutorial if one was written?

J0 avatar Nov 07 '23 16:11 J0

Hey @NBKRTM and @rukshar69,

Sorry it's been a bit busy. What would you like to see in a tutorial if one was written?

I'd say overriding these methods making use of the python client library for Supabase would suffice.

joaorighetto avatar Dec 20 '23 12:12 joaorighetto

Any update on this on how to integrate it

talhaanwarch avatar Mar 03 '24 11:03 talhaanwarch

Here is the code I wrote . Thanks @joaorighetto for the hint. I created a file storage.py at same level where settings.py file is

class SupabaseStorage(Storage):
    def __init__(self, bucket_name="blog_storage", **kwargs):
        self.bucket_name = bucket_name

        self.supabase = create_client(settings.SUPABASE_URL, settings.SUPABASE_KEY)
        self.storage_client = self.supabase.storage.from_(self.bucket_name)

    def _open(self, name, mode="rb"):
        # Implement the method to open a file from Supabase
        pass

    def _save(self, name, content):
        # Implement the method to save a file to Supabase
        content_file = content.file
        content_file.seek(0)  # Move the file pointer to the beginning
        content_bytes = content_file.read()
        data = self.supabase.storage.from_(self.bucket_name).upload(
            name, content_bytes, {"content-type": content.content_type}
        )
        return data.json()["Key"]  # name/path of the file

    def exists(self, name):
        # Implement the method to check if a file exists in Supabase
        pass

    def url(self, name):
        # Implement the method to return the URL for a file in Supabase
        return f"{settings.SUPABASE_URL}/storage/v1/object/public/{name}"

then add this line in settings.py

DEFAULT_FILE_STORAGE = "blog_project.storage.SupabaseStorage"

talhaanwarch avatar Mar 03 '24 14:03 talhaanwarch

Thanks all - I haven't really had the time to look into this - appreciate everyone jumping on. I'll see if we can get this listed somewhere in the Supabase tutorial section.

J0 avatar Mar 03 '24 16:03 J0

Would accept a PR for this or a docs one since there is the S3 compat version now.

jschneier avatar Apr 25 '24 03:04 jschneier