user9882001
user9882001

Reputation: 67

Accessing google cloud storage from cloud function(without download)

I am running a Google cloud function, which accesses the google cloud storage bucket for an excel file. My goal is to read the file and do some calculations with it, but is it possible without downloading the file to the /tmp folder? Things I have tried:

  storage_client = storage.Client()
  file_obj = storage_client.get_bucket(‘bucketname’).get_blob('filename.xlsx')
  
  excel_file = None
  file_obj.download_to_file(excel_file)
  wb = openpyxl.load_workbook(excel_file)

at first I thought I could attain a file object, but then after I read the error message I realised I was being asked for a file path so I would have to download to the /tmp folder, which is something I would like to avoid.

I also tried download_as_bytes(), but unfortunately openpyxl cannot read bytes.

Any help/hint would be appreciated :)

Upvotes: 0

Views: 543

Answers (1)

Rafael Lemos
Rafael Lemos

Reputation: 5819

As you can see in this Community Answer:

In the docs for openpyxl.load_workbook it says:

#:param filename: the path to open or a file-like object

So if you have bytes as the input you can assemble a "proto-object" to satisfy the parameter requirements of openpyxl.load_workbook and it will work, so like the example below:

from io import BytesIO
...
excel_file = None
file_obj.download_as_bytes(excel_file)
wb = load_workbook(filename=BytesIO(excel_file.read()))

Upvotes: 1

Related Questions