【问题标题】:How we can access any configuration file from other bucket (instead of composer bucket)我们如何从其他存储桶(而不是作曲家存储桶)访问任何配置文件
【发布时间】:2019-08-21 07:16:40
【问题描述】:

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

提前致谢

【问题讨论】:

  • 使用 Google Cloud Console 并查找存储桶中每个对象的路径。您将看到它们是如何命名的。但是,您不能使用标准文件 I/O 库来读取对象。您需要使用与您使用相同的 SDK 来获取名称以读取对象(除非对象是公共的,那么您将使用 HTTP 请求库)。
  • 注意您的代码有问题。例如,在 for 循环之后引用 blob.name(或者您的代码显示中的缩进是否错误?)。
  • 没有缩进错误,代码显示错误,反正我改了
  • 在您的安全点上,我建议您不要存储和使用 JSON 密钥文件。最好使用 Composer 的身份并在您的存储桶上授权此服务帐户(仅作为 Object Reader,如果不需要其他操作)。
  • 嗨@BhageshArora,如果您仍然遇到此问题,您可以在帖子中添加完整的错误跟踪,以进一步调查您的问题吗?但是,如果问题现在已经解决,您能否发布答案并接受它,以便更好地了解解决方案?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 2016-09-26
    相关资源
    最近更新 更多