【发布时间】:2019-12-13 13:29:18
【问题描述】:
我在 Google Cloud Storage 存储桶下的Picture(类似文件夹)内上传了多张图片。
例如。 Picture/a.jpg, Picture/b.jpg, Pictuer/c.jpg, .....
现在,我想将 Cloud Storage 中的这些多张图片作为列表直接显示到我的 cakephp 2.x Web 应用程序中。
根据 Google Cloud Storage 文档,要访问存储桶内的每个对象,必须生成 Signed URLs。 因此,我根据从数据库中选择的数据为每个对象创建了签名 URL。以下是我的示例代码。
<?php
# Imports the Google Cloud client library
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\Exception\GoogleException;
function index() {
//$rsl = 'select data from database';
for($i=0; $i<$count; $i++) {
# create url to access image from google cloud storage
$file_path = $rsl[$i]['pic']['file_path'];
if(!empty($file_path)) {
$rsl[$i]['pic']['real_path'] = $this->get_object_v4_signed_url($file_path);
} else {
$rsl[$i]['pic']['real_path'] = '';
}
}
}
/**
* Generate a v4 signed URL for downloading an object.
*
* @param string $bucketName the name of your Google Cloud bucket.
* @param string $objectName the name of your Google Cloud object.
*
* @return void
*/
function get_object_v4_signed_url($objectName) {
$cloud = parent::connect_to_google_cloud_storage();
$storage = $cloud[0];
$bucketName = $cloud[1];
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
if($object->exists()) {
$url = $object->signedUrl(
# This URL is valid for 1 minutes
new \DateTime('1 min'),
[
'version' => 'v4'
]
);
} else {
$url = '';
}
return $url;
}
?>
问题是,生成时间太长,因为每个文件都生成为 v4 签名的 URL。当我阅读 Google Cloud Storage 文档时,我没有看到一次生成多个对象的 v4 签名 URL(可能是我错了)。那么,有没有办法加快这个生成过程呢?
【问题讨论】: