首页 > 解决方案 > 在 google app engine 2.7 标准环境中访问静态 json 文件 - IOError(2, 'No such file or directory')

问题描述

我们正在尝试访问 Google App Engine 中 Python 代码中的 .json 文件。

这是我们的 app.yaml

service: worker
instance_class: F2
runtime: python27
api_version: 1
threadsafe: true
automatic_scaling:
  max_instances: 100
  min_pending_latency: 200ms  # default value
  max_pending_latency: automatic

handlers:

- url: /stylesheets
  static_dir: stylesheets

- url: /images
  static_dir: images

- url: /javascripts
  static_dir: javascripts

- url: /(.*\.(gif|png|jpg|json))$
  static_files: static/\1
  upload: static/.*\.(gif|png|jpg|json)$
  application_readable: true


- url: /static
  static_dir: static
  application_readable: true 

- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico


- url: /.*
  script: worker.app
  login: admin

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest

我们像这样在我们的应用程序中使用它。

 path = os.path.join(os.path.dirname(__file__)[0], '/static/google_web_app_script_credentials.json')
 flow = InstalledAppFlow.from_client_secrets_file(path,self.GOOGLESHEETS_SCOPES )

但是我们收到了错误。

IOError(2, 'No such file or directory')

我们尝试了各种更改路径的选项,包括 path = os.path.join(os.path.dirname( file )[0], '../static/google_web_app_script_credentials.json')

当我们转到 GCP 控制台 -> 调试并查看当前代码时,我们能够看到静态文件夹下的 .json 文件。这意味着文件在那里,我们无法让应用程序读取它。这项服务是“工人”。

标签: google-app-enginegoogle-app-engine-python

解决方案


从您的 App Engine 应用程序中,您无权访问您的静态文件。将文件移动到项目中的另一个位置,然后您就可以访问它了。

更重要的是,您不想将凭据存储在静态文件夹中!网络上的任何人都可以通过 URL 访问它们!


推荐阅读