uploadthing icon indicating copy to clipboard operation
uploadthing copied to clipboard

[BUG]: getting error on using utapi.deleteFiles

Open alifares10 opened this issue 3 years ago • 19 comments

hey, im getting "Missing UPLOADTHING_SECRET env variable." when trying to delete file.

this is my code : export const deleteFiles = async (fileKey: string) => { await utapi.deleteFiles(fileKey); };

i checked the env variables and everything is fine, the upload works

alifares10 avatar Jul 10 '23 08:07 alifares10

Hey, @alifares10

Where you using upload through UT components, hooks or UT API?

Mr0Bread avatar Jul 12 '23 09:07 Mr0Bread

im using uploadFiles function from generateReactHelpers

alifares10 avatar Jul 12 '23 09:07 alifares10

@alifares10 Are you calling utapi.deleteFiles on client or server?

Mr0Bread avatar Jul 12 '23 10:07 Mr0Bread

server

alifares10 avatar Jul 12 '23 10:07 alifares10

@alifares10 Where do you call it? API endpoint, server action?

Are you using create-t3-app starter or something else?

Mr0Bread avatar Jul 12 '23 11:07 Mr0Bread

im calling it in a server component, i moved it to a client one and still got the same problem

im using create next app

alifares10 avatar Jul 13 '23 07:07 alifares10

Were you able to fix it?

Mine worked by putting "use server" above and adding this to the next.config.js:

experimental: {
      serverActions: true,
   },

ProgrammerJaneki avatar Jul 13 '23 17:07 ProgrammerJaneki

im calling it in a server component, i moved it to a client one and still got the same problem

It definitely won't work in a client component, since it relies on environment variables that don't exist on client. We intended it to be used either in a /api route, or in TRPC for example. It should also work in server actions, but given the current state of those, I don't know that I would necessarily recommend them 😅

markflorkowski avatar Jul 13 '23 18:07 markflorkowski

Please reopen with a reproduction repo if this issue persists.

juliusmarminge avatar Jul 17 '23 22:07 juliusmarminge

Hi, I'm confused about the location of the utapi.deleteFiles. Where should I put it? is in api/uploadthing folder?

tung2307 avatar Aug 03 '23 06:08 tung2307

You can use the utapi functions anywhere that runs serverside, so yes, /api is a reasonable place to use it.

markflorkowski avatar Aug 03 '23 21:08 markflorkowski

so in /api/uploadthing I named deleteFile.ts, that I use the code as @alifares10

import { utapi } from "uploadthing/server";

export const deleteFiles = async (fileKey: string) => {
  
  await utapi.deleteFiles(fileKey);
};

When I called deleteFile(fileKey) from client, I got the same error "Missing UPLOADTHING_SECRET env variable."

tung2307 avatar Aug 03 '23 22:08 tung2307

When I called deleteFile(fileKey) from client, I got the same error "Missing UPLOADTHING_SECRET env variable."

Well, yes. Cause then you're not calling the utapi serverside. You'll jave to make it an API route and call it using fetch in that case.

juliusmarminge avatar Aug 04 '23 07:08 juliusmarminge

Ugh. I thought Next.js's best feature was that you don't have to make fetches to internal APIs!

But that aside, what's the best practice on how to secure this? Because I shouldn't make a fetch request with my private API key from a client component, right?

theDanielJLewis avatar Dec 22 '23 17:12 theDanielJLewis

Ugh. I thought Next.js's best feature was that you don't have to make fetches to internal APIs!

But that aside, what's the best practice on how to secure this? Because I shouldn't make a fetch request with my private API key from a client component, right?

How do you make mutations elsewhere in your app?

juliusmarminge avatar Dec 22 '23 18:12 juliusmarminge

I'm still a bit of a scrappy developer, so I hope I use the correct terminology.

I created "use server"; functions, like updateUser(), and then I call those from functions inside my components.

But UploadThing doesn't work that same way.

theDanielJLewis avatar Dec 22 '23 18:12 theDanielJLewis

Okay so you're using server actions. You can call utapi.deleteFiles() from that

juliusmarminge avatar Dec 22 '23 19:12 juliusmarminge

@juliusmarminge, I thought I was, but it was complaining that utapi.deleteFiles() was not a function.

theDanielJLewis avatar Dec 22 '23 20:12 theDanielJLewis

Did you create an instance of the UTApi?

import { UTApi } from "uploadthing/server";

const utapi = new UTApi(opts);

...

const deleteFile = async (key: string) => {
  "use server"
  // authenticate user
  // verify they have permission to delete 
  await utapi.deleteFiles(key);
}

juliusmarminge avatar Dec 22 '23 21:12 juliusmarminge