jschneier / django-storages

https://django-storages.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
2.74k stars 856 forks source link

Adding a Custom Storage backend for Supabase #1099

Closed J0 closed 5 months ago

J0 commented 2 years ago

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

hector97i commented 2 years ago

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

J0 commented 2 years ago

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.

rukshar69 commented 1 year ago

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?

NBKRTM commented 10 months ago

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

J0 commented 10 months ago

Hey @NBKRTM and @rukshar69,

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

joaorighetto commented 9 months ago

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.

talhaanwarch commented 6 months ago

Any update on this on how to integrate it

talhaanwarch commented 6 months ago

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"
J0 commented 6 months ago

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.

jschneier commented 5 months ago

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