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 apathname
.
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' })
}
}