运行 gcloud 本地测试

Running gcloud test locally

我有一个预提交挂钩设置 运行 Google 云存储 api 的测试套件。我可以模拟 Google 构建和 运行 测试 cloud-build-local --config=cloudbuild.yaml --dryrun=false .。但是,当 运行 预提交挂钩时,我收到错误

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

示例 google_cloud_api.py 函数

def list_blobs(bucket_name):
    """Returns list of names of all the blobs in the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blobs = bucket.list_blobs()
    return [blob.name for blob in blobs]

示例 test_google_cloud_api.py 函数

def test_get_all_files():
    blobs = google_cloud_storage_api.list_blobs(GCS_BUCKET_NAME)
    for blob in blobs:
        print(blob)

创建一个服务帐户并下载 json 文件,将 GOOGLE_APPLICATION_CREDENTIALS 设置为该文件

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account.json

或使用

gcloud auth application-default login