Flask:无法验证 Google Cloud Storage 客户端

Flask: Could not authenticate the Google Cloud Storage client

我正在制作一个网络应用程序,您可以在其中上传和下载文件,但我将其托管在 Heroku 上,因此我无法将文件存储在我的计算机上。我决定使用一种叫做 Flask-GoogleStorage 的东西,但它需要一个“Google Cloud Storage Client”。我去了云控制台并创建了一个服务帐户并将私钥放入我的代码中,但我一直收到此错误:

[2022-01-03 19:54:20,784] WARNING in google_storage: Could not authenticate the Google Cloud Storage client

这是我用来设置存储的代码:

app.config['GOOGLE_APPLICATION_CREDENTIALS'] = PRIVATE_SERVICE_KEY
app.config['GOOGLE_STORAGE_LOCAL_DEST'] = UPLOAD_FOLDER
app.config['SERVER_NAME'] = SERVER_NAME
#Google cloud storage
with app.app_context():
    files = Bucket("files")
    storage = GoogleStorage(files)
    app.config.update(
        GOOGLE_STORAGE_LOCAL_DEST = app.instance_path,
        GOOGLE_STORAGE_SIGNATURE = {"expiration": timedelta(minutes=5)},
        GOOGLE_STORAGE_FILES_BUCKET = "files-bucket-id"
    )
    storage.init_app(app)

任何建议都会非常有用,因为我以前没有使用过这个 google 平台,我很迷茫。

如果有任何更简单的在线存储文件的方法,我也将不胜感激。

提前致谢!

好的,我认为我明白你需要做什么。

您需要安装 Cloud SDK (gcloud) to follow these instructions but you can also do everything using Google's Cloud Console:

BILLING=[[YOUR-BILLING-ACCOUNT]]
PROJECT=[[YOUR-PROJECT]]
BUCKET=[[YOUR-BUCKET]]
ACCOUNT=[[YOUR-SERVICE-ACCOUNT]]

# Create Project
gcloud projects create ${PROJECT}

# Associate Billing required for Cloud Storage
gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}

# Create Bucket
gsutil mb -p ${PROJECT} gs://${BUCKET}

# Create Service Account
gcloud iam service-accounts create ${ACCOUNT} \
--project=${PROJECT}

EMAIL=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com

# Grant Service Account permission to Cloud Storage
gcloud projects add-iam-policy-binding ${PROJECT} \
--role=roles/storage.admin \
--member=serviceAccount:${EMAIL}

# Create Service Account Key
gcloud iam service-accounts keys create ${PWD}/${ACCOUNT}.json \
--iam-account=${EMAIL} \
--project=${PROJECT}

# Export
export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/${ACCOUNT}.json

flask run

和(用值替换 [[VARIABLES]]):

from flask_googlestorage import GoogleStorage, Bucket

files = Bucket("files")
storage = GoogleStorage(files)

app = Flask(__name__)
app.config.update(
    GOOGLE_STORAGE_LOCAL_DEST = app.instance_path,
    GOOGLE_STORAGE_SIGNATURE = {"expiration": timedelta(minutes=5)},
    GOOGLE_STORAGE_FILES_BUCKET = "[[BUCKET]]"
)
storage.init_app(app)