无法从googlecolaboratory打开googlestorage中的文件

2024-04-18 17:59:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用TPU引擎打开google colab工作簿中google存储桶中存储的文件。然而,我总是面临错误:

FileNotFoundError: [Errno 2] No such file or directory: 'gs://vocab_jb/merges.txt'

我的问题很简单:我应该如何使谷歌存储中的一个桶从谷歌colab中可读?我什么都试过了:

  1. 使用IAM公开存储桶
  2. 将特殊电子邮件地址分配给所有者
  3. 通过LCA选项公开文件
  4. 跟随x个不同的tutorials
  5. 每次我都尝试通过“gs://bucket”或“bucket”调用buckethttps://..."

但没有一种选择是正确的。更让我困惑的是,公开水桶的作用是有限的。我也读过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'

Tags: inimporttxtgsbucketosversiontf
2条回答

找到了使用库gcsfsarticle,该库在colab中读取云存储桶。我查了一下GCSFS,这个库是beta版的,不是官方的谷歌库

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)

以下是读取示例文件时的输出片段: enter image description here

您不能简单地使用os软件包在gcs上打开文件。如果您将gcs存储桶装载到文件系统中,以便操作系统可以通过FUSE访问文件,那么您将能够做到这一点。但为了使事情变得简单,你应该导入gcs 将cloudstorage导入为gcs 然后使用gcs_file=gcs.open(文件名)

有关更多示例,请参阅谷歌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

我希望这能解决你的问题

相关问题 更多 >