【问题标题】:Internal server error using apache2 and google-cloud-storage使用 apache2 和 google-cloud-storage 的内部服务器错误
【发布时间】:2019-11-07 09:14:14
【问题描述】:

所以我设法把所有东西都连接起来。谷歌的路径和设置 apache2 似乎运行良好。但是我得到一个内部错误;/ 在服务器内。在函数中 hello

storage_client= storage.Client() 导致错误

在功能测试中

storage_client= storage.Client() 结果成功并且python能够找到secret.json 这对我来说很奇怪..



#__init__.py code
from flask import Flask
from google.cloud import storage

app = Flask(__name__)

@app.route("/")
def hello():
    #Does not work here
    storage_client= storage.Client()
    return "error"

#Works without @app.route("")
def test():    
    storage_client= storage.Client()
    # successfuly connects and prints
    print(storage_client) 

if __name__ == "__main__":
    app.run()

#wsgi code
#!/usr/bin/python
#!/usr/secret.json
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/FlaskApp/")
from google.cloud import storage
from FlaskApp import app as application

application.secret_key = 'Add your secret key'



这是日志,flaskapp.wsgi 中似乎有错误,但我不太确定...

mod_wsgi (pid=23303): Target WSGI script '/var/www/html/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.


mod_wsgi (pid=23303): Exception occurred processing WSGI script '/var/www/html/FlaskApp/flaskapp.wsgi'.

Traceback (most recent call last):
File "/var/www/html/FlaskApp/flaskapp.wsgi", line 7, in <module>
from FlaskApp import app as application
File "/var/www/html/FlaskApp/FlaskApp/__init__.py", line 5, in <module>
 storage_client = storage.Client()
File "/usr/local/lib/python2.7/dist-packages/google/cloud/storage/


raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
 DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or ex$

【问题讨论】:

  • 虚拟机是在本地还是在 GCP 上?
  • 否 ;/ @guillaumeblaquiere

标签: flask apache2 google-cloud-storage digital-ocean


【解决方案1】:

您需要为您计划使用的服务帐户提供凭据。正如您在错误中看到的那样,您可以为此设置environment variable GOOGLE_APPLICATION_CREDENTIALS

为此,请完成以下步骤:

  • 为您计划使用的服务帐户下载 JSON 格式的 service account key
  • (在 LinuxMacOS 上)运行: export GOOGLE_APPLICATION_CREDENTIALS="[PATH-TO-THE-KEY]"
  • (在 Windows 上):$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH-TO-THE-KEY]"

或者,您可以pass the path 到您的代码中的服务帐户密钥:

storage_client = storage.Client.from_service_account_json(
        'service_account_key.json')

【讨论】:

    猜你喜欢
    • 2021-04-05
    • 1970-01-01
    • 2018-11-23
    • 2016-08-31
    • 2023-02-17
    • 2011-08-08
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多