谷歌人工智能平台无法写入云存储

2024-10-02 22:32:31 发布

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

在Google AI平台上运行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”

铲斗和作业都位于同一区域

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

我没有得到任何权限错误,正如官方文件所说,AI平台已经有了读/写云存储的权限

这是我的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"),
)

这是我可以重现问题的最简单版本


Tags: 文件ioimporttxtcloudbuckettftensorflow
1条回答
网友
1楼 · 发布于 2024-10-02 22:32:31

云存储不是一个文件系统。记住这一点,您可以在bucket中执行uploadsdownloadsDeletion操作

您试图做的是打开一个文件并写入其中。您应该做的是在本地创建文件,然后将其上载到所需的bucket

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
        )
    )

相关问题 更多 >