Upload python object to Google Cloud Storage without saving it to file

Question:

I am clipping images in Python 2.7 on Windows 10 and would like to send them to Google’s Cloud Vision API. For images saved locally, I can send them using the google.cloud python module like this:

                blob.upload_from_filename(filename=path)                        

Documented here

I would rather not save each crop to file, just to send the numpy arrays directly. I could change the numpy arrays to str objects and use

blob.upload_from_string()

but then Cloud Vision API would not be able to read them in my bucket. Is there any way to pipe from python to cloud storage bucket without wasting time/space saving them as an intermediate temporary object?

Asked By: bw4sz

||

Answers:

Unfortunately there is not. I am sorry!

Answered By: James

you can convert your numpy array to json and then

filename = "file_name_required on cloud"
json_object = [] # your array

and then do

blob = BUCKET.blob(filename)
blob.upload_from_string(data=json.dumps(json_object),content_type='application/json')
Answered By: umang garg