我们如何从其他存储桶(而不是Composer存储桶)访问任何配置文件

时间:2019-08-21 07:16:40

标签: python python-3.x google-cloud-platform google-cloud-storage

我是python和google composer的新手。 我正在尝试从python脚本中从Google云存储中读取配置(.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脚本都无法访问其他Google云存储(存储桶)。 我没有收到这样的文件或目录错误

我们可以提供什么路径或任何其他访问方式? 预先感谢

1 个答案:

答案 0 :(得分:1)

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