Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Fetch Data From API

I have an api url . I need to fetch the data from api  and store in GCS bucket as CSV file using python cloud function.

Can anyone help me on this ?

0 1 288
1 REPLY 1

Hi @NishinThattil,

To fetch data from an API and store it as a CSV file in a Google Cloud Storage (GCS) bucket using a Python Cloud Function, follow these steps:

  1. Install the necessary libraries: You'll need the requests and google-cloud-storage libraries
  2. Set up authentication: Your Cloud Function needs permissions to access GCS. Ensure your service account has the necessary roles (e.g., Storage Object Admin). The easiest way is often to grant the service account these roles at the bucket level.
  3. Deploy the function: Use the Google Cloud CLI to deploy your function. You'll need a requirements.txt file listing the dependencies.

Remember to replace placeholders like <YOUR_REGION>, YOUR_API_URL, YOUR_GCS_BUCKET_NAME with your actual values. The optional --set-env-vars avoids hardcoding the API URL directly in the code. You can manage environment variables through the Google Cloud console.

I hope the above information is helpful.