supabase / cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
https://supabase.com/docs/reference/cli/about
MIT License
1.02k stars 201 forks source link

Add missing cache control and content type to `supabase storage` cli sub-commands #1895

Closed daniel-j-h closed 7 months ago

daniel-j-h commented 7 months ago

Is your feature request related to a problem? Please describe.

When using the supabase cli to interact with the storage layer, two crucial file options seem to be missing

  1. the cache control option to customize caching, and
  2. the content type option, to customize mime types

Both file options seem to be present e.g. in the javascript library

Compare

These two missing options are crucial for certain workflows and use cases and the supabase storage cli would greatly benefit in supporting them. Otherwise the only workaround is using the javascript library for one-off scripts for these specific file upload use-cases, defeating the purpose of having the supabase storage cli in the first place.

Describe the solution you'd like

Ideally the supabase storage cli sub-commands creating or modifying files get two new optional arguments:

Example usage

supabase storage cp --cache-control 'public, max-age=30758400, immutable' myfile.ext ss:///mybucket/myfile.ext
supabase storage cp --content-type 'text/html' myfile.ext ss:///mybucket/myfile.ext

Describe alternatives you've considered

None

Additional context

As an inspiration, check the aws cli

aws s3 cp \
  --cache-control 'public, max-age=30758400, immutable' \
  --content-type 'binary/octet-stream' \
  myfile.ext s3://mybucket/myfile.ext
daniel-j-h commented 7 months ago

Here is one example to reproduce this

  1. Use supabase storage cp to upload an index.html file
  2. Check the response at https://xxx.supabase.co/storage/v1/object/public/mybucket/index.html

See how


mime1 mime2

daniel-j-h commented 7 months ago

Here is a workaround to upload a file with cache control and content type until we have it in the supabase storage cli

curl --proto =https --tlsv1.3 'https://xxxx.supabase.co/storage/v1/object/mybucket/index.html' \
  -H "Authorization: Bearer $SUPABASE_SERVICE_KEY" \
  -H 'Cache-Control: max-age=3600' \
  -H 'Content-Type: text/html; charset=utf-8' \
  --data-binary '@/tmp/index.html'

Note: an unrelated issue I ran into (also visible in the screen grab above) and what's not obvious as a user:

The content type text/html is not supported on the storage API side at the moment

Even if we set text/html manually, we'll get a text/plain response back from the served content. It would be great to have this in the documentation somewhere. More on this in https://github.com/orgs/supabase/discussions/7377

sweatybridge commented 7 months ago

Thanks for the suggestion. Currently the content-type is automatically determined based on the first 512 bytes of the file being uploaded and the cache-control defaults to max-age=3600.

In the linked PR, I'm adding support for custom --cache-control and --content-type flags so that you can override these defaults. This will be released to beta version v1.141.0.