I’m pretty new to Netlify, so please forgive me if I’m missing something that should be obvious, but I’m having trouble both accessing Identity context and using Blobs from the same Function.
A couple of days ago I implemented a couple of functions to save a Blob and to get a Blob. Let’s take a simplified version of the get a blob function as an example:
import { getStore } from "@netlify/blobs"
export default async (req, context) => {
const itemId = new URL(req.url).searchParams.get('id')
const items = getStore("items")
const item = await items.get(itemId, { type: 'json' })
if (!item) {
return new Response('no item found with id', {
status: 404
})
}
return new Response(JSON.stringify(item))
}
These two functions have been working just fine for me. But now I want to start to associate these blob items with an Identity user using their sub as a blob key.
I was unable to find a way to get Identity information from the function as written above. Despite me including the proper authorization header in my requests, the context
variable never had any user information for me. Which makes sense because the Functions and Identity docs mention a clientContext
property but the context object docs don’t mention it. However, the Lambda compatibility for Functions docs mention that the context
will contain Identity information if it is passed.
So I implemented a new function using the Lambda-compatible format to fetch Identity information from the context.
const handler = async function (req, ctx) {
const user =
(!('clientContext' in ctx) || !('user' in ctx.clientContext))
? null : ctx.clientContext.user
if (user === null) {
return {
statusCode: 200,
body: JSON.stringify({message: 'hello anonymous'})
}
} else {
return {
statusCode: 200,
body: JSON.stringify({message: `hello, ${user.email}`})
}
}
}
export { handler }
And that works just fine.
So then I went to go move the Blobs functionality into the Lambda-compatible function and I started getting an error.
import { getStore } from "@netlify/blobs"
const handler = async function (event, ctx) {
const items = getStore("items")
const item = await items.get('static-item-id', { type: 'json' })
if (!item) {
return {
body: 'no item found with id',
statusCode: 404
}
}
return {
body: JSON.stringify(item),
statusCode: 200
}
}
export { handler }
This function in both local using netlify dev
and a preview deploy throws an error on getStore
: MissingBlobsEnvironmentError: The environment has not been configured to use Netlify Blobs. To use it manually, supply the following properties when creating a store: siteID, token
Searching this forum and chatting with the AI bot seem to suggest I need to supply a Netlify API key, but I haven’t been able to successfully do that - most of the examples seem to be for build functions which I am not doing. And even if I could get that working, that doesn’t seem like a good solution.
So…am I missing anything here? Is there a way to get Identity information from a non-Lambda function or use Blobs from a Lambda function so that I can do both of these things together?
Hopefully I’ve provided enough info as this doesn’t seem to be site specific, but please let me know if I can provide anything else to assist in troubleshooting.
Thanks!