Next.js Discord

Discord Forum

Vercel Blob + NextJS - How can I set custom pathname on the server before uploading a large file?

Unanswered
Black carp posted this in #help-forum
Open in Discord
Black carpOP
I started experimenting with Vercel Blob in my NextJS API app and my current set up is the code below (I can't directly call the put function from my API route because my files are larger than 4.5mb which is over the size limit of the routes / server functions), and I'm trying to figure out how to set my own custom pathname to the file I upload, I have to add a userId prefix before uploading the file and I can't figure out if it's even possible which is very weird because I feel like that's the most basic feature.

My code: (everything works fine I just need to set custom pathname on the server)

Client:
  import { upload } from "@vercel/blob/client";

  const newBlob = await upload(file.name, file, {
    access: "public",
    handleUploadUrl: `${TAP_PAGES_API_BASE_URL}/dev/upload`,
  });


Server:
const body = (await request.json()) as HandleUploadBody;

  const jsonResponse = await handleUpload({
    body,
    request,
    onBeforeGenerateToken: async (pathname) => {
      return { 
        //...options... //
      };
    },
    onUploadCompleted: async ({ blob, tokenPayload }) => {
      // some code
    },
  });

  return NextResponse.json(jsonResponse);

8 Replies

Black carpOP
Up
White-cheeked Pintail
${userId}-${file.name}
The pathname can be any string

https://vercel.com/docs/vercel-blob/using-blob-sdk#upload
@White-cheeked Pintail `${userId}-${file.name}` The `pathname` can be any string https://vercel.com/docs/vercel-blob/using-blob-sdk#upload
Black carpOP
Why is the client the one that decides the pathname of the file tho? it could be used by malicious actors... for now I throw an error inside onBeforeGenerateToken if the provided pathname isn't what I want it to be, but for some reason even when erorr is thrown in this "before" function and the server didn't make (or shoudln't make yet) a request to vercel-blob the dashboard still counts the action as "advanced operation" so weird...

Couple more questions:
1. Is it possible to get the size of an entire folder without getting all it's children and manually calculating the size?
2. Is it possible to get the bandwidth a folder / file used during a period (could be just since the beginning of the month or since a specific date), I can see in the vercel blob dashboard the usage per file but can't figure out how to get that info via the @vercel/blob package
@White-cheeked Pintail You can generate the name in `onBeforeGenerateToken` if you want as well. I’ll would take a look at this: <https://vercel.com/docs/vercel-blob/client-upload#create-a-client-upload-route> as for your other questions 1. Not to my knowledge, you have to use list and manually add the size of each 2. I don’t think so, just in the dashboard
Black carpOP
The link you provided (I specifically looked at the anchor point as well) doesn't show or say how to set the pathname in the server... and returning custom pathname in the onBeforeGenerateToken function doesn't do anything unfortunately
ur best bet might be to have userId added in the pathname parameter in upload() and just verify the user properly
@White-cheeked Pintail ur best bet might be to have userId added in the pathname parameter in upload() and just verify the user properly
Black carpOP
Yeah that's my current approach, I think something is broken by the way vercel calculates the advanced operation usage I perform a single action and refresh the dashboard and I see the advanced operations jump by 4~9 operations (if it was 100 before I performed a single upload, after I perform the upload sometimes it jumps to 104 and sometimes to 109) thats very strange behaviour especially because this is a brand new blob storage that a few hours ago I created and it's only been used in my local machine, no code with @vercel/blob has been shipped yet and I am the only one with access to this vercel account... :/