为了账号安全,请及时绑定邮箱和手机立即绑定

Google Cloud Storage 身份验证引发错误

Google Cloud Storage 身份验证引发错误

PHP
侃侃无极 2021-06-17 21:15:36
我正在尝试编写一个脚本,允许我将文件上传到 Google Storage。这是我到目前为止所得到的: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我检查以确保以下文件存在,他们这样做:我的密钥文件.json文件测试.txt我错过了什么?
查看完整描述

1 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

从创建 JSON 凭证IAM->Service accounts并授予存储管理员权限。


设置GOOGLE_APPLICATION_CREDENTIALS者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

]);


查看完整回答
反对 回复 2021-06-19
  • 1 回答
  • 0 关注
  • 239 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信