InfluxDB:由于我的令牌和os.getenv,无法与客户联系

发布于 2025-01-21 01:46:04 字数 1460 浏览 2 评论 0原文

我只是从infuxdb开始。 在UI上,Python有一个部分可以从数据库开始,其中一个示例:初始化客户端,写数据,提出请求并关闭客户端。为了验证您必须在UI上生成令牌的请求,该请求允许保护请求。但是,当我运行示例代码时,我会从我的代码的第12行中获得错误“只能连接str(不是“非型”)到str。

代码:

from datetime import datetime
import os

from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS

# You can generate an API token from the "API Tokens Tab" in the UI
token = os.getenv("INFLUX_TOKEN")
org = "XXXXXXX"
bucket = "XXXXXXX Bucket"

with InfluxDBClient(url="https://europe-west1-1.gcp.cloud2.influxdata.com", token=token, org=org) as client:

    write_api = client.write_api(write_options=SYNCHRONOUS)
    point = Point("mem") \
        .tag("host", "host1") \
        .field("used_percent", 23.43234543) \
        .time(datetime.utcnow(), WritePrecision.NS)

    write_api.write(bucket, org, point)
    
    query = """from(bucket: "XXXXXXX Bucket") |> range(start: -1h)"""
    tables = client.query_api().query(query, org=org)
    for table in tables:
        for record in table.records:
            print(record)
            
    client.close()

我知道问题来自OS.getEnv(“ influx_token”),因为它应该返回字符串,但实际上返回了非类型对象,但是我不知道为什么它不起作用。有了令牌,我得到了2件事:令牌的名称及其在创建时获得的代码。 I've tried :

  • os.getenv("the name of the token")
  • os.getenv("the code of the token")
  • same of 2 above but with ' instead of "

All the time the same error, so please if someone可以帮我!

I am just starting with InfluxDB.
On the UI there is a section for Python to start with the database with an example: initialize the client, write data, make a request and close the client. In order to authenticate the request you have to generate a token on the UI which allows to secure the request. But when I run the example code I get the error "can only concatenate str (not "NoneType") to str" from line 12 of my code.

The code :

from datetime import datetime
import os

from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS

# You can generate an API token from the "API Tokens Tab" in the UI
token = os.getenv("INFLUX_TOKEN")
org = "XXXXXXX"
bucket = "XXXXXXX Bucket"

with InfluxDBClient(url="https://europe-west1-1.gcp.cloud2.influxdata.com", token=token, org=org) as client:

    write_api = client.write_api(write_options=SYNCHRONOUS)
    point = Point("mem") \
        .tag("host", "host1") \
        .field("used_percent", 23.43234543) \
        .time(datetime.utcnow(), WritePrecision.NS)

    write_api.write(bucket, org, point)
    
    query = """from(bucket: "XXXXXXX Bucket") |> range(start: -1h)"""
    tables = client.query_api().query(query, org=org)
    for table in tables:
        for record in table.records:
            print(record)
            
    client.close()

I understand that the problem come from the os.getenv("INFLUX_TOKEN") because it's supposed to return a string but actually return NoneType object, but I dont know why it doesn't work. With the token I got 2 things : the name of the token and its code which I obtain when when it's created.
I've tried :

  • os.getenv("the name of the token")
  • os.getenv("the code of the token")
  • same of 2 above but with ' instead of "

All the time the same error, so please if someone can help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2025-01-28 01:46:04

如果您的环境变量实际上有问题
os.getEnv(“ influx_token”)返回这意味着influx_token未设置变量。

在生成令牌后手动将其设置在外壳中,

您可以通过输入SSH Shell Export Influx_token =“您的influx-token”

您可以找到有关环境变量的更多信息。 https://stackoverflow.com/questions/71855049/Storing-sensistive-information-intheformation-in-the-code/71855256#71855256"> here

You actually have a problem with your environment variable if
os.getenv("INFLUX_TOKEN") returns None it means that INFLUX_TOKEN variable is not set.

You can manually set it in your shell, after you generate the token by typing in your ssh shell

export INFLUX_TOKEN="your-influx-token"

You can find more about the environment variables here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文