您好我已经创build了一个脚本,应该使一组图像文件在谷歌存储桶公众可读。 后来另一个脚本将使他们再次私密。
脚本运行在GCE(Google计算引擎)上。 它开始运行正常,并公开设置文件,但在它完成所有文件之前遇到以下致命exception
Uncaught exception 'DomainException' with message 'Could not load the default credentials. in (...)/vendor/google/auth/src/ApplicationDefaultCredentials.php:148 Stack trace: #0 (...)/vendor/google/apiclient/src/Google/Client.php(1053): Google\Auth\ApplicationDefaultCredentials::getCredentials('https://www.goo...') #1 (...)/vendor/google/apiclient/src/Google/Client.php(354): Google_Client->createApplicationDefaultCredentials() #2 (...)/vendor/google/apiclient/src/Google/Client.php(777): Google_Client->authorize() #3 (...)/vendor/google/apiclient/src/Google/Service/Resource.php(232): Google_Client->execute(Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #4 (...)/vendor/google/auth/src/ApplicationDefaultCredentials.php on line 148
我正在使用的代码如下。
require "../vendor/autoload.php"; $client = new Google_Client(); $client->useApplicationDefaultCredentials(); $client->addScope(Google_Service_Storage::DEVSTORAGE_READ_WRITE); $storage = new Google_Service_Storage($client); $acl = new Google_Service_Storage_ObjectAccessControl(); $acl->setEntity('allUsers'); $acl->setRole('READER'); foreach($objects as $object){ $files = $storage->objects->listObjects($GLOBALS["bucket"],array("prefix"=>$object->name())); $thumbs = $storage->objects->listObjects($GLOBALS["bucket"],array("prefix"=>"thumb/".$object->thumb())); $files = array_merge($files["items"],$thumbs["items"]); foreach($files as $file){ if(strpos($file["name"],".pdf")===false) $storage->objectAccessControls->insert($GLOBALS["bucket"], $file["name"], $acl); } }
任何人有任何build议,为什么默认凭据有时无法find?
我改变了我的代码,通过使用php的“exec”命令来使用“gsutil acl ch”命令。 这工作没有任何问题,并且更快一点,因为我可以使用-m参数作为mulitprocess运行gsutil。
但是为什么其他脚本无法检索默认凭据的问题有时仍然存在。 我尝试将函数包装在try / catch块中,当遇到exception时,强制它重试几次。 在这些重试上,可以再次find默认凭据,并且该脚本继续在设置文件上的ACL之前继续执行,直到它再次失败。
似乎从来没有版本的API不具有相同的问题。
新版本与旧版本略有不同。 我一直在使用他们的ServiceBuilder,而不是创build一个Google_Client对象。 代码现在看起来像这样。
$builder = new ServiceBuilder([ 'projectId' => "[projectID]", 'keyFilePath' => "[keyFilePath]", ]); $bucket = $builder->storage()->bucket("[bucketName]"); $objects = $this->bucket->objects(["prefix"=>"[PathPrefix]"]); foreach($objects as $object) $object->acl()->add("allUsers","READER");