supabase-js icon indicating copy to clipboard operation
supabase-js copied to clipboard

Supabase JS Storage not returning ID

Open simonsayssa opened this issue 1 year ago • 8 comments

Supabase JS is not returning the ID of the uploaded file. After checking the storage API latest update, the ID is being returned after calling the "upload" API. However, in the supabase-js storage.upload() function, only the path (string) is returned. Kindly add the file id to the response since some users will link the file id to their database tables.

Steps to Reproduce:

const { data: uploadFileData, error: uploadFileError } = await supabaseClient
    .storage
    .from(bucketName)
    .upload(path, file, {
      {},
      cacheControl: "3600",
      upsert: true
    });

the id was not returned.

Expected Behavior: the response data object should contain id.

After going through the code in supabase-js package, I noticed that the ID is being returned by the storage API but the structure of the returned object was {"path": string}. Kindly fix it to { id: string; path: string; fullPath: string }

simonsayssa avatar Dec 05 '23 09:12 simonsayssa

@simonsayssa can you elaborate more on this? I am not able to reproduce this, also after digging into code, i found that in supabase-js, there is one function named 'storage' which exports whole storage-js client

get storage() {
    return new SupabaseStorageClient(this.storageUrl, this.headers, this.fetch)
}

sumitbhanushali avatar Dec 10 '23 11:12 sumitbhanushali

the bug is under the "upload" function in StorageFileApi.ts. If you notice in the code above, the issue is not the storage function, but in the supabaseClient.storage().from().upload() function. The code is currently:

async upload(
    path: string,
    fileBody: FileBody,
    fileOptions?: FileOptions
  ): Promise<
    | {
        data: { path: string }
        error: null
      }
    | {
        data: null
        error: StorageError
      }
  > {
    return this.uploadOrUpdate('POST', path, fileBody, fileOptions)
  }

while it should be:

async upload(
    path: string,
    fileBody: FileBody,
    fileOptions?: FileOptions
  ): Promise<
    | {
        data: { id: string; path: string; fullPath: string }
        error: null
      }
    | {
        data: null
        error: StorageError
      }
  > {
    return this.uploadOrUpdate('POST', path, fileBody, fileOptions)
  }

The issue is solved by adding the id and fullPath to the response interface of the upload function in StorageFileApi.ts under the storage-js package

ssayssa avatar Dec 12 '23 07:12 ssayssa

did you check with git blame whether this was intentional.

Also, you can raise the PR for the fix

sumitbhanushali avatar Dec 12 '23 07:12 sumitbhanushali

Any updates? Id field is needed a lot

nikitastryuk avatar Feb 15 '24 17:02 nikitastryuk

Facing the same problem here, but I need the fullPath property, which is also not implemented in the interface.

matheusdamiao avatar Mar 21 '24 17:03 matheusdamiao

The only workaround that worked for me it was to cast using 'as'

const data = await supabase.storage
          .from(bucket)
          .upload(file.name, file);
const castedResponse = data as SupabaseUploadResponse; 

implementing my own interface:

interface SupabaseUploadResponse {
  data: { path: string; fullPath: string } | null;
  error: any | null;
}

You guys can just add an ID and be happy. It's not the best solution, but as I said, it's a workaround.

matheusdamiao avatar Mar 21 '24 18:03 matheusdamiao

Was this ever fixed? It seems odd that the ID does actually come back in the http response but the typing of the {data} response masks this.

pariah140 avatar Jun 18 '24 10:06 pariah140

@pariah140 I don't think there was an official fix, however, @matheusdamiao solution did work with a small tweak. (Just added id)

export interface SupabaseUploadResponse {
  data: { id: string; path: string; fullPath: string } | null;
  error: any | null;
}
  const { data, error } = (await client.storage
    .from('documents')
    .upload(
      userId + '/' + sanitizedFileName,
      file,
      {}
    )) as SupabaseUploadResponse;

andreellisjunior avatar Jul 16 '24 13:07 andreellisjunior