在Cloudflare Pages KV上,如何为KV键生成初始值?

huangapple go评论74阅读模式
英文:

On Cloudflare Pages KV, how do you seed values for KV keys

问题

我有一个使用Cloudflare Pages的项目,其中包含使用KV的函数。我希望在调用函数之前使用预先提供的KV值来测试我的函数。我应该如何做到这一点?

英文:

I have a Cloudflare Pages project with functions which used KV. I wish to test my functions with KV values that are seeded before the function is called. How do I do this?

答案1

得分: 0

解决方案:
在您的项目的根目录,即函数目录所在的位置,创建一个文件夹:

.wrangler/state/kv/<bind-name>/<key>

是您的 KV 命名空间。
是一个以您的密钥名称命名的文件。

对我来说,bind-name 是 TEST,key 是 BUG_TEST。所以我的文件是:

.wrangler/state/kv/TEST/BUG_TEST

在文件中放入密钥的值。

然后,当您启动 wrangler 时使用 --persist。对我来说,使用 pages,命令是:

npx wrangler pages dev --kv=TEST --persist -- npm start

另外,如果您正在使用 git,请不要忘记将 .wrangler 添加到 .gitignore 中。

英文:

The solution:
On the root directory of your project … i.e. where the functions directory is, create a file in a directory:

.wrangler/state/kv/&lt;bind-name&gt;/&lt;key&gt;

&lt;bind-name&gt; is your KV namespace.
&lt;key&gt; is a file with the name of your key.

for me bind-name is TEST, key is BUG_TEST. So my file is:

.wrangler/state/kv/TEST/BUG_TEST

Inside the file place the value for the key.

Then when you start your wrangler use --persist. For me, using pages, the command is:

npx wrangler pages dev --kv=TEST --persist -- npm start

Also, if you are using git don’t forget to add .wrangler to .gitignore

huangapple
  • 本文由 发表于 2023年2月24日 09:45:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定