pythoninfluxdbinfluxdb-python

how create a organization and bucket in influxdb with python


how create a organization and bucket in influxdb with python? i have a python script for creating a org and bucket in influx db but it wont work and return unauthorized response

can any body help me in this issue with influxdb apis?

HTTP response body: 
{   "code": "unauthorized",     "message": "write:orgs is unauthorized" }

Python Code:

from influxdb_client import InfluxDBClient, BucketRetentionRules
from influxdb_client.rest import ApiException

def create_org_and_bucket(url, token, org_name, bucket_name, bucket_retention_days):
    # Convert retention days to seconds
    if bucket_retention_days == -1:
        bucket_retention_seconds = None  # No expiration
    else:
        bucket_retention_seconds = bucket_retention_days * 86400  # Convert days to seconds

    # Connect to InfluxDB
    client = InfluxDBClient(url=url, token=token)
    orgs_api = client.organizations_api()
    buckets_api = client.buckets_api()

    try:
        # Create organization
        org = orgs_api.create_organization(name=org_name)
        print(f"Organization '{org_name}' created successfully.")

        try:
            # Create bucket
            if bucket_retention_seconds is None:
                retention_rules = []
            else:
                retention_rules = [BucketRetentionRules(type="expire", every_seconds=bucket_retention_seconds)]

            bucket = buckets_api.create_bucket(name=bucket_name, org_id=org.id, retention_rules=retention_rules)
            print(f"Bucket '{bucket_name}' in organization '{org_name}' created successfully.")
            return org, bucket

        except ApiException as e:
            # If bucket creation fails, delete the created organization
            print(f"Error creating bucket: {e}")
            orgs_api.delete_organizations_id(org.id)
            print(f"Rolled back organization '{org_name}'.")
            return False

    except ApiException as e:
        print(f"Error creating organization: {e}")
        return False

    finally:
        # Close the connection to InfluxDB
        client.close()

url = "http://localhost:8086/"
token = ""
org_name = input("org name: ")
bucket_name = input("bucket name: ")
bucket_retention_days = -1

result = create_org_and_bucket(url, token, org_name, bucket_name, bucket_retention_days)
if result:
    org, bucket = result
    print(f"Successfully created organization '{org.name}' and bucket '{bucket.name}'.")
else:
    print("Failed to create organization and bucket.")

i try this and also ask gpt and gemini but it wont work and returns same error


Solution

  • I had same problem, In Influxdb there are 3 types of tokens.

    1. All Access Token which has the access to all orgs, buckets
    2. Write/Read which you can specify the access of token base on read, write operations.
    3. Operator Token which will created throw Influxdb setup process.

    For what you need to do (create org, create bucket) you will need Operator Token

    Read More: https://docs.influxdata.com/influxdb/v2/admin/tokens/#operator-token