首页 > 解决方案 > 上传到 Google Cloud Bucket - 凭据无效

问题描述

我正在尝试将文件上传到我的 Google Cloud Bucket,但是我不确定如何包含凭据。我有一个包含凭据的 .json 文件,并且可以创建一个 ServiceBuilder,但我不知道如何使用它来上传文件(我一直在使用这个:https ://github.com/GoogleCloudPlatform/google -cloud-php#google-cloud-storage-ga

以下返回 401:Invalid Credentials:

<?php

  require 'vendor/autoload.php';

  use Google\Cloud\Storage\StorageClient;
  use Google\Cloud\Core\ServiceBuilder;

  // Authenticate using a keyfile path
  $cloud = new ServiceBuilder([
      'keyFilePath' => 'keyfile.json'
  ]);

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

  $bucket = $storage->bucket('storage-123456');

  // Upload a file to the bucket.
  $bucket->upload(
      fopen('data/file.txt', 'r')
  );

  // Using Predefined ACLs to manage object permissions, you may
  // upload a file and give read access to anyone with the URL.
  $bucket->upload(
      fopen('data/file.txt', 'r'),
      [
          'predefinedAcl' => 'publicRead'
      ]
  );

  // Download and store an object from the bucket locally.
  $object = $bucket->object('file_backup.txt');
  $object->downloadToFile('data/file_backup.txt');

?>

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

解决方案


啊,我是厚。

不需要服务构建器:

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

推荐阅读