Home > other >  Download blob from gcp storage bucket with python flask
Download blob from gcp storage bucket with python flask

Time:09-28

I have a GCP bucket with some pdf files. I am able to download the files from bucket with python(flask) on local host using below code -

storage_client = storage.Client.from_service_account_json("ebillaae7f0224519.json")
bucket = storage_client.get_bucket("bill_storage1")
blob = bucket.blob(file_name)
blob.download_to_filename("file1.pdf")

But when deployed on GCP(app engine) i get this error "line 1282, in download_to_filename with open(filename, "wb") as file_obj: OSError: [Errno 30] Read-only file system: 'file1.pdf'" Then i modified code to -

storage_client = storage.Client.from_service_account_json("ebillaae7f0224519.json")
bucket = storage_client.get_bucket("bill_storage1")
blob = bucket.blob(file_name)
blob.download_to_filename("/tmp/file1.pdf")

This works after deployment but i cant download the files from bucket to local folder through my app, how to do that?

CodePudding user response:

You can use tempfile.gettempdir() function to get the tmp directory according to your runtime environment

For example

import tempfile

storage_client = storage.Client.from_service_account_json("ebillaae7f0224519.json")
bucket = storage_client.get_bucket("bill_storage1")
blob = bucket.blob(file_name)
blob.download_to_filename("{}/file1.pdf".format(tempfile.gettempdir()))

Locally, the file will be download in your OS default temporary directory.

CodePudding user response:

You cannot write or make modifications within Google App Engine's file system post deployment. You can only read files within the app. That is why certain storage solutions such as Cloud Storage or any database is a recommended way to store files or data.

Google App Engine is serverless. It can scale up quickly because it is simply spinning up instances based from the container image built from your source code and pre-built runtime. Once the container image is built during deployment, you can no longer write/edit its contents. You will have to redeploy to make changes.

One exception is the directory called /tmp where you can store temporary files. It's because this directory is mounted in the instance's RAM. Storing temporary data consumes RAM. Therefore, when the instance is deleted, files in the /tmp directory are deleted as well.

You may check this documentation for additional information.

  • Related