Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listBuckets throws error #18

Closed
ChristophePRAT opened this issue May 18, 2023 · 22 comments
Closed

listBuckets throws error #18

ChristophePRAT opened this issue May 18, 2023 · 22 comments
Labels
bug Something isn't working

Comments

@ChristophePRAT
Copy link

Bug report

Listing buckets crashes the app.

Describe the bug

When I try to use .listBuckets() as mentioned here, my app throws an error (caught using fatalError with the name "Error"). The error status code is 400.

To Reproduce

  • Initialize the client
  • Run try await client.storage.listBuckets()

System information

  • OS: macOS
@ChristophePRAT ChristophePRAT added the bug Something isn't working label May 18, 2023
@nyeu
Copy link

nyeu commented Jun 3, 2023

Having the same issue - everything related to storage returns 400

@ChristophePRAT
Copy link
Author

It appears that some headers are not parsed correctly. The solution for me was to manually set them up.

@jason-dubon
Copy link

It appears that some headers are not parsed correctly. The solution for me was to manually set them up.

Hey Christophe,

I am having a similar issue. I tried with the SupabaseStorageClient and SupabaseClient but no luck on my end.

What did you pass in the headers ? and for which Supabase class ?

@ChristophePRAT
Copy link
Author

You need to use SupabaseStorageClient and parse the headers Authorization and apikey, like this example:

var apiKey = "YOUR_API_KEY"
var url = "https://YOUR_URL.supabase.co/storage/v1"

let client = SupabaseStorageClient(url: url, headers: ["Authorization": "Bearer \(apiKey)", "apikey": apiKey])

Hope this helps!

@nyeu
Copy link

nyeu commented Jun 5, 2023

I am still getting no buckets returned, no way to create a bucket and also no way to upload anything after trying with SupabaseStorageClient. I get a 400 "Invalid Input" when trying to create a bucket and 0 buckets returned when I list them. Uploading an image to a bucket I know it exists returns: Message too long. Any ideas/help will be appreciated.

The only thing that changed is that I do seem to be getting requests in the dashboard:

Screenshot 2023-06-05 at 23 07 07

@jason-dubon
Copy link

Thanks I'm little bit closer now. I added /storage/v1 to the end of the url (was getting a 404 error without it). Although, I am still getting an empty list as well. Did you do any other configurations. I currently have two buckets created that are both fully public.

@nyeu
Copy link

nyeu commented Jun 5, 2023

Yea same, with the tips @ChristophePRAT mentioned it feels I'm getting closer. I have also tried to substitute the Bear token with the session token rather than the apikey but no luck. I've created another bucket with no policies and public and it can't find it.
I am debugging with the error reports in Supabase storage dashboard dashboard and I can see every request I make but it returns an empty array. Somehow it just doesn't find the buckets created through the dashboard. Trying to create a dashboard through the app it also fails, giving this error:
"new row violates row-level security policy for table \"buckets\""

This is the closest I've been so far but very disappointed at the moment since I cannot do anything with storage :/

@jason-dubon
Copy link

jason-dubon commented Jun 5, 2023

I just got my buckets to list by inserting the service_role (secret) in the Authorization header

headers: [ "Authorization": "Bearer \(service_role)", "apikey" : apikey, ]
Screenshot 2023-06-05 at 5 31 02 PM

I will be testing/playing around with upload and download features next.

@nyeu
Copy link

nyeu commented Jun 5, 2023

omg you are a legend! I can see my buckets finally

@jason-dubon
Copy link

Haha it was a group effort!

By the way, I am currently working on a Supabase Swift series. Here is a link to it: https://www.youtube.com/playlist?list=PLWqniOnIerMYcqbMXAhdxAPDkGGNvpo7F

It's a little tricky to find good resources on using Supabase in Swift, but I have I put together all the info I have been able to gather on there.

@nyeu
Copy link

nyeu commented Jun 5, 2023

Awesome - I'll check it out! I'm currently using it in 2 different projects. Did you manage to upload something? I get the error message: Message too long. It's a jpg picture so it shouldn't be an issue but not sure what's going on.

@jason-dubon
Copy link

jason-dubon commented Jun 5, 2023

Yes, I was able to upload sucessfully.

let file = File(name: "profile_photo", data: photoData, fileName: "profile_photo.jpg", contentType: "jpg")

let result = try await storage.from(id: "images").upload(path: "\(user.uid)/profile_photo.jpg", file: file, fileOptions: FileOptions(cacheControl: "2400"))
I am passing the photoData (Data). My bucket is also a public one.

@jason-dubon
Copy link

hey @ChristophePRAT and @nyeu,

