vercel / vercel/storage

Pathname isn't being set by onBeforeGenerateToken

Open
#863 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
595
Forks
101
Avg merge
2h 24m
Merged PRs (30d)
1

Description

The title says it all!

Ive checked and my console logs are working just fine, all look legit (I think!)

PathName: tmp/StoreRequests/bacd36d979d76e760dabcc3f/images/bacd460c72902d264959fc8d.jpg
 POST /api/upload 200 in 269ms
export async function POST(request: Request): Promise<NextResponse> {
  const body = (await request.json()) as HandleUploadBody;
  try {
    const jsonResponse = await handleUpload({
      body,
      request,
      onBeforeGenerateToken: async (fileName, storeId) => {
        const user = await getServerUser();
        if (!user) {
          throw new Error("Unauthorized");
        }
        const id = generateUUID();
        const collection: TCollections = "StoreRequests";
        const pathname = `tmp/${collection}/${storeId}/images/${id}.jpg`;

        console.log("PathName:", pathname);

        return {
          pathname,
          allowedContentTypes: ["image/jpeg"],
          maximumSizeInBytes: 2 * 1024 * 1024, // 2MB
          tokenPayload: JSON.stringify({
            userId: user.id,
            storeId,
            filename: fileName, // Keep track of original name
            pathname,
            timestamp: Date.now(),
          }),
          cacheControlMaxAge: 365 * 24 * 60 * 60,
        } satisfies GenerateClientTokenOptions & { tokenPayload: string };
      },
      onUploadCompleted: async ({ blob, tokenPayload: _tokenPayload }) => {
        console.log("Blob upload completed:", blob.pathname);
      },
    });

    return NextResponse.json(jsonResponse);
  } catch (error) {
    console.error("Upload error:", error);
    return NextResponse.json(
      { error: (error as Error).message },
      { status: 400 },
    );
  }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the POST handler's handleUpload call and trace how onBeforeGenerateToken consumes the returned GenerateClientTokenOptions, especially pathname. Compare the logged pathname with the pathname on the completed blob in onUploadCompleted. Done means identifying why the callback pathname is not reflected in the uploaded blob and confirming the behavior with a reproducible upload.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.