允许 Google 云计算引擎实例将文件写入 Google 存储桶 - Python

Allow Google Cloud Compute Engine Instance to write file to Google Storage Bucket - Python

在我的 python 服务器脚本中 运行 在 google 云 VM 实例上,它尝试在存储中保存图像 (jpeg)。但它抛出以下错误。

File "/home/thamindudj_16/server/object_detection/object_detector.py", line 109, in detect Hand new_img.save("slicedhand/{}#sliced_image{}.jpeg".format(threadname, i)) File

"/home/thamindudj_16/.local/lib/python3.5/site-packages/PIL/Image.py", line 2004, in save fp = builtins.open(filename, "w+b")

OSError: [Errno 5] Input/output error: 'slicedhand/thread_1#sliced_image0.jpeg'

包括 python 脚本在内的所有文件都在 google 存储桶 中,并已使用 gcsfuse 安装到 VM 实例。应用程序尝试将新图像保存在 slicedhand 文件夹中。

Python 发生图像保存的代码片段。

from PIL import Image

...
...

i = 0
new_img = Image.fromarray(bounding_box_img)      ## conversion to an image
new_img.save("slicedhand/{}#sliced_image{}.jpeg".format(threadname, i))

我认为问题可能出在权限访问上。医生说使用 --key_file。但是我应该使用什么密钥文件以及在哪里可以找到它。我不清楚是这个问题还是其他问题。

如有任何帮助,我们将不胜感激。

我了解到您正在 Linux VM 实例上使用 gcfuse 来访问 Google 云存储。

密钥文件是一个服务帐户凭据密钥,它将允许您将 Cloud SDK 或客户端库作为另一个服务帐户启动。 You can download key file from Cloud Console。但是,如果您使用的是 VM 实例,则会自动使用 Compute Engine 默认服务帐户。您可以使用控制台命令检查它:$ gcloud init.

configure properly your credentials, please follow the documentation

Compute Engine 默认服务帐户,需要 enabled Access Scope Storage > Full。访问范围是限制对云 API 的访问级别的机制。这可以在机器创建期间或 VM 实例停止时完成。

请注意,Access Scopes 是为您 select VM 实例的服务帐户明确定义的。

Cloud Storage objects names have requirements. It is strongly recommended avoid using hash symbol "#" in the names of the objects.