在GCP虚拟机上使用python检索存储桶

时间:2019-09-14 14:04:12

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

我正在尝试在GCP虚拟机(而不是本地计算机)上使用Python3。我需要从某个存储桶中提取数据。这是我正在使用的代码:

public static class TextureContent
{
    public static List<Texture2D> TexturesFromFolder(this ContentManager contentManager, GraphicsDevice graphics, string contentFolder)
    {
        //System.Console.WriteLine("./Content/"+contentFolder);
        DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory+"/"+contentFolder);
        if(!dir.Exists)
            throw new DirectoryNotFoundException();
        List<Texture2D> result = new List<Texture2D>();

        FileInfo[] files = dir.GetFiles("*.*");
        foreach(FileInfo file in files)
        {
            string key = Path.GetFileName(file.Name);
            System.Console.WriteLine(key);
            //result.Add(contentManager.Load<Texture2D>(contentFolder + "/" + key));
            FileStream fileStream = new FileStream(contentManager.RootDirectory+"/"+contentFolder+"/"+key, FileMode.Open);
            Texture2D spr = Texture2D.FromStream(graphics, fileStream);
        }
        return result;
    }
}

它返回以下错误:

from google.cloud import storage
client = storage.Client()
bucket_name = timetables-new/23/00_H0.json
bucket = client.get_bucket("bucket_name")

如何检索文件,以便可以在python脚本中使用它?

编辑: 由于我使用对象名称作为存储桶名称(/不允许),所以我也尝试过:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/username/.local/lib/python3.6/site-packages/google/cloud/storage/client
.py", line 301, in get_bucket
    bucket.reload(client=self)
  File "/home/username/.local/lib/python3.6/site-packages/google/cloud/storage/_helpe
rs.py", line 130, in reload
    _target_object=self,
  File "/home/username/.local/lib/python3.6/site-packages/google/cloud/_http.py", lin
e 393, in api_request
    raise exceptions.from_http_response(response)
google.api_core.exceptions.NotFound: 404 GET https://www.googleapis.com/storage/v1/b/timetables-new/23/00_H0.json?projection=noAcl: Not Found

哪个是整个存储桶的名称,但是我有相同的错误。

2 个答案:

答案 0 :(得分:1)

您的帖子中缺少的一件事是您实际上是如何进行身份验证的。您可能需要如下设置服务帐户的json密钥:

GOOGLE_APPLICATION_CREDENTIALS=key.json

答案 1 :(得分:0)

尝试使用Gsutil的Gcsfuse,结果如下:

  • gsutil允许您将文件从存储桶复制到虚拟机:
gsutil -m cp gs://name_of_bucket/.../file destination/path/on/your/VM 
  • gcsfuse创建一个符号链接以允许访问存储桶中的文件:
mkdir /path/to/mount
gcsfuse bucket_name /path/to/mount