Google Storage - dynamically change ACL on single object (PHP)

Viewed 50

I need to update the ACL basically by adding or removing the allUsers entity.

I have the PHP library and what I'm doing at moment is:

$storage = new StorageClient([
    'projectId' => "xxxxx",
    'keyFilePath' => mykey, 
]);
$bucket = $storage->bucket('mybucket');
$acl = $bucket->acl('objectAccessControls', 'path/file/on/bucket');
if(add){
  $acl->add('allUsers', 'READER');
}else{
  $acl->delete('allUsers');
}

With this code actually changes ALL bucket configuration, not the file only.

How can I correctly specify the path of a specific file and change permissions only on path/file/on/bucket? I'm using the wrong functions?

Here the documentation that I'm using https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.90.0/storage/acl

This is the case if add: enter image description here

This is the else case: enter image description here

UPDATE 1: Using this to delete seems working -> https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls/delete

Tried to include the parameters listed here to the call I do, something like this:

$options = ['object' => 'path/obj'];
$acl->delete('allUsers', $options)

Still not working

1 Answers

Actually I've solved by using the Google_Service

  $client = new Google_Client();
  $client->setApplicationName('GoogleBuck/0.1');
  $client->useApplicationDefaultCredentials(); // app engine env
  $client->addScope('https://www.googleapis.com/auth/devstorage.full_control'); 
  $storage = new Google_Service_Storage($client);
  $acl = new Google_Service_Storage_ObjectAccessControl($client);
  $acl->setEntity('allUsers');
  $acl->setRole('READER');
  $acl->setBucket($bucketName);
  $acl->setObject($objectName);

To add

$response = $storage->objectAccessControls->insert($bucketName, $objectName, $acl);

To delete

$response = $storage->objectAccessControls->delete($bucketName, $objectName, 'allUsers');
 
Related