2
votes

I'm setting up Google Cloud Storage bucket CORS configuration using PHP API, but it doesn't seem to work

I read the document given in : https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.96.0/storage/bucket

Here's my Laravel source code:

    use Google\Cloud\Core\ServiceBuilder;

    ...

    $projectId = 'myProjectId';
    $bucketName = 'myBucketName';

    $gcloud = new ServiceBuilder([
        'keyFilePath' => 'resources/google-credentials.json',
        'projectId' => $projectId
    ]);

    $storage = $gcloud->storage();
    $bucket = $storage->bucket($bucketName);

    //change bucket configuration
    $result = $bucket->update([
        'cors' => [
            'maxAgeSeconds' => 3600,
            'method' => [
                "GET","HEAD"
            ],
            "origin" => [
                "*"
            ],
            "responseHeader" => [
                "Content-Type"
            ]
        ]
    ]);

    //print nothing and bucket doesn't changed
    dd($bucket->info()['cors']);

After execute this code, the bucket CORS configuration doesn't changed (My boss don't want me to use gsutil shell command to deal with this)

2

2 Answers

1
votes

You're very close! CORS accepts a list, so you'll just need to make a slight modification:

$result = $bucket->update([
    'cors' => [
        [
            'maxAgeSeconds' => 3600,
            'method' => [
                "GET","HEAD"
            ],
            "origin" => [
                "*"
            ],
            "responseHeader" => [
                "Content-Type"
            ]
        ]   
    ]
]);

Let me know if it helps :).

0
votes

The only thing I needed to change was when I config disks in laravel, using this code in config/filesystems.php when adding a disk for google:

'google' => [
    'driver' => 's3',
     'key' => 'xxx',
     'secret' => 'xxx',
     'bucket' => 'qrnotesfiles',
     'base_url'=>'https://storage.googleapis.com'
]

Here is the code example fist get file contents from request:

$file = $request->file('avatar')

second save it into storage:

Storage::disk('google')->put('avatars/' , $file);