如何上传文件到谷歌云存储,并将其设置为公共权限,并获得链接回来


How to upload file to google cloud storage and set it to public permissions and getting the link back

我正在上传文件到谷歌云存储,我想上传具有公共权限的文件,如果可能的话,也可以得到响应的链接。

现在我只是上传…

My code

require_once  dirname(dirname(__FILE__)).'/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('/blabla.json');
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
$storage = new Google_Service_Storage($client);
$fileTmpName="blabla.jpg";
$file_name = "bodzin";
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($file_name);
$storage->objects->insert(
"creatives_new",
$obj,
['name' => $file_name, 'data' => file_get_contents($fileTmpName),'uploadType' => 'media']
);

我发现了这颗宝石:

https://cloud.google.com/storage/docs/access-control/making-data-public storage-make-object-public-php

use Google'Cloud'Storage'StorageClient;
/**
* Make an object publically accessible.
*
* @param string $bucketName the name of your Cloud Storage bucket.
* @param string $objectName the name of your Cloud Storage object.
*
* @return void
*/
function make_public($bucketName, $objectName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $object = $bucket->object($objectName);
    $object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
    printf('gs://%s/%s is now public' . PHP_EOL, $bucketName, $objectName);
}

值得注意的另一件有趣的事情是,链接似乎将永远存在https://storage.googleapis.com//