无法从 google-colaboratory 打开 google- 存储中的文件
Unable to open file in google-storage from google-colaboratory
我正在尝试使用 TPU 引擎打开存储在 google-colab 工作簿中的 google-存储桶中的文件。然而,我总是面临错误:
FileNotFoundError: [Errno 2] No such file or directory: 'gs://vocab_jb/merges.txt'
我的问题很简单:我应该如何使 google-storage 中的存储桶可从 google-colab 读取?我什么都试过了:
- 使用 IAM
创建存储桶 public
- 为所有者分配一个特殊的电子邮件地址
- 通过 LCA 选项制作文件 public
- 已关注 x 个不同 tutorials
- 我每次都尝试通过“gs://bucket”或“https://...”调用存储桶
但是 none 的选项工作正常。更让我困惑的是,让桶 public 工作的时间有限。我也读过 this post 但答案没有帮助。另外,我真的不关心读或写的权利。
我正在按以下方式初始化我的 TPU:
import os
use_tpu = True #@param {type:"boolean"}
bucket = 'vocab_jb'
if use_tpu:
assert 'COLAB_TPU_ADDR' in os.environ, 'Missing TPU; did you request a TPU in Notebook Settings?'
from google.colab import auth
auth.authenticate_user()
%tensorflow_version 2.x
import tensorflow as tf
print("Tensorflow version " + tf.__version__)
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR']) # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!')
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
with open("gs://vocab_jb/merges.txt", 'rb') as f:
a = f.read()
FileNotFoundError: [Errno 2] No such file or directory: 'gs://vocab_jb/merges.txt'
您无法通过简单地使用 os 包在 gcs 上打开文件。如果您将 gcs 存储桶挂载到您的文件系统中,那么文件可能可以通过 FUSE 提供给 os,您就可以做到这一点。但是为了简单起见,您应该导入 gcs
将云存储导入为 gcs
而不是使用 gcs_file = gcs.open(filename)
有关更多示例,请参阅 Google GCS 文档 https://cloud.google.com/storage/docs/downloading-objects#code-samples
或应用引擎的例子
https://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/read-write-to-cloud-storage
希望这能解决您的问题。
找到这个 article that uses the library gcsfs
that reads through a cloud storage bucket in colab. I looked up GCSFS 并且这个库处于测试阶段,不是官方 Google 库。
GCSFS is a pythonic file-system interface to Google Cloud Storage. This
software is beta, use at your own risk.
只需确保首先在 collab 中安装库。
pip install gcsfs
以下是您代码中的实现:
import os
import gcsfs
import google.auth
from google.colab import auth
auth.authenticate_user()
credentials, project_id = google.auth.default()
fs = gcsfs.GCSFileSystem(project=project_id, token=credentials)
use_tpu = True #@param {type:"boolean"}
bucket = 'vocab_jb'
if use_tpu:
assert 'COLAB_TPU_ADDR' in os.environ, 'Missing TPU; did you request a TPU in Notebook Settings?'
%tensorflow_version 2.x
import tensorflow as tf
print("Tensorflow version " + tf.__version__)
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR']) # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!')
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
reader = fs.open("gs://your-bucket-here/kinglear_on_roids.txt")
for text in reader:
print(text)
这是读取示例文件时的输出片段:
我正在尝试使用 TPU 引擎打开存储在 google-colab 工作簿中的 google-存储桶中的文件。然而,我总是面临错误:
FileNotFoundError: [Errno 2] No such file or directory: 'gs://vocab_jb/merges.txt'
我的问题很简单:我应该如何使 google-storage 中的存储桶可从 google-colab 读取?我什么都试过了:
- 使用 IAM 创建存储桶 public
- 为所有者分配一个特殊的电子邮件地址
- 通过 LCA 选项制作文件 public
- 已关注 x 个不同 tutorials
- 我每次都尝试通过“gs://bucket”或“https://...”调用存储桶
但是 none 的选项工作正常。更让我困惑的是,让桶 public 工作的时间有限。我也读过 this post 但答案没有帮助。另外,我真的不关心读或写的权利。
我正在按以下方式初始化我的 TPU:
import os
use_tpu = True #@param {type:"boolean"}
bucket = 'vocab_jb'
if use_tpu:
assert 'COLAB_TPU_ADDR' in os.environ, 'Missing TPU; did you request a TPU in Notebook Settings?'
from google.colab import auth
auth.authenticate_user()
%tensorflow_version 2.x
import tensorflow as tf
print("Tensorflow version " + tf.__version__)
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR']) # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!')
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
with open("gs://vocab_jb/merges.txt", 'rb') as f:
a = f.read()
FileNotFoundError: [Errno 2] No such file or directory: 'gs://vocab_jb/merges.txt'
您无法通过简单地使用 os 包在 gcs 上打开文件。如果您将 gcs 存储桶挂载到您的文件系统中,那么文件可能可以通过 FUSE 提供给 os,您就可以做到这一点。但是为了简单起见,您应该导入 gcs 将云存储导入为 gcs 而不是使用 gcs_file = gcs.open(filename)
有关更多示例,请参阅 Google GCS 文档 https://cloud.google.com/storage/docs/downloading-objects#code-samples 或应用引擎的例子 https://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/read-write-to-cloud-storage
希望这能解决您的问题。
找到这个 article that uses the library gcsfs
that reads through a cloud storage bucket in colab. I looked up GCSFS 并且这个库处于测试阶段,不是官方 Google 库。
GCSFS is a pythonic file-system interface to Google Cloud Storage. This software is beta, use at your own risk.
只需确保首先在 collab 中安装库。
pip install gcsfs
以下是您代码中的实现:
import os
import gcsfs
import google.auth
from google.colab import auth
auth.authenticate_user()
credentials, project_id = google.auth.default()
fs = gcsfs.GCSFileSystem(project=project_id, token=credentials)
use_tpu = True #@param {type:"boolean"}
bucket = 'vocab_jb'
if use_tpu:
assert 'COLAB_TPU_ADDR' in os.environ, 'Missing TPU; did you request a TPU in Notebook Settings?'
%tensorflow_version 2.x
import tensorflow as tf
print("Tensorflow version " + tf.__version__)
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR']) # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
except ValueError:
raise BaseException('ERROR: Not connected to a TPU runtime; please see the previous cell in this notebook for instructions!')
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
reader = fs.open("gs://your-bucket-here/kinglear_on_roids.txt")
for text in reader:
print(text)
这是读取示例文件时的输出片段: