通过 python (Flask) 从 GCS 读取和写入图像

Read and write images from GCS via python (Flask)

在本地机器上,我能够提取图像的特征并将相应的 .npy 文件存储在不同的文件夹中。

from PIL import Image
from feature_extractor import FeatureExtractor
from pathlib import Path
import numpy as np

if __name__ == '__main__':
    fe = FeatureExtractor()

    for img_path in sorted(Path("./static/img").glob("*.jpg")):
        print(img_path)  # e.g., ./static/img/xxx.jpg
        feature = fe.extract(img=Image.open(img_path))
        feature_path = Path("./static/feature") / (img_path.stem + ".npy")  # e.g., ./static/feature/xyz.npy
        np.save(feature_path, feature)

现在我想在 google 云中托管它。但是为此,我需要从 Google Cloud Storage 读取和写入图像。有没有类似 Path for GCS 的库?

这个有官方客户端库 pip install --upgrade google-cloud-storage

参见:https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-python


编辑: 来源:

如果您想列出 GCS 存储桶中的苍蝇:

from google.cloud import storage

client = storage.Client()
for blob in client.list_blobs('bucketname', prefix='abc/myfolder'):
  print(str(blob))