MrK
MrK

Reputation: 1078

Google Cloud Storage API - new ACL?

I am using the Google Cloud Storage SDK for PHP for uploading resources, but i am having issues making the file i upload publicly available, for example i tried using acl=> ['READER' => 'allUsers'] in the upload config.

Anyways, i would like to be able to share all resources i upload to the allUsers user, therefore granting access to the images publicly.

Here is my code:

public function upload($sourceFilename, $destFilename) {

        $bucket = $this->storage->bucket(self::IMAGE_BUCKET_NAME);

        //Base options
        $options = [
            'name' => $destFilename,
            'validate' => true,
            'acl' => [
                'READER' => 'allUsers'
            ]
        ];

        //Local file read:
        $fileHandle = @fopen($sourceFilename, 'r');

        if ($fileHandle === false) {
            throw new \Exception("Failed to open file for upload.");
        }

        //Resumable uploader:
        $uploader = $bucket->getResumableUploader($fileHandle, $options);


        try {
            $object = $uploader->upload();

        } catch (GoogleException $ex) {
            $resumeUri = $uploader->getResumeUri();
            $object = $uploader->resume($resumeUri);
            //do some error handling ?
        }
    }

Upvotes: 0

Views: 91

Answers (1)

Brandon Yarbrough
Brandon Yarbrough

Reputation: 38379

Try changing the 'acl' section to:

'predefinedAcl' => 'publicRead'

Upvotes: 1

Related Questions