Skip to content

Commit 24321dd

Browse files
authored
storage: add v4 post policy (GoogleCloudPlatform#1049)
1 parent d04c9f8 commit 24321dd

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

storage/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"src/enable_requester_pays.php",
3535
"src/activate_hmac_key.php",
3636
"src/generate_encryption_key.php",
37+
"src/generate_v4_post_policy.php",
3738
"src/bucket_metadata.php",
3839
"src/get_bucket_acl.php",
3940
"src/get_bucket_acl_for_entity.php",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
/**
3+
* Copyright 2020 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_post_policy_v4]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Generates a V4 POST Policy to be used in an HTML form and echo's form.
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 generate_v4_post_policy($bucketName, $objectName)
38+
{
39+
$storage = new StorageClient();
40+
$bucket = $storage->bucket($bucketName);
41+
42+
$response = $bucket->generateSignedPostPolicyV4(
43+
$objectName,
44+
new \DateTime('10 min'),
45+
[
46+
'fields' => [
47+
'x-goog-meta-test' => 'data'
48+
]
49+
]
50+
);
51+
52+
$url = $response['url'];
53+
$output = "
" . PHP_EOL;
54+
foreach ($response['fields'] as $name => $value) {
55+
$output .= " " . PHP_EOL;
56+
}
57+
$output .= "
"
. PHP_EOL;
58+
$output .= "
"
. PHP_EOL;
59+
$output .= "" . PHP_EOL;
60+
61+
echo $output;
62+
}
63+
# [END storage_generate_signed_post_policy_v4]

storage/storage.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,23 @@
618618
upload_object_v4_signed_url($bucketName, $objectName);
619619
});
620620

621+
$application->add(new Command('generate-v4-post-policy'))
622+
->setDescription('Generate a v4 post policy form for uploading an object.')
623+
->setHelp(<<
624+
The %command.name% command generates a v4 post policy form for uploading an object.
625+
626+
php %command.full_name% --help
627+
628+
EOF
629+
)
630+
->addArgument('bucket', InputArgument::REQUIRED, 'The Cloud Storage bucket name')
631+
->addArgument('object', InputArgument::REQUIRED, 'The Cloud Storage object name')
632+
->setCode(function ($input, $output) {
633+
$bucketName = $input->getArgument('bucket');
634+
$objectName = $input->getArgument('object');
635+
generate_v4_post_policy($bucketName, $objectName);
636+
});
637+
621638
// for testing
622639
if (getenv('PHPUNIT_TESTS') === '1') {
623640
return $application;

storage/test/GenerateV4PostPolicy.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/**
3+
* Copyright 2020 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 GenerateV4PostPolicy.
27+
*/
28+
class GenerateV4PostPolicy extends TestCase
29+
{
30+
use TestTrait;
31+
use ExecuteCommandTrait;
32+
33+
private static $storage;
34+
private static $bucketName;
35+
private static $objectName;
36+
private static $commandFile = __DIR__ . '/../storage.php';
37+
38+
/** @beforeClass */
39+
public static function setUpObject()
40+
{
41+
self::$storage = new StorageClient();
42+
self::$bucketName = self::requireEnv('GOOGLE_STORAGE_BUCKET');
43+
self::$objectName = sprintf('test-object-%s', time());
44+
}
45+
46+
public function testGenerateSignedPostPolicy()
47+
{
48+
$bucketName = self::$bucketName;
49+
$objectName = self::$objectName;
50+
$output = $this->runCommand('generate-v4-post-policy', [
51+
'bucket' => $bucketName,
52+
'object' => $objectName,
53+
]);
54+
55+
$this->assertContains("
", $output);
57+
$this->assertContains("", $output);
58+
$this->assertContains("", $output);
59+
$this->assertContains("", $output);
60+
$this->assertContains("", $output);
61+
$this->assertContains("", $output);
62+
$this->assertContains("", $output);
63+
$this->assertContains("", $output);
64+
}
65+
}

storage/test/ObjectSignedUrlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use PHPUnit\Framework\TestCase;
2525

2626
/**
27-
* Unit Tests for RequesterPaysCommand.
27+
* Unit Tests for ObjectSignedUrl.
2828
*/
2929
class ObjectSignedUrlTest extends TestCase
3030
{

0 commit comments

Comments
 (0)