【问题标题】:Unable to open file in google-storage from google-colaboratory无法从 google-colaboratory 打开 google-storage 中的文件
【发布时间】:2021-04-30 16:08:25
【问题描述】:

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

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

我的问题很简单:我应该如何让 google-storage 中的存储桶可以从 google-colab 读取?我已经尝试了一切:

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

但是没有一个选项能正常工作。更让我困惑的是,将存储桶公开的时间有限。我也读过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'

【问题讨论】:

  • 如果您在存储桶中使对象公开可读,我看不到您实际下载文件的代码部分。使用任何模块作为请求或 urllib 来实际下载文件(例如检查这个post),只有在文件下载后你才能尝试打开它。此外,我强烈建议您从帖子中删除您的存储桶名称和任何其他 PII,因为这可能会导致隐私问题。

标签: python google-cloud-platform google-cloud-storage google-colaboratory bucket


【解决方案1】:

您无法通过简单地使用 os 包在 gcs 上打开文件。如果您将 gcs 存储桶安装在文件系统中,您将能够做到这一点,这样文件可能可以通过 FUSE 提供给操作系统。但为了简单起见,您应该导入 gcs 将云存储导入为 gcs 而不是使用 gcs_file = gcs.open(filename)

有关更多示例,请参阅 GCS 的 Google 文档https://cloud.google.com/storage/docs/downloading-objects#code-samples 或应用引擎的示例 https://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/read-write-to-cloud-storage

我希望这能解决你的问题。

【讨论】:

    【解决方案2】:

    发现这个article 使用库gcsfs 读取colab 中的云存储桶。我查了GCSFS,这个库处于测试阶段,不是谷歌官方库。

    GCFS 是 Google Cloud Storage 的 Python 式文件系统接口。这 软件为测试版,使用风险自负。

    请确保首先在 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)
    

    这是读取我的示例文件时输出的 sn-p:

    【讨论】:

    • 它似乎工作!我永远不会感谢你,我永远不会自己来这个图书馆。也许,如果我可能会问,您如何使用此库将数据保存到文件中?我也想到了这个问题,因为它是我代码的下一步。我知道我的问题超出了范围,如有必要,我会发布另一个问题。但是再次感谢您,这对我很有帮助。
    猜你喜欢
    • 2018-07-03
    • 2018-06-10
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2020-01-01
    • 2020-09-25
    • 1970-01-01
    相关资源
    最近更新 更多