首页 > 解决方案 > 我们如何从其他存储桶(而不是作曲家存储桶)访问任何配置文件

问题描述

我是 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'

提前致谢

标签: pythonpython-3.xgoogle-cloud-platformgoogle-cloud-storage

解决方案


我们无法从作曲家存储桶访问任何配置文件。我们只能使用存储访问 API 访问文件,但不能直接从任何其他存储桶中读取任何属性文件。


推荐阅读