gpt4 book ai didi

cloudflare - 什么是 cloudflare KV preview_ids 以及如何获得?

转载 作者:行者123 更新时间:2023-12-03 20:04:39 28 4
gpt4 key购买 nike

我有以下 wrangler.toml .当我想使用 devpreview (例如 npx wrangler devnpx wrangler preview )牧马人要求添加 preview_id到 KV 命名空间。这是现有 KV 命名空间的标识符吗?
我看到 Cloudflare Workers GH 有一张票,地址是 https://github.com/cloudflare/wrangler/issues/1458这表明应该澄清这一点,但票证已通过添加错误消息关闭

In order to preview a worker with KV namespaces, you must designate a preview_id in your configuration file for each KV namespace you'd like to preview."


这就是我在这里的原因。 :)
至于更大的上下文,如果有人能澄清一下,我会非常高兴:我看到如果我给出一个现有命名空间的值,我可以预览并看到类型为 __some-worker-dev-1234-workers_sites_assets_preview 的 KV 命名空间在 Cloudflare 中生成。这与 preview_id 中使用的标识符所指向的 KV 命名空间具有不同的标识符。以及我在 preview_id 中使用的标识符所指向的 KV 命名空间是空的。为什么提供现有 KV 命名空间的标识符会删除错误消息、部署 Assets 并允许预览,但实际的 KV 命名空间是空的并创建了一个新命名空间?
kv-asset-handler怎么办知道查看这个生成的命名空间来检索 Assets 吗?
我目前正在使用默认生成的 Cloudare Worker 对我的站点进行测试,我想知道我是否误解了某些东西,或者是否有一些机制在预览/发布站点 namespace 到 scipt 期间捆绑了一些机制。
如果有一些带有自动映射的随机机制,那么是否可以让每个开发人员都可以拥有自己的私有(private)预览 KV 命名空间?
type = "javascript"
name = "some-worker-dev-1234"
account_id = "<id>"
workers_dev = true

kv_namespaces = [
{ binding = "site_data", id = "<test-site-id>" }
]

[site]
# The location for the site.
bucket = "./dist"

# The entry directory for the package.json that contains
# main field for the file name of the compiled worker file in "main" field.
entry-point = ""

[env.production]
name = "some-worker-1234"
zone_id = "<zone-id>"
routes = [
"http://<site>/*",
"https://www.<site>/*"
]

# kv_namespaces = [
# { binding = "site_data", id = "<production-site-id>" }
# ]

import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'

/**
* The DEBUG flag will do two things that help during development:
* 1. we will skip caching on the edge, which makes it easier to
* debug.
* 2. we will return an error message on exception in your Response rather
* than the default 404.html page.
*/
const DEBUG = false

addEventListener('fetch', event => {
try {
event.respondWith(handleEvent(event))
} catch (e) {
if (DEBUG) {
return event.respondWith(
new Response(e.message || e.toString(), {
status: 500,
}),
)
}
event.respondWith(new Response('Internal Error', { status: 500 }))
}
})

async function handleEvent(event) {
const url = new URL(event.request.url)
let options = {}

/**
* You can add custom logic to how we fetch your assets
* by configuring the function `mapRequestToAsset`
*/
// options.mapRequestToAsset = handlePrefix(/^\/docs/)

try {
if (DEBUG) {
// customize caching
options.cacheControl = {
bypassCache: true,
}
}

const page = await getAssetFromKV(event, options)

// allow headers to be altered
const response = new Response(page.body, page)

response.headers.set('X-XSS-Protection', '1; mode=block')
response.headers.set('X-Content-Type-Options', 'nosniff')
response.headers.set('X-Frame-Options', 'DENY')
response.headers.set('Referrer-Policy', 'unsafe-url')
response.headers.set('Feature-Policy', 'none')

return response

} catch (e) {
// if an error is thrown try to serve the asset at 404.html
if (!DEBUG) {
try {
let notFoundResponse = await getAssetFromKV(event, {
mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req),
})

return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 })
} catch (e) {}
}

return new Response(e.message || e.toString(), { status: 500 })
}
}

/**
* Here's one example of how to modify a request to
* remove a specific prefix, in this case `/docs` from
* the url. This can be useful if you are deploying to a
* route on a zone, or if you only want your static content
* to exist at a specific path.
*/
function handlePrefix(prefix) {
return request => {
// compute the default (e.g. / -> index.html)
let defaultAssetKey = mapRequestToAsset(request)
let url = new URL(defaultAssetKey.url)

// strip the prefix from the path for lookup
url.pathname = url.pathname.replace(prefix, '/')

// inherit all other props from the default request
return new Request(url.toString(), defaultAssetKey)
}
}

最佳答案

如果格式不明显(对我来说不是),这里是 docs 中的示例配置 block 为几个 KV 命名空间指定了 preview_id:

kv_namespaces = [  
{ binding = "FOO", id = "0f2ac74b498b48028cb68387c421e279", preview_id = "6a1ddb03f3ec250963f0a1e46820076f" },
{ binding = "BAR", id = "068c101e168d03c65bddf4ba75150fb0", preview_id = "fb69528dbc7336525313f2e8c3b17db0" }
]
您可以在仪表板的 Workers KV 部分或使用 Wrangler CLI 生成新的命名空间 ID: wrangler kv:namespace create "SOME_NAMESPACE" --preview

关于cloudflare - 什么是 cloudflare KV preview_ids 以及如何获得?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63332306/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com