[Python] How to Write Key-Value Pairs to a Cloudflare KV Database?

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use the Cloudflare Python SDK to bulk update key-value pairs in a KV database, but I'm encountering an error. Here's the documentation for the bulk_update API I followed:

    bulk_update api doc

    I refer doc sample code to write the following code.

    import os
    import json
    from cloudflare import Cloudflare

    # read json file as str
    with open('config.json', 'r', encoding='utf-8') as file:
    data = json.load(file)
    json_str = json.dumps(data, ensure_ascii=False)

    client = Cloudflare(
    api_email=os.environ.get("CLOUDFLARE_EMAIL"),
    api_key=os.environ.get("CLOUDFLARE_API_KEY"),
    )

    response = client.kv.namespaces.keys.bulk_update(
    namespace_id=os.environ.get("KV_NAMESPACE_ID"),
    account_id=os.environ.get("CF_ACCOUNT_ID"),
    body=[{
    "key" : "sb",
    "values": json_str
    }],
    )
    print(response.successful_key_count)


    I get the following error:

    $python3 upload.py

    Traceback (most recent call last):
    File "/home/hall/sb-converter/upload.py", line 35, in <module>
    response = client.kv.namespaces.keys.bulk_update(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: 'KeysResource' object has no attribute 'bulk_update'


    Python SDK Version:

    import cloudflare
    print(cloudflare.__version__)


    4.1.0


    Questions:

    • Am I using the wrong version of the SDK?
    • Is there a different way to update key-value pairs in this version of the SDK?
    • How can I fix this error?

    Continue reading...

Compartilhe esta Página