微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法从 google-colaboratory 打开 google-storage 中的文件

如何解决无法从 google-colaboratory 打开 google-storage 中的文件

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

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

我的问题很简单:我应该如何从 google-colab 读取 google-storage 中的存储桶?我什么都试过了:

  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'

解决方法

您不能仅使用 os 包在 gcs 上打开文件。如果您在文件系统中挂载 gcs 存储桶,那么文件可能可以通过 FUSE 供操作系统使用,那么您将能够做到这一点。但是为了使事情简单,您应该导入 gcs 将云存储导入为 gcs 然后使用 gcs_file = gcs.open(filename)

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

希望这能解决您的问题。

,

发现此 article 使用库 defp deps do [ {:phoenix,"~> 1.4.0"},{:phoenix_pubsub,"~> 1.1"},{:phoenix_ecto,"~> 4.0"},{:ecto_sql,"~> 3.0"},{:postgrex,">= 0.0.0"},{:gettext,"~> 0.11"},{:jason,"~> 1.0"},{:plug_cowboy,"~> 2.0"},{:httpoison,"~> 1.5"},{:elixir_uuid,"~> 1.2"},{:distillery,{:timex,"~> 3.5"},{:logger_json,"~> 3.0.2"},{:google_api_storage,"~> 0.1"},{:goth,"~> 0.8.0"},{:exmagick,"~> 0.0.1"},{:mime,{:mogrify,"~> 0.6.1"},{:gen_rmq,git: "https://github.com/akashsethiya/gen_rmq"},{:levenshtein,"~> 0.2.0"},# {:exredis,">= 0.2.4"},{:appsignal,{:magic_number,"~> 0.0.4"},{:google_api_pub_sub,"~> 0.3.0"},{:diplomat,"~> 0.2"},{:sparkpost,"~> 0.5.1"},{:jose,"~> 1.9"},{:redix,{:libcluster,{:mix_audit,"~> 0.1.4",only: [:dev,:test],runtime: false},{:sobelow,"~> 0.10.4",:test]},{:cowlib,"~> 2.9.0",override: true},{:gun,"~> 2.0.0",hex: :grpc_gun,{:grpc,github: "elixir-grpc/grpc"},{:protobuf,"~> 0.7.1"} ] end ,该库在 colab 中读取云存储桶。我查了一下 GCSFS,发现这个库处于测试阶段,不是 Google 的官方库。

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

只需确保首先在 collab 中安装库。

gcsfs

以下是您代码中的实现:

pip install gcsfs

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。