我想在使用 dbutils.fs.put() 写入文件时编辑 api_key,有什么方法可以本地执行此操作吗?
另一个选项是我手动删除出现的 api 密钥,但我现在想避免这种情况。
使用
dbutils.fs.put()
写入文件时,没有本地方法可以编辑机密/密钥。不过,我已经尝试了一种您可以使用的解决方法。
是在将文件写入 DBFS 之前手动删除文件中出现的密钥。
我尝试过以下示例:
api_key = "k0u2bPJUpQ"
content = """
This is some sample content that contains the API key: {api_key}.
This is sensitive information that needs to be redacted.
""".format(api_key=api_key)
redacted_content = content.replace(api_key, "[REDACTED]")
dbutils.fs.put("/FileStore/tables/file.txt", redacted_content, overwrite=True)
结果:
+------------------------------------------------------------------+
|value |
+------------------------------------------------------------------+
| |
|This is some sample content that contains the API key: [REDACTED].|
|This is sensitive information that needs to be redacted. |
+------------------------------------------------------------------+
在上面的代码中,API 密钥存储在变量 api_key 中。 我已经定义了要写入文件的内容,其中包括 API 密钥。 我使用
replace()
方法将出现的 API 密钥替换为字符串“[REDACTED]”。
使用 dbutils.fs.put() 将编辑后的内容写入路径“/FileStore/tables/file.txt”指定的文件中。
Azure Databricks 提供了一种对使用
dbutils.secrets.get()
读取的秘密值进行编辑的本机方法
了解更多关于 get 命令(dbutils.secrets.get)