Google Cloud connection

What I’m trying to do:

Hello! I have a Colab program that asks the user for their name to access or create a bucket in Google Cloud Storage. Considering the files that exist in that bucket, the user begins an interaction with a chatbot based on vertex ai.

The interface created in Anvil works perfectly as long as I have run my Colab code and have made the authentications from there.

I want to move the entire program to Anvil, but I have had several problems with authentication to access my GCS from Anvil.

What I’ve tried and what’s not working:

I have tried the following method inside a fuction to check if it can login succesfully:

Code Sample:

def authenticate_and_list_buckets():

    if not os.path.exists(data_files['client_secret_.json']):
        with open(data_files['client_secret_.json'], "rb") as f:
          print(f.read())
    
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = data_files['client_secret_.json']

    project_id = "project123"

    storage_client = storage.Client(project=project123)

    try:

        buckets = list(storage_client.list_buckets())

        if buckets:
            return [bucket.name for bucket in buckets]
        else:
            return "Autenticación exitosa, pero no se encontraron buckets."
    except Exception as e:
        return f"Error al autenticar: {e}" ``` 

**Thanks for advance**
1 Like

I will start by mentioning that I’m not a professional in this field or have much experience but, I too wanted to integrate my Google Colab script with my Anvil app so I could utilize Colab’s GPU… However, I believe the Google Colab environment will timeout, disconnect, and will need manual intervention to restart. This is why I had to utilize Google Cloud Functions instead. I hope this info helps in some way.

1 Like

Thank you for your response.

Actually, I had to make a connection using de json and google auth library:

from google.oauth2 import service_account
import google.cloud.storage

credentials = service_account.Credentials.from_service_account_file((data_files['credentials.json']))
storage_client = storage.Client(credentials=credentials)

The json file was uploaded previously on the files section.
Thank you for you time.

1 Like