I noticed something interesting after implementing the Storage module. I have it successfully in my application, but when I try to sign out using the Supabase auth module, I get a new error.
api(GoTrue.GoTrueError.APIError(message: Optional("Body cannot be empty when content-type is set to \'application/json\'"), msg: nil, code: nil, error: Optional("FastifyError"), errorDescription: nil))

This goes away when I remove/comment any code thats using Supabase Storage.

@ChristophePRAT
Copy link
Author

This is strange. I think there definitely is a problem with how the headers are set by default. I will try to have a look and do a PR this weekend. Thanks for the additional information and for the solution you provided!

@grdsdev
Copy link
Contributor

grdsdev commented Jun 8, 2023

Please check usage on the integration test implementation at https://github.com/supabase-community/storage-swift/blob/main/Tests/SupabaseStorageTests/SupabaseStorageTests.swift

There's a local instance of Supabase now on the repository (https://github.com/supabase-community/storage-swift/tree/main/supabase), please use it to test your implementation.

For what I saw from this issue, I think its an issue with wrong RLS permissions, please make sure storage RLS is configured correctly and use the integration test and local supabase instance as an example.

If the issue persists, please let me know.

@ChristophePRAT
Copy link
Author

Hi folks!

I have understood more clearly what problems we are facing.
Here's a quick summary of the 3 problems we are facing in this issue:

  • @nyeu had trouble setting up the permissions for his storage database. He fixed the issue using his service_role key in the authorization header, which enabled him to bypass RLS
  • @MexJason encountered an issue with the authentication, which is related to the supabase-community/gotrue-swift repo. I will try to do a PR on this repo as well to remove the Content-Type header when it is not needed
  • My issue was related to the headers as well. I was using a SupabaseClient and accessing the storage module, but the reason why I stumbled into an error was that the Authorization header isn't set when initializing a SupabaseClient (c.f. this line of code). But my issue relates to the supabase-community/supabase-swift repo. I will therefore do a PR after updating the tests on that repo.

Please let me know if I missed something. Thanks a lot for helping me figure it out!

@grdsdev
Copy link
Contributor

grdsdev commented Jun 12, 2023

supabase/supabase-swift#87 got merged, can you please verify if it solves this issue?

@ChristophePRAT
Copy link
Author

I have just tested it. Works fine now!

@rtorcato
Copy link

Never use that key on front end code. I am able to upload from swiftui after setting my storage policy. Even tho the storage bucket is set to "public" you won't be able to upload without first setting the storage policy for inserting. I added ((bucket_id = 'images'::text) AND (auth.uid() = id)) to my policy. "images" being the name of my bucket.

@jason-dubon
Copy link

jason-dubon commented Jun 13, 2023

Thanks everyone!

Never use that key on front end code. I am able to upload from swiftui after setting my storage policy. Even tho the storage bucket is set to "public" you won't be able to upload without first setting the storage policy for inserting. I added ((bucket_id = 'images'::text) AND (auth.uid() = id)) to my policy. "images" being the name of my bucket.

This worked for me as well. However are you able to successfully sign out ?

I still get this issue when signing out after using the storage section.
api(GoTrue.GoTrueError.APIError(message: Optional("Body cannot be empty when content-type is set to \'application/json\'"), msg: nil, code: nil, error: Optional("FastifyError"), errorDescription: nil))

@rtorcato
Copy link

Thanks everyone!

Never use that key on front end code. I am able to upload from swiftui after setting my storage policy. Even tho the storage bucket is set to "public" you won't be able to upload without first setting the storage policy for inserting. I added ((bucket_id = 'images'::text) AND (auth.uid() = id)) to my policy. "images" being the name of my bucket.

This worked for me as well. However are you able to successfully sign out ?

I still get this issue when signing out after using the storage section. api(GoTrue.GoTrueError.APIError(message: Optional("Body cannot be empty when content-type is set to \'application/json\'"), msg: nil, code: nil, error: Optional("FastifyError"), errorDescription: nil))

yea I have no errors on sign out. I do see errors after logging in. It looks like some refresh token error since it comes up after a few seconds and will fill my console after awhile.

I tried adding delete function and looks like I needed to set select and delete storage policies, but after that deleting the image before uploading works fine. Without the proper storage policies deleting did not throw any errors at all.

I understand that supabase swift is still early in dev so I expect some errors.

@kevoniori
Copy link

Never use that key on front end code. I am able to upload from swiftui after setting my storage policy. Even tho the storage bucket is set to "public" you won't be able to upload without first setting the storage policy for inserting. I added ((bucket_id = 'images'::text) AND (auth.uid() = id)) to my policy. "images" being the name of my bucket.

I tried this and it doesnt seem to work for me. I also tried using the authtoken from the session in the headers and no access

Image 8-7-23 at 12 25 PM

Whenever I try to use my apikey the error returns to say invalid jwt. When I use the session token I get back a 400 error still but this time with no error message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants