0
votes

I want to upload a file into GCS using a bearer token, and I know how to do it using REST API, but is there a way to do it using Google's SDK?

Basically I want to translate the following REST API request to the SDK.

def put_in_storage(bucket_name, blob_name, content, access_token, project)
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json',
    }

    url = f"https://storage.googleapis.com/upload/storage/v1/b/{bucket_name}/o/?uploadType=multipart&project={project}&name={blob_name}"

    return requests.post(url, data=content, headers=headers)

Doing the following using Google's SDK (as I read here)

from google.oauth2.credentials Credentials
from google.cloud import storage

def put_in_storage(bucket_name, blob_name, content, access_token):
    credentials = Credentials(access_token)
    gcs_client = storage.Client(credentials=credentials)
    bucket = gcs_client.bucket(bucket_name)
    blob = bucket.blob(blob_name)
    blob.upload_from_string(data=content)

Only raises the following error

google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

The reason I want to do it, is because the SDK wraps the upload type based on the file size (resumable/multipart).

1

1 Answers

0
votes

Adding project id to the Client initiation solved the problem (inspired by this discussion in Github)

gcs_client = storage.Client(credentials=credentials, project=project)