0
votes

I'm trying to upload a file to my Google Cloud Bucket, however I'm not sure how to include the credentials. I have a .json file containing the credentials, and can create a ServiceBuilder, but I don't know how to then use this to upload the file (I've been using this: https://github.com/GoogleCloudPlatform/google-cloud-php#google-cloud-storage-ga)

The following returns a 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');

?>
1

1 Answers

0
votes

Ah I was being thick.

Theres no need for the service builder:

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