Can't read the blobs that I upload?

I’ve started using blobs to store data in my vercel app, but I cannot access the blobs via a pathname.

I was hoping to just have a ‘get’ method to match ‘put’, but I only see list() and head().

List successfully shows me that I put() the blob correctly, and head() works if I pass the url in. However, when I pass the pathname in, it says:

The documentation for head says it accepts the following parameters: url: (Required) A string specifying the unique URL of the blob object to read. You can also pass only a pathname.) for head says:
It accepts the following parameters:

  • url: (Required) A string specifying the unique URL of the blob object to read. You can also pass only a pathname.

WHen I pass in the pathname (the one used to create the blob, which is the same one returned as pathname), however, it always says ‘Error saving data to Vercel Blob: BlobNotFoundError: Vercel Blob: The requested blob does not exist’.

I do see the blob in list() and on the vercel website.

Is this a bug or am I misunderstanding the documentation?

Here is some code for my API that is meant to save the blob I pass in, and I’m attempting to then read the blob back.

I’ve also created a separate api to just read the blob via the pathname and the same thing happens.

import { put, head } from '@vercel/blob'
import { nanoid } from 'nanoid'
import * as dotenv from 'dotenv'
import { VercelRequest, VercelResponse } from '@vercel/node'

dotenv.config()


export default async function handler(req: VercelRequest, res: VercelResponse) {
  try {
    if (req.method !== 'POST') {
      return res.status(405).json({ error: 'Method not allowed' })
    }

    // Extract JSON blob from the request body
    const { data } = req.body

    if (!data || typeof data !== 'object') {
      return res.status(400).json({ error: 'Invalid JSON blob provided' })
    }

    const key: string = nanoid(10)
    const path: string = `shares/${key}`

    // Save JSON blob to Vercel Blob
    const blob = Buffer.from(JSON.stringify(data)) // Convert JSON to binary
    const putResult = await put(path, blob, {
      contentType: 'application/json',
      access: 'public'
    })

    console.log('putResult', putResult)
    console.log('pathname', putResult.pathname)

    // Use the correct path for the head request
    const headResult = await head(putResult.pathname)

    console.log('headResult', headResult)

    res.status(200).json({ key })
  } catch (error) {
    console.error('Error saving data to Vercel Blob:', error)
    res.status(500).json({ error: 'Internal Server Error' })
  }
}

Hey @yipe,
The issue you are encountering is happening because your Blobs get a random suffix on upload. This is the default behavior of Vercel Blob to ensure a certain level of security.

The pathname returned from the Blob SDK will never contain that random suffix so that your app doesn’t have to worry about it.

It would be great if you could share more details about what you are trying to achieve with your code. In most cases, you should not need to call head right after put. That said, there is a workaround for your issue which looks like this:

// get pathname from blob URL
const url = new URL(putResult.url);
// Use the correct path for the head request
const headResult = await head(url.pathname.slice(1));

Keep in mind there are no benefits from using the pathname over the blob URL for head operations. This is a utility for when you know the pathname of the Blob so that you don’t need to construct the complete URL yourself.

You could also disable the random suffix completely like this:

const putResult = await put(path, blob, {
  contentType: "application/json",
  access: "public",
  addRandomSuffix: false,
});