Google Cloud Storage身份验证引发错误 [英] Google Cloud Storage authentication throws errors

查看:243
本文介绍了Google Cloud Storage身份验证引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本允许我将文件上传到Google Storage.这是到目前为止我得到的:

I'm trying to write a script that will allow me uploading files to Google Storage. Here's what I've got so far:

define("PROJECT_ID", "my-project");
define("BUCKET_NAME", "my-bucket");

require '../../vendor/autoload.php';

use Google\Cloud\Storage\StorageClient;

// Authenticating with a keyfile path.
$storage = new StorageClient([
    'keyFilePath' => '../../my-keyfile.json'
]);

// Providing the Google Cloud project ID.
$storage = new StorageClient([
    'projectId' => PROJECT_ID
]);

$bucket = $storage->bucket(BUCKET_NAME);

$uploadFile = '../fileTest.txt';

$bucket->upload(
    fopen($uploadFile, 'r')
);

我不断收到以下错误:

Fatal error: Uncaught Google\Cloud\Core\Exception\ServiceException: { "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } } in /home/domain/vendor/google/cloud/Core/src/RequestWrapper.php:362 Stack trace: #0 /home/domain/vendor/google/cloud/Core/src/RequestWrapper.php(206): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException)) #1 /home/domain/vendor/google/cloud/Core/src/Upload/MultipartUploader.php(64): Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array) #2 /home/domain/vendor/google/cloud/Storage/src/Bucket.php(285): Google\Cloud\Core\Upload\MultipartUploader->upload() #3 /home/domain/public_html/dev/incl/test.php(39): Google\Cloud\Storage\Bucket->upload(Resource id #60) #4 /home/domain/public_html/dev/index.php(42): include_once('/hom in /home/domain/vendor/google/cloud/Core/src/RequestWrapper.php on line 362

我检查以确保以下文件已存在,并且它们确实存在:

I checked to make sure the folloring files exist, and they do:

  • my-keyfile.json
  • fileTest.txt

我想念什么?

推荐答案

IAM->Service accounts创建JSON凭证,并授予Storage Admin权限.

Create the JSON credential from IAM->Service accounts and grant Storage Admin permissions.

通过export GOOGLE_APPLICATION_CREDENTIALS="YOUR-my-keyfile.json"

或更新您的代码:

$storage = new StorageClient([
    'keyFilePath' => '../../my-keyfile.json'
]);

// Providing the Google Cloud project ID.
$storage = new StorageClient([
    'projectId' => PROJECT_ID
]);

$storage = new StorageClient([
    'keyFilePath' => '../../my-keyfile.json',
    'projectId' => PROJECT_ID
]);

这篇关于Google Cloud Storage身份验证引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