如何使用凭据文件而不将其推到存储库

发布于 2025-02-06 16:21:33 字数 359 浏览 2 评论 0原文

我正在构建一个在GCP(Google Cloud Platform)上部署的Django Web应用程序。我需要使用Google Cloud存储存储桶来存储从应用程序生成的文件,因此我将代码添加到settings.py

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(BASE_DIR, 'credential.json')

中。目前,我将凭据文件放在计算机上的项目目录中,并且效果很好。但是现在,我需要将项目推向项目交接的公共存储库,我无法推动凭据文件,因为它包含了云存储存储桶的私钥。我该怎么做才能使程序正常运行,而无需将凭据文件推向存储库或其他人可以访问的文件?

I am building a Django web app that is deployed on GCP (google cloud platform). I need to use the google cloud storage bucket to store files generated from the app, So I added the code to settings.py

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(BASE_DIR, 'credential.json')

In the code, credential.json is referred to. Currently, I put the credential file in the project directory on my computer and it works fine. But now I need to push the project to a public repository for project handoff and I can’t push the credential file because it contains the private key to the cloud storage bucket. What should I do to make the program run normally without pushing the credential file to the repository or the file being accessible by other people?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2025-02-13 16:21:34

不要推动凭据文件,并告诉用户在项目文档中创建它。

一个常见的模式是提供一个示例文件,上面有虚拟数据,以帮助用来了解文件的结构。
您将拥有这样的文档:

复制 recredential.example.json recredential.json ,并使用您的服务器设置填充。

// file: credentials.example.json 
{
    "token": "obviously not my token",
    "email": "[email protected]",
}

Don't push the credential files and tell the user to create it in the project documentation.

A common pattern is to provide an example file with dummy data to help uses to understand the structure of the file.
You will have a documentation like this:

Copy the credential.example.json as credential.json and fill it with your server settings.

// file: credentials.example.json 
{
    "token": "obviously not my token",
    "email": "[email protected]",
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文