我无法使用 Cloud Shell 从 python 访问存储桶中的文件。我创建了这个简单的例子:
open('gs://iwc/panda.jpg')
结果是:
jklieb@cloudshell:~$ python demo.py
Traceback (most recent call last):
File "demo.py", line 1, in <module>
open('gs://iwc/panda.jpg')
IOError: [Errno 2] No such file or directory: 'gs://iwc/panda.jpg'
但是使用gsutil时文件是可见的:
jklieb@cloudshell:~$ gsutil ls gs://iwc
gs://iwc/panda.jpg
我创建了一个单独的虚拟机实例(不是与 Cloud Shell 关联的临时实例)并得到了类似的结果。
我还使用 ctpu 配置了一个 TPU,但同样无法访问另一个存储桶中的文件。 ctpu 上的文档说应该正确设置权限以允许访问存储桶。
我是不是犯了一些简单到我看不出来的错误?
请您参考如下方法:
解决方案是使用 tensorflow.gfile.Open() 访问文件:
jklieb@iwildcam2018:~$ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow.gfile as gf
>>> gf.Open('gs://iwc/panda.jpg')
<tensorflow.python.platform.gfile.GFile object at 0x7f03e8b15f28>
这适用于 Cloud Shell 和我创建的其他虚拟机。 tensorflow.gfile.Glob() 可用于获取与模式匹配的文件列表。
