Skip to content

Commit 882adac

Browse files
jkwluibshaffer
authored andcommitted
Adds Storage v2 signed url sample (GoogleCloudPlatform#887)
1 parent 39eda6a commit 882adac

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

storage/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"src/get_bucket_policy_only.php",
3838
"src/get_object_acl.php",
3939
"src/get_object_acl_for_entity.php",
40+
"src/get_object_v2_signed_url.php",
4041
"src/get_requester_pays_status.php",
4142
"src/get_retention_policy.php",
4243
"src/get_default_event_based_hold.php",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Copyright 2019 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_generate_signed_url]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Generate a v2 signed URL for downloading an object.
31+
*
32+
* @param string $bucketName the name of your Google Cloud bucket.
33+
* @param string $objectName the name of your Google Cloud object.
34+
*
35+
* @return void
36+
*/
37+
function get_object_v2_signed_url($bucketName, $objectName)
38+
{
39+
$storage = new StorageClient();
40+
$bucket = $storage->bucket($bucketName);
41+
$object = $bucket->object($objectName);
42+
# This URL is valid for 1 hour
43+
$url = $object->signedUrl(new \DateTime('next hour'));
44+
45+
printf('The signed url for %s is %s\n', $objectName, $url);
46+
}
47+
# [END storage_generate_signed_url]

storage/storage.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@
458458
)
459459
->addArgument('project', InputArgument::REQUIRED, 'Your billable Google Cloud Project ID')
460460
->addArgument('bucket', InputArgument::REQUIRED, 'The Cloud Storage bucket name')
461-
->addArgument('object', InputArgument::REQUIRED, 'The Cloud Storage bucket name')
461+
->addArgument('object', InputArgument::REQUIRED, 'The Cloud Storage object name')
462462
->addArgument('upload-from', InputArgument::REQUIRED, 'Path to the file to upload')
463463
->addArgument('kms-key-name', InputArgument::REQUIRED, 'KMS key ID used to encrypt objects server side.')
464464
->setCode(function ($input, $output) {
@@ -470,6 +470,23 @@
470470
upload_with_kms_key($projectId, $bucketName, $objectName, $uploadFrom, $kmsKeyName);
471471
});
472472

473+
$application->add(new Command('get-object-v2-signed-url'))
474+
->setDescription('Generate a v2 signed URL for downloading an object.')
475+
->setHelp(<<
476+
The %command.name% command generates a v2 signed URL for downloading an object.
477+
478+
php %command.full_name% --help
479+
480+
EOF
481+
)
482+
->addArgument('bucket', InputArgument::REQUIRED, 'The Cloud Storage bucket name')
483+
->addArgument('object', InputArgument::REQUIRED, 'The Cloud Storage object name')
484+
->setCode(function ($input, $output) {
485+
$bucketName = $input->getArgument('bucket');
486+
$objectName = $input->getArgument('object');
487+
get_object_v2_signed_url($bucketName, $objectName);
488+
});
489+
473490
// for testing
474491
if (getenv('PHPUNIT_TESTS') === '1') {
475492
return $application;

storage/test/ObjectSignedUrlTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* Copyright 2019 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Storage\Tests;
19+
20+
use Google\Cloud\TestUtils\TestTrait;
21+
use Google\Cloud\TestUtils\ExecuteCommandTrait;
22+
use Google\Cloud\Storage\StorageClient;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Unit Tests for RequesterPaysCommand.
27+
*/
28+
class ObjectSignedUrlTest extends TestCase
29+
{
30+
use TestTrait;
31+
use ExecuteCommandTrait;
32+
33+
private static $bucketName;
34+
private static $objectName;
35+
private static $commandFile = __DIR__ . '/../storage.php';
36+
37+
/** @beforeClass */
38+
public static function setUpObject()
39+
{
40+
$storage = new StorageClient();
41+
self::$bucketName = self::requireEnv('GOOGLE_STORAGE_BUCKET');
42+
self::$objectName = sprintf('test-object-%s', time());
43+
$storage
44+
->bucket(self::$bucketName)
45+
->upload("test file content", [
46+
'name' => self::$objectName
47+
]);
48+
}
49+
50+
public function testGetV2SignedUrl()
51+
{
52+
$output = $this->runCommand('get-object-v2-signed-url', [
53+
'bucket' => self::$bucketName,
54+
'object' => self::$objectName,
55+
]);
56+
57+
$this->assertContains("The signed url for " . self::$objectName . " is", $output);
58+
}
59+
}

0 commit comments

Comments
 (0)