python - 我们如何从其他存储桶(而不是作曲家存储桶)访问任何配置文件
问题描述
我是 python 和谷歌作曲家的新手。我正在尝试从我的 python 脚本中从谷歌云存储中读取配置(.properties)文件。配置文件包含键值对。我尝试使用 configparser 来读取配置文件并使用操作员进行正常操作如果我们在相同的作曲家环境中拥有相同的文件,那么我们可以提供类似“/home/airflow/gcs/dags/config.properties”的路径
但是对于其他桶,我可以给出什么路径?
我正在尝试使用以下代码访问路径 storage_client
separator = "="
keys = {}
def iterate_bucket():
bucket_name = 'other-bucket'
storage_client = storage.Client.from_service_account_json(
'/home/airflow/gcs/data/*************.json')
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs()
return blobs
def read_Prop():
blobs = iterate_bucket()
for blob in blobs:
if "config.properties" in blob.name:
print("hello : ", blob.name)
#file_name = "/home/airflow/gcs/" + blob.name
file_name = blob.name
with open(file_name, 'r') as f:
for line in f:
if separator in line:
name, value = line.split(separator, 1)
keys[name.strip()] = value.strip()
print(keys.get('any_key'))
我也使用了 configparser
config = configparser.ConfigParser()
config.read(blob.name)
在这两种情况下,我的 python 脚本都无法访问其他谷歌云存储(桶)。我没有收到此类文件或目录错误
我们可以提供什么路径或任何其他访问方式?
示例 2 -
def readYML():
bucket_name = 'external-bucket'
storage_client = storage.Client.from_service_account_json(
'/home/airflow/gcs/data/private-key.json')
bucket = storage_client.get_bucket(bucket_name)
file_name = ""
blobs = bucket.list_blobs()
print("blobs : ",blobs)
for blob in blobs:
if "sample_config.yml" in blob.name:
print("hello : ", blob.name)
file_name = blob.name
print("file_name : ",file_name)
with open(file_name, 'r') as ymlfile:
CFG = yaml.load(ymlfile)
print("inside readYML method : ", CFG['configs']['project_id'])
return CFG
查看日志 - 在打印语句中,文件名即将到来,但是当我们读取没有此类文件或目录错误的错误时。
[2020-01-02 12:39:12,853] {base_task_runner.py:101} INFO - Job 11782:
Subtask Raw1 [2020-01-02 12:39:12,852] {logging_mixin.py:95} INFO - hello :
sample_config.yml
[2020-01-02 12:39:12,853] {logging_mixin.py:95} INFO - file_name :
sample_config.yml
[2020-01-02 12:39:12,853] {base_task_runner.py:101} INFO - Job 11782:
Subtask Raw1 [2020-01-02 12:39:12,853] {logging_mixin.py:95} INFO -
file_name : sample_config.yml
[2020-01-02 12:39:12,855] {models.py:1846} ERROR - [Errno 2] No such file or
directory: 'sample_config.yml'
提前致谢
解决方案
我们无法从作曲家存储桶访问任何配置文件。我们只能使用存储访问 API 访问文件,但不能直接从任何其他存储桶中读取任何属性文件。
推荐阅读
- oracle-apex-5.1 - Oracle APEX - 如何根据另一个项目的值在页面加载时显示一个项目
- jdbc - 将 JDBC 和 Saxon DateTime 数据作为 ZonedDateTime 对象获取
- php - Drupal 8:尝试读取上传的文件内容
- javascript - 如何将带有\的字符串路径转换为\\?
- git - Git commit, how to break one commit into two
- html - 使用来自 API 调用的数据在子组件中设置 HTML 选择的默认值
- bash - 转义 jq 查询并将输出保存在 bash 变量中
- unit-testing - 模拟 Httpservletrequest 和 requestcontext
- r - 绘制拟合的分段线性模型显示的断点比估计的要多
- android - Android:AWS Cognito 与 API Gateway 结合使用错误的凭证