我正在 docker-compose.yml 文件中设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量。这是我的 docker-compose.yml 文件的结构:
version: "2"
services:
server:
container_name: server
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/dir1/dir2/file1.json
我的 JSON 文件位于上述目录中。从我的 python 代码(也作为 Docker 容器的一部分运行)中,我使用 Google Speech API,如下所示:
import io
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
#take speech wav file and turn it into text. return text
def transcribe_file(speech_file):
"""Transcribe the given audio file."""
client = speech.SpeechClient()
当我执行上面的操作时,它会抛出错误 google.auth.exceptions.DefaultCredentialsError: File/dir1/dir2/file1.json was not found.
我无法理解为什么当 JSON 存在于正确的目录中时却找不到它。
最佳答案
确保将 /dir1/dir2/file1.json
添加到 Docker 镜像或将其安装为卷。仅将文件放在主机上的该位置是不够的。
关于python - 在docker compose yml文件中设置环境变量时出现文件未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52195378/