首页 > 解决方案 > 谷歌云存储 500 内部服务器错误

问题描述

设置控制器.php

    namespace App\Controllers;

    use Google\Cloud\Storage\StorageClient;

    class SettingsController extends Controller {

        public function createBucket($request, $response) {
            $projectId = 'myProjectId'; //here I specified my projectId 

            $storage = new StorageClient([
                'projectId' => $projectId
            ]);

            $bucketName = 'socnetfilestestkekdsfa213kfh34';
            $bucket = $storage->createBucket($bucketName);
        }
     }

所以,我按照文档编写了这段代码,但我总是收到 500 Network Error。

这是我的index.php

<?php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../config/db.php';

$app = new \Slim\App(['settings' => $config]);

require __DIR__ . '/../app/dependencies.php';
require __DIR__ . '/../app/routes.php';

$app->run();

这是我的项目结构:

后端项目结构

标签: phpgoogle-cloud-platformgoogle-cloud-storage

解决方案


最后,我解决了这个问题。正如我提到require __DIR__ . '/../vendor/autoload.php';的是正确的。就我而言, 500 Error 的原因是环境变量。对于已部署的应用程序,您应该将您的 Service Google Cloud 帐户密钥 config.json 放入您的项目中。我把我的放在一个配置文件夹中(检查我上面的项目结构)。这意味着在 Heroku 的“设置”中,我需要指定 GOOGLE_APPLICATION_CREDENTIALS: ../config/yourAppName.json

感谢这篇文章: https ://medium.com/@naz_islam/how-to-authenticate-google-cloud-services-on-heroku-for-node-js-app-dda9f4eda798


推荐阅读