【问题标题】:Google AI platform can't write to Cloud StorageGoogle AI 平台无法写入云存储
【发布时间】:2021-02-26 10:43:49
【问题描述】:

在 Google AI Platform 上运行 tensorflow-cloud 作业,作业的入口点如下:

import tensorflow as tf

filename = r'gs://my_bucket_name/hello.txt'
with tf.io.gfile.GFile(filename, mode='w') as w:
  w.write("Hello, world!")

with tf.io.gfile.GFile(filename, mode='r') as r:
  print(r.read())

作业成功完成,在日志中打印“hello world”。

存储桶和作​​业都在同一个区域中。

但我在 Cloud Storage 中找不到该文件。它不在那里。我跑了一些其他的测试,我做了tf.io.gfile.listdir然后写了一个新文件,又写了tf.io.gfile.listdir,我打印了之前和之后,似乎添加了一个文件但是当我打开云存储时,我找不到它那里。还能够从存储中读取文件。

我没有收到任何权限错误,正如官方文档所说,AI Platform 已经拥有读取/写入 Cloud Storage 的权限。

这是我的main.py 文件:

import tensorflow_cloud as tfc

tfc.run(
   entry_point="run_me.py",
   requirements_txt="requirements.txt",
   chief_config=tfc.COMMON_MACHINE_CONFIGS['CPU'],
   docker_config=tfc.DockerConfig(
      image_build_bucket="test_ai_storage"),
)

这是我可以重现问题的最小版本。

【问题讨论】:

    标签: python tensorflow google-cloud-platform google-cloud-storage gcp-ai-platform-training


    【解决方案1】:

    云存储不是文件系统。考虑到这一点,您可以在存储桶中执行uploadsdownloadsDeletion 操作。

    您要做的是打开一个文件并写入它。您应该做的是在本地创建文件,然后将其上传到所需的存储桶。

    from google.cloud import storage
    
    
    def upload_blob(bucket_name, source_file_name, destination_blob_name):
        """Uploads a file to the bucket."""
        # bucket_name = "your-bucket-name"
        # source_file_name = "local/path/to/file"
        # destination_blob_name = "storage-object-name"
    
        storage_client = storage.Client()
        bucket = storage_client.bucket(bucket_name)
        blob = bucket.blob(destination_blob_name)
    
        blob.upload_from_filename(source_file_name)
    
        print(
            "File {} uploaded to {}.".format(
                source_file_name, destination_blob_name
            )
        )
    

    【讨论】:

      猜你喜欢
      • 2020-06-20
      • 1970-01-01
      • 2021-10-04
      • 2014-05-12
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      相关资源
      最近更新 更多