3
votes

Google Cloud Storage (not the same as Google Drive) appears to have compatibility with S3 APIs:

https://developers.google.com/storage/docs/migrating#migration-simple

Does anyone know if I can use the aws/aws-sdk-php (https://packagist.org/packages/aws/aws-sdk-php) package and configure it to connect to my Google Cloud Storage instead of AWS S3?

I have tried the following code:

<?php 
use Aws\S3\S3Client;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\AwsS3 as Adapter;
require_once 'vendor/autoload.php';
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', 1);


$client = S3Client::factory(array(
    'key'    => 'MY_GCS_KEY',
    'secret' => 'MY_GCS_SECRET',
    'endpoint' => 'storage.googleapis.com'
));
$filesystem = new Filesystem(new Adapter($client, 'MY_GCS_BUCKET'));
$filesystem->write('filename.txt', 'contents');

But this give me an error:

Fatal error: Uncaught Aws\S3\Exception\InvalidAccessKeyIdException: AWS Error Code: InvalidAccessKeyId, Status Code: 403, AWS Request ID: BF7C1317719A4C67, AWS Error Type: client, AWS Error Message: The AWS Access Key Id you provided does not exist in our records., User-Agent: aws-sdk-php2/2.6.15 Guzzle/3.9.2 curl/7.32.0 PHP/5.5.4-1+debphp.org~raring+1 thrown in /var/www/prudhub/dev/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91

Anyone know how or if I can properly setup the aws/aws-sdk-php package to connect to Google Cloud Storage instead?

EDIT

Here is the code that made it work:

<?php 
use Aws\S3\S3Client;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\AwsS3 as Adapter;
require_once 'vendor/autoload.php';


$client = S3Client::factory(array(
    'key'    => 'MY_GCS_KEY',
    'secret' => 'MY_GCS_SECRET',
    'base_url' => 'https://storage.googleapis.com'
));
$filesystem = new Filesystem(new Adapter($client, 'MY_GCS_BUCKET'));
$filesystem->write('filename.txt', 'contents');
1

1 Answers