检查blob是否存在

Mar*_*a R 1 google-cloud-storage airflow google-cloud-composer

我正在尝试构建一个 DAG,它首先检查 Google Cloud Storage 中是否存在给定的路径/blob。blob 是包含一些 RAW 数据的那个,而不是安装在 Composer 工作器上的那个。

或者,一次性查看它是否存在并包含文件(列表> 1)会很方便,但存在已经是一件好事。

到目前为止,我尝试通过 bash 命令、google.cloud.storage 库和 gcs_hook 使用 gsutil stats 都无济于事。所有这些都为一个文件夹返回 False 我很确定存在

def check_folder(templates_dict,**kwargs):
    bucket = 'bucketname'
    blob_name = templates_dict['blob_name']
    # Blob name is something along the lines of '2019-04-10/11/' 
    gcs = GoogleCloudStorageHook()
    flag = gcs.exists(bucket,blob_name)
    if flag:
        print(flag)
        return('this_is_true')
    else:
        print(flag)
        return('this_is_not_true')
Run Code Online (Sandbox Code Playgroud)

对于给定的 blob_name,我很确定存在,我期待一个 true,但它总是返回 False。知道发生了什么吗?谢谢!

Tom*_*mme 5

exists函数GoogleCloudStorageHook检查对象,不支持使用前缀。如果要检查给定前缀中是否有任何数据,则需要使用该list函数。

例子:

if bool(hook.list(bucket, prefix=prefix)):
    print('some data is in my folder!')
Run Code Online (Sandbox Code Playgroud)

您可能还想查看 Sensor GoogleCloudStoragePrefixSensor的实现,因为它与您正在做的非常相似。