在本地运行gcloud测试

时间:2019-06-13 22:22:18

标签: google-cloud-platform google-cloud-storage pytest google-cloud-build

我有一个预提交的挂钩安装程序,用于运行Google Cloud Storage api的测试套件。我可以模拟Google Build并运行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)

1 个答案:

答案 0 :(得分:2)

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

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

或使用

gcloud auth application-default login