如何从 AI Platform 作业访问 Google Cloud Storage Bucket

How to access Google Cloud Storage Bucket from AI Platform job

我的 Google AI Platform / ML Engine 训练作业似乎无法访问我放入 Google Cloud Storage 存储桶中的训练文件。

Google 的 AI Platform / ML Engine 要求您将训练数据文件存储在他们的一个 Cloud Storage 存储桶中。从 CLI 本地访问工作正常。但是,当我发送训练作业时(在确保数据位于我的 Cloud Storage 存储桶中的适当位置之后),我收到一个错误,似乎是由于无法访问存储桶 Link URL.

错误是因为我试图阅读 Google 提供的网页内容 "Hey, you don't have access to this." 我看到这个 gaia.loginAutoRedirect.start(5000, 和一个 URL 末尾有这个标志:noautologin=true.

我知道 AI Platform 和 Cloud Storage 之间的权限是一回事,但两者都在同一个项目下。我正在使用的演练至少暗示如果在同一项目下则不需要进一步的操作。

我假设我需要使用存储桶概览选项卡中提供的 Link URL。尝试了 Link for gsutil 但 python(来自 Google 的 CloudML Samples repo)对使用 gs://.

感到不安

我认为 Google 的示例被证明是不够的,因为它们的示例数据来自 public URL 而不是私有云存储桶。

最终,我收到的错误消息是 Python 错误。但就像我说的,这之前是 Google 的 HTML/CSS/JS 的一堆 INFO 日志,说我没有权限获取我想要获取的文件。这些日志实际上只是因为我也在 util.py 文件中添加了打印语句 - 就在训练文件的 read_csv() 之前。 (所以 Python 解析错误是由于试图将 HTML 解析为 CSV。

... 
INFO    g("gaia.loginAutoRedirect.stop",function(){var b=n;b.b=!0;b.a&&(clearInterval(b.a),b.a=null)});
INFO    gaia.loginAutoRedirect.start(5000,
INFO    'https:\x2F\x2Faccounts.google.com\x2FServiceLogin?continue=https%3A%2F%2Fstorage.cloud.google.com%2F<BUCKET_NAME>%2Fdata%2F%2Ftrain.csv\x26followup=https%3A%2F%2Fstorage.cloud.google.com%2F<BUCKET_NAME>%2Fdata%2F%2Ftrain.csv\x26service=cds\x26passive=1209600\x26noautologin=true',
ERROR   Command '['python', '-m', u'trainer.task', u'--train-files', u'gs://<BUCKET_NAME>/data/train.csv', u'--eval-files', u'gs://<BUCKET_NAME>/data/test.csv', u'--batch-pct', u'0.2', u'--num-epochs', u'1000', u'--verbosity', u'DEBUG', '--job-dir', u'gs://<BUCKET_NAME>/predictor']' returned non-zero exit status 1.
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/root/.local/lib/python2.7/site-packages/trainer/task.py", line 137, in <module>
    train_and_evaluate(args)
  File "/root/.local/lib/python2.7/site-packages/trainer/task.py", line 80, in train_and_evaluate
    train_x, train_y, eval_x, eval_y = util.load_data()
  File "/root/.local/lib/python2.7/site-packages/trainer/util.py", line 168, in load_data
    train_df = pd.read_csv(training_file_path, header=0, names=_CSV_COLUMNS, na_values='?')
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 678, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 446, in _read
    data = parser.read(nrows)
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 1036, in read
    ret = self._engine.read(nrows)
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.py", line 1848, in read
    data = self._reader.read(nrows)
  File "pandas/_libs/parsers.pyx", line 876, in pandas._libs.parsers.TextReader.read
  File "pandas/_libs/parsers.pyx", line 891, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas/_libs/parsers.pyx", line 945, in pandas._libs.parsers.TextReader._read_rows
  File "pandas/_libs/parsers.pyx", line 932, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas/_libs/parsers.pyx", line 2112, in pandas._libs.parsers.raise_parser_error
ParserError: Error tokenizing data. C error: Expected 5 fields in line 205, saw 961

为了获取数据,我或多或少试图模仿这个: https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/census/tf-keras/trainer/util.py

在我的 util.py 副本中,我尝试了多种方式来解决我的存储桶问题: https://console.cloud.google.com/storage/browser/<BUCKET_NAME>/data(认为这是 5 月份的 "Link URL")
https://storage.cloud.google.com/<BUCKET_NAME>/data(这是现在的 "Link URL" - 7 月)
gs://<BUCKET_NAME>/data(这是 URI - 它给出了关于不喜欢 gs 作为 url 类型的不同错误)

正在转载上面评论的答案:

看起来 URL 方法需要 cookie based authentication if it's not a public object. Instead of using a URL, I would suggest using tf.gfile with a gs:// path, as is used in the Keras sample. If you need to download the file from GCS in a separate step, you can use the GCS client library