1"use strict";(self.webpackChunkfiber_docs=self.webpackChunkfiber_docs||[]).push([["11502"],{253302(e,n,t){t.r(n),t.d(n,{metadata:()=>l,default:()=>u,frontMatter:()=>i,contentTitle:()=>a,toc:()=>c,assets:()=>s});var l=JSON.parse('{"id":"cloudflarekv/cloudflarekv","title":"Cloudflare KV","description":"Release","source":"@site/storage_versioned_docs/version-mockstorage_v1.x.x/cloudflarekv/README.md","sourceDirName":"cloudflarekv","slug":"/cloudflarekv/","permalink":"/storage/mockstorage_v1.x.x/cloudflarekv/","draft":false,"unlisted":false,"editUrl":"https://github.com/gofiber/storage/edit/main/cloudflarekv/README.md","tags":[],"version":"mockstorage_v1.x.x","lastUpdatedAt":1791052031000,"frontMatter":{"id":"cloudflarekv","title":"Cloudflare KV"},"sidebar":"left_sidebar","previous":{"title":"Clickhouse","permalink":"/storage/mockstorage_v1.x.x/clickhouse/"},"next":{"title":"Coherence","permalink":"/storage/mockstorage_v1.x.x/coherence/"}}'),o=t(474848),r=t(28453);let i={id:"cloudflarekv",title:"Cloudflare KV"},a,s={},c=[{value:"Table of Contents",id:"table-of-contents",level:3},{value:"Signatures",id:"signatures",level:3},{value:"Installation",id:"installation",level:3},{value:"Examples",id:"examples",level:3},{value:"Config",id:"config",level:3},{value:"Default Config",id:"default-config",level:3},{value:"Using an Existing Cloudflare API Client",id:"using-an-existing-cloudflare-api-client",level:3}];function d(e){let n={a:"a",code:"code",h3:"h3",img:"img",li:"li",p:"p",pre:"pre",ul:"ul",...(0,r.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(n.p,{children:[(0,o.jsx)(n.img,{src:"https://img.shields.io/github/v/tag/gofiber/storage?filter=cloudflarekv*",alt:"Release"}),"\n",(0,o.jsx)(n.a,{href:"https://gofiber.io/discord",children:(0,o.jsx)(n.img,{src:"https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7",alt:"Discord"})}),"\n",(0,o.jsx)(n.img,{src:"https://img.shields.io/github/actions/workflow/status/gofiber/storage/test-cloudflarekv.yml?label=Tests",alt:"Test"})]}),"\n",(0,o.jsxs)(n.p,{children:["A Cloudflare KV storage driver using ",(0,o.jsx)(n.a,{href:"https://github.com/cloudflare/cloudflare-go",children:"cloudflare/cloudflare-go"}),"."]}),"\n",(0,o.jsx)(n.h3,{id:"table-of-contents",children:"Table of Contents"}),"\n",(0,o.jsxs)(n.ul,{children:["\n",(0,o.jsx)(n.li,{children:(0,o.jsx)(n.a,{href:"#signatures",children:"Signatures"})}),"\n",(0,o.jsx)(n.li,{children:(0,o.jsx)(n.a,{href:"#installation",children:"Installation"})}),"\n",(0,o.jsx)(n.li,{children:(0,o.jsx)(n.a,{href:"#examples",children:"Examples"})}),"\n",(0,o.jsx)(n.li,{children:(0,o.jsx)(n.a,{href:"#config",children:"Config"})}),"\n",(0,o.jsx)(n.li,{children:(0,o.jsx)(n.a,{href:"#default-config",children:"Default Config"})}),"\n"]}),"\n",(0,o.jsx)(n.h3,{id:"signatures",children:"Signatures"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:"func New(config ...Config) Storage\nfunc NewFromConnection(api APIInterface, config ...Config) *Storage\nfunc (s *Storage) Get(key string) ([]byte, error)\nfunc (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error)\nfunc (s *Storage) Set(key string, val []byte, exp time.Duration) error\nfunc (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error\nfunc (s *Storage) Delete(key string) error\nfunc (s *Storage) DeleteWithContext(ctx context.Context, key string) error\nfunc (s *Storage) Reset() error\nfunc (s *Storage) ResetWithContext(ctx context.Context) error\nfunc (s *Storage) Close() error\nfunc (s *Storage) Conn() *cloudflare.API\n"})}),"\n",(0,o.jsx)(n.h3,{id:"installation",children:"Installation"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-bash",children:"go mod init github.com/<user>/<repo>\n"})}),"\n",(0,o.jsx)(n.p,{children:"And then install the Cloudflare KV implementation:"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-bash",children:"go get github.com/gofiber/storage/cloudflarekv\n"})}),"\n",(0,o.jsx)(n.h3,{id:"examples",children:"Examples"}),"\n",(0,o.jsx)(n.p,{children:"Import the storage package."}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:'import "github.com/gofiber/storage/cloudflarekv"\n'})}),"\n",(0,o.jsxs)(n.p,{children:["You can use the following methods to create storage. The Key must be an API Token generated
1with at least ",(0,o.jsx)(n.code,{children:"Account.Workers KV Storage"})," permission. Check the ",(0,o.jsx)(n.a,{href:"https://developers.cloudflare.com/fundamentals/api/get-started/create-token/",children:"Create API Token"})," documentation to generate one."]}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:'// Initialize default config\nstore := cloudflarekv.New()\n\nstore := cloudflarekv.New(cloudflarekv.Config{\n Key: "",\n Email: "",\n AccountID: "fiber",\n NamespaceID: "fiber",\n Reset: false,\n})\n\n'})}),"\n",(0,o.jsx)(n.h3,{id:"config",children:"Config"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:'type Config struct {\n\n // Cloudflare Auth Token\n //\n // Optional. Default is ""\n Key string\n\n // Cloudflare Email\n //\n // Optional. Default is ""\n Email string\n\n // Account id\n //\n // Optional. Default is "fiber"\n AccountID string\n\n // Namespace id\n //\n // Optional. Default is "fiber"\n NamespaceID string\n\n // Reset clears any existing keys in existing Table\n //\n // Optional. Default is false\n Reset bool\n}\n'})}),"\n",(0,o.jsx)(n.h3,{id:"default-config",children:"Default Config"}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:'var ConfigDefault = Config{\n Key: "",\n Email: "",\n AccountID: "fiber",\n NamespaceID: "fiber",\n Reset: false,\n}\n'})}),"\n",(0,o.jsx)(n.h3,{id:"using-an-existing-cloudflare-api-client",children:"Using an Existing Cloudflare API Client"}),"\n",(0,o.jsxs)(n.p,{children:["If your application already holds a Cloudflare API client, you can build the storage on it instead of creating a second one. Only the ",(0,o.jsx)(n.code,{children:"Email"}),", ",(0,o.jsx)(n.code,{children:"AccountID"}),", ",(0,o.jsx)(n.code,{children:"NamespaceID"})," and ",(0,o.jsx)(n.code,{children:"Reset"})," options are read; the credentials come from the client."]}),"\n",(0,o.jsxs)(n.p,{children:["The client stays yours to manage: ",(0,o.jsx)(n.code,{children:"Close"})," on a storage built this way is a no-op, so the rest of your application keeps working."]}),"\n",(0,o.jsx)(n.pre,{children:(0,o.jsx)(n.code,{className:"language-go",children:'import (\n "github.com/cloudflare/cloudflare-go"\n "github.com/gofiber/storage/cloudflarekv"\n)\n\nfunc main() {\n api, err := cloudflare.NewWithAPIToken("token")\n if err != nil {\n panic(err)\n }\n\n store := cloudflarekv.NewFromConnection(api, cloudflarekv.Config{\n Email: "[email protected]",\n AccountID: "account-id",\n NamespaceID: "namespace-id",\n })\n defer store.Close()\n}\n'})})]})}function u(e={}){let{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,o.jsx)(n,{...e,children:(0,o.jsx)(d,{...e})}):d(e)}},28453(e,n,t){t.d(n,{R:()=>i,x:()=>a});var l=t(296540);let o={},r=l.createContext(o);function i(e){let n=l.useContext(r);return l.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:i(e.components),l.createElement(r.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.