简体   繁体   English

从Cloud Storage读取具有固定前缀但后缀为随机的文件

[英]Read files from Cloud Storage having definite prefix but random postfix

I am using the following code to read the contents of a file in Google Cloud Storage from Cloud Functions. 我正在使用以下代码从Cloud Functions读取Google Cloud Storage中文件的内容。 Here the name of file (filename) is defined. 此处定义了文件名(文件名)。 I now have files that will have a definite prefix but the postfix can be anything. 我现在有一些文件,这些文件将有一个明确的前缀,但后缀可以是任何东西。 Example - ABC-khasvbdjfy7i76.csv 示例-ABC-khasvbdjfy7i76.csv

How to read the contents of such files? 如何读取此类文件的内容?

I know there will be "ABC" as a prefix. 我知道会有“ ABC”作为前缀。 But the postfix can be anything random. 但是后缀可以是随机的。

storage_client = storage.Client()
bucket = storage_client.get_bucket('test-bucket')
blob = bucket.blob(filename)
contents = blob.download_as_string()
print("Contents : ")
print(contents)

You can use prefix parameter of list_blobs method to filter objects beginning with your prefix, and iterate on the objects : 您可以使用list_blobs方法的prefix参数来过滤以您的前缀开头的对象,并在这些对象上进行迭代:

from google.cloud import storage

storage_client = storage.Client()
bucket = storage_client.get_bucket('test-bucket')

blobs = bucket.list_blobs(prefix="ABC")

for blob in blobs:
    contents = blob.download_as_string()
    print("Contents of %s:" % blob.name)
    print(contents)

You need to know the entire path of a file to be able to read it. 您需要知道文件的完整路径才能读取它。 And since the client can't guess the random suffix, you will first have to list all the files with the non-random prefix. 而且由于客户端无法猜测随机后缀,因此您首先必须列出所有带有非随机前缀的文件。

There is a list operation, to which you can pass a prefix, as shown here: Google Cloud Storage + Python : Any way to list obj in certain folder in GCS? 有一个list操作,您可以向其传递一个前缀,如下所示: Google Cloud Storage + Python:是否可以在GCS的某些文件夹中列出obj?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 努力从 Google Cloud Storage 存储桶中读取 csv 文件 - Struggling to read csv files from Google Cloud Storage bucket 如何从Google Cloud Storage Python远程读取公共文件? - How can I read public files from google cloud storage python remotely? Google Datalab从云存储中读取 - Google Datalab read from cloud storage 如何使用AST python模块从中缀转换为后缀/前缀? - How to convert from infix to postfix/prefix using AST python module? 一次将多个文件从谷歌驱动器上传到谷歌云存储 - upload multiple files at once from google drive to google cloud storage 如何在特定文件集从谷歌云到达云存储时启动云数据流管道 function - how to launch a cloud dataflow pipeline when particular set of files reaches Cloud storage from a google cloud function 使用 Python 脚本中的 Google Cloud Functions 从 Google Cloud Storage 读取 CSV - Read a CSV from Google Cloud Storage using Google Cloud Functions in Python script Google Cloud Storage 存储桶 list_blobs 前缀太宽泛 - Google Cloud Storage bucket list_blobs prefix is too broad 使用 Cloud Functions 将文件从 Google Cloud Storage 传输到 Windows VM 实例 - Transfer files from Google Cloud Storage to Windows VM instance using Cloud Functions Python:来自locals()的具有定义前缀的随机函数 - Python: random function from locals() with defined prefix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM