Thamindu DJ
Thamindu DJ

Reputation: 1949

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

In my python server script which is running on a google cloud VM instance, it tries to save an image(jpeg) in the storage. But it throws following error.

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'

All the files including python scripts are in a google storage bucket and have mounted to the VM instance using gcsfuse. App tries to save new image in the slicedhand folder.

Python code snippet where image saving happen.

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

I think may be the problem is with permission access. Doc says to use --key_file. But what is the key file I should use and where I can find that. I'm not clear whether this is the problem or something else.

Any help would be appreciated.

Upvotes: 3

Views: 2793

Answers (1)

Pawel Czuczwara
Pawel Czuczwara

Reputation: 1520

I understand that you are using gcfuse on your Linux VM Instance to access Google Cloud Storage.

Key file is a Service Account credentials key, that will allow you to initiate Cloud SDK or Client Library as another Service Account. You can download key file from Cloud Console. However, if you are using VM Instance, you are automatically using Compute Engine Default Service Account. You can check it using console command: $ gcloud init.

To configure properly your credentials, please follow the documentation.

Compute Engine Default Service Account, need to have enabled Access Scope Storage > Full. Access Scope is the mechanism that limits access level to Cloud APIs. That can be done during machine creation or when VM Instance is stopped.

Please note that Access Scopes are defined explicitly for the Service Account that you select for VM Instance.

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

Upvotes: 2

Related Questions