[英]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.