google-app-engine - 在 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 文件。这意味着文件在那里,我们无法让应用程序读取它。这项服务是“工人”。
解决方案
从您的 App Engine 应用程序中,您无权访问您的静态文件。将文件移动到项目中的另一个位置,然后您就可以访问它了。
更重要的是,您不想将凭据存储在静态文件夹中!网络上的任何人都可以通过 URL 访问它们!
推荐阅读
- java - Android 文件:如果我将 .zip 文件复制到 /sdcard/Download,则 uri 会出错
- c - malloc 调用的静态替换
- reactjs - 如何将 MDBootstrap 卡排成一行?
- php - 内联显示 Wordpress 排队样式的 css 文件?
- c# - 如何在 asp.net core 3.0 中使用 TagHelpers?
- python - 可扣除的建模——难以用 scipy.optimize.minimize 实现收敛
- ruby-on-rails - 如何上传文件并处理处理和验证 - 一个非常笼统的概述?
- python - 左合并两个数据框并仅填充 Pandas 中的 NaN 值
- php - 添加存储库以构造会导致用户弃用错误
- php - thumbFileFullUrlArrPHP 图像上传 - 复制文件,制作缩略图并保存两个图像