$analytics = new \Google_Service_Storage($client); $objects=$analytics->objects->listObjects({BUCKET}); $object=$analytics->objects->get({BUCKET},$objects->items[0]->name); 

I receive an object, a link to a file, but I do not understand how to download

    2 answers 2

     $ch = curl_init($object); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); $output = curl_exec($ch); $fh = fopen("file_name.ext", 'w'); fwrite($fh, $output); fclose($fh); 

    or

     $url = $object; //Взять url из объекта - в предыдущем тоже также надо file_put_contents('file_name.ext', file_get_contents($url)); 
    • Yes, so I tried. The problem is that with such methods of downloading the file, the request is not authorized and the file is not given accordingly. The answer is “Anonymous users doesn’t have storage.objects.get access to { package_name }” - bbn
    • Then you need to pull the authorization headers out of this object, if possible - Dmytro K
     $analytics = new \Google_Service_Storage($client); $name={OBJECT_NAME}; $bucket={BUCKET}; $file = $analytics->objects->get($bucket, $name, ['alt' => 'media']); file_put_contents(basename($name), $file->getBody()); 

    the decision was the following