上传到Google Cloud Bucket - 凭据无效

时间:2018-04-29 08:28:03

标签: php google-cloud-platform google-cloud-storage

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

以下内容返回401:凭据无效:

<?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');

?>

1 个答案:

答案 0 :(得分:0)

啊,我很厚。

Theres不需要服务构建器:

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