I want write Google Cloud Function to get data from a bucket to label box here is the function this gives Forbidden error in labelbox side how to fix that code

I want write Google Cloud Function to get data from a bucket to label box here is the function code

import labelbox
from labelbox import Client, Dataset
import os
import uuid
import logging

# Add your API key below
LABELBOX_API_KEY = "my api key"
client = Client(api_key=LABELBOX_API_KEY)

def upload_asset(event, context):
    """Uploads an asset to Catalog when a new asset is uploaded to GCP bucket. 
       If a dataset with bucket_name exists in Catalog, then an asset is added to that dataset. Otherwise, a new dataset is created.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    file = event
    bucket_name = file['bucket']
    object_name = file["name"]
    
    try:
        datasets = client.get_datasets(where=Dataset.name == bucket_name)
        dataset = next(datasets, None)
        if not dataset:
            dataset = client.create_dataset(name=bucket_name)
        url = f"gs://{bucket_name}/{object_name}"
        dataset.create_data_row(row_data=url, external_id=object_name )
        logging.getLogger().setLevel(logging.DEBUG)
        return "success"
    except Exception as e:
        print(f"Error: {e}")
        return "failure"

this gives Forbidden error in labelbox side how to fix that

I wrote a Google Cloud Function in Python to read data from a Google Cloud Storage bucket and upload it to Labelbox using the Labelbox Python SDK. I expected the function to run without errors and upload the data to my Labelbox project.

However, when I triggered the function, I received a “Forbidden” error message in Labelbox, and the function did not complete successfully. The error message indicated that Labelbox was unable to authenticate my request, but I’m not sure what’s causing the problem.

I’ve checked that my Labelbox API key is correct and that I have the necessary permissions to upload data to my Labelbox project. I’ve also confirmed that my Google Cloud Function is able to read the data from the bucket successfully.

I’m not sure what else to try to fix this issue. Can you help me understand what might be causing the “Forbidden” error and how to resolve it?

Hi @ishan , have you tried following this guide about using Cloud functions? How to sync your cloud storage with Catalog

1 Like

yes I followed that article