2

I'm upload multiple images inside Picture(like folder) under Google Cloud Storage bucket.

Eg. Picture/a.jpg, Picture/b.jpg, Pictuer/c.jpg, .....

Now, I want to directly show those multiple images from Cloud Storage into my cakephp 2.x web application as a list.

According to Google Cloud Storage documentation, to access each object inside bucket, Signed URLs must be generated. So, I created signed URLs for each object based on my selecting data from database. Following is my sample code.

<?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;
}
?>

The problem is, it takes too long to generate because each file is generated to v4 signed URL. When I read Google Cloud Storage documentation, I didn't see to generate v4 signed URL of multiple objects at once(may be I'm wrong). So, is there anyway to speed it up this generating process?

Miguel
  • 956
  • 6
  • 20
Cloud
  • 1,004
  • 1
  • 18
  • 47

1 Answers1

2

As you mention there is no explanation in the Google Cloud Storage documentation on how to generate signed URLs for multiple objects using PHP as you do. However, I have found that using the gsutil signurl command, you can specify multiple paths or even use a wildcard:

gsutil signurl -d 1m ~/sandbox/key.json gs://bucket_name/object_1.jpg gs://bucket_name/object_2.jpg ...

gsutil signurl -d 1m ~/sandbox/key.json gs://bucket_name/*

This is specified in the gsutil help signurl page: "Multiple gs:// urls may be provided and may contain wildcards. A signed url will be produced for each provided url, authorized for the specified HTTP method and valid for the given duration."

Another option could be using multi-threading in your APP, here you'll find how to perform multi-threading using the pthreads API. If you choose to use this API you should bear in mind (from the documentation):

Warning The pthreads extension cannot be used in a web server environment. Threading in PHP is therefore restricted to CLI-based applications only.

Warning pthreads (v3) can only be used with PHP 7.2+: This is due to ZTS mode being unsafe in 7.0 and 7.1.

But since you have a web APP, I think that neither of those solutions would be useful for you. Besides that you could try using Cloud Functions to generate the signed URLs.

Miguel
  • 956
  • 6
  • 20