supabase / storage-py

Home Page:https://supabase-community.github.io/storage-py/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

storage upload mp3 file is defaulting to text/plain MIME type

jeo1kim opened this issue · comments

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:
supabase.storage.from_(PUBLIC_BUCKET).upload(f"{filename}", join('/tmp', filename), file_options={"cache-control": "3600", "content_type": "audio/mpeg", "x-upsert": "false"})

    I am using nextjs, vercel and python. I am calling the upload function but am getting this error. When I open the bucket to accept all MIME type I can see the file being created but the MIME type is text/plain. 
    
    `supabase.storage.from_(PUBLIC_BUCKET).upload(f"{filename}", join('/tmp', filename), file_options={"content_type": "audio/mpeg"})

File "/var/task/storage3/_sync/file_api.py", line 232, in upload
return self._request(
File "/var/task/storage3/_sync/file_api.py", line 48, in _request
raise StorageException(
storage3.utils.StorageException: {'statusCode': 400, 'error': 'invalid_mime_type', 'message': 'mi`

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

I have the same issue with images - would love to know how to fix - cant find any docs on this.

I think maybe the file_options parameter should look like this file_options={"content-type": "audio/mpeg"} , I had the same issue with pdf files but tracking down the upload function helped me, here is its code

    def upload(
        self, path: str, file: Union[str, Path], file_options: Optional[dict] = None
    ) -> Response:
        """
        Uploads a file to an existing bucket.

        Parameters
        ----------
        path
            The relative file path including the bucket ID. Should be of the format `bucket/folder/subfolder/filename.png`.
            The bucket must already exist before attempting to upload.
        file
            The File object to be stored in the bucket. or a async generator of chunks
        file_options
            HTTP headers. For example `cache-control`
        """
        if file_options is None:
            file_options = {}
        headers = {**self._client.headers, **DEFAULT_FILE_OPTIONS, **file_options}
        filename = path.rsplit("/", maxsplit=1)[-1]

        if (
            isinstance(file, BufferedReader)
            or isinstance(file, bytes)
            or isinstance(file, FileIO)
        ):
            # bytes or byte-stream-like object received
            files = {"file": (filename, file, headers.pop("content-type"))}
        else:
            # str or pathlib.path received
            files = {"file": (filename, open(file, "rb"), headers.pop("content-type"))}

        _path = self._get_final_path(path)
        return self._request(
            "POST",
            f"/object/{_path}",
            files=files,
            headers=headers,
        )

Hey team,

As @radfaz mentioned you'll probably want to specify the mime_type like

with open("/content/sample-5s.mp4", 'rb') as f:
  supabase.storage.from_("testbucket").upload(file=f,path="sample.mp4", file_options={"content-type": "audio/mpeg"})

Will update the docs to reflect this.

List of relevant mime types: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

Going to close but please feel free to re-open if there are further issues

commented

why we pop the "content-type" from the header option, so no matter what the user set ,it always use the "text/plain". @J0 @radfaz