Skip to content

Commit e375441

Browse files
authored
adds list objects with prefix sample to storage (GoogleCloudPlatform#242)
1 parent 360f0e3 commit e375441

File tree

5 files changed

+158
-22
lines changed

5 files changed

+158
-22
lines changed

storage/api/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"src/functions/get_object_acl_for_entity.php",
3232
"src/functions/list_buckets.php",
3333
"src/functions/list_objects.php",
34+
"src/functions/list_objects_with_prefix.php",
3435
"src/functions/make_public.php",
3536
"src/functions/move_object.php",
3637
"src/functions/rotate_encryption_key.php",

storage/api/composer.lock

Lines changed: 79 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/api/src/ObjectsCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ protected function configure()
8888
InputOption::VALUE_NONE,
8989
'Delete the bucket'
9090
)
91+
->addOption(
92+
'prefix',
93+
null,
94+
InputOption::VALUE_REQUIRED,
95+
'List objects matching a prefix'
96+
)
9197
;
9298
}
9399

@@ -111,7 +117,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
111117
object_metadata($bucketName, $objectName);
112118
}
113119
} else {
114-
list_objects($bucketName);
120+
if ($prefix = $input->getOption('prefix')) {
121+
list_objects_with_prefix($bucketName, $prefix);
122+
} else {
123+
list_objects($bucketName);
124+
}
115125
}
116126
}
117127
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Google Inc. All Rights Reserved.
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/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START list_objects_with_prefix]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Add ACL to a Cloud Storage Bucket.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*
34+
* @return void
35+
*/
36+
function list_objects_with_prefix($bucketName, $prefix)
37+
{
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
$options = ['prefix' => $prefix];
41+
foreach ($bucket->objects($options) as $object) {
42+
printf('Object: %s' . PHP_EOL, $object->name());
43+
}
44+
}
45+
# [END list_objects_with_prefix]

storage/api/test/ObjectsCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ public function testListObjects()
6565
$this->expectOutputRegex("/Object:/");
6666
}
6767

68+
public function testListObjectsWithPrefix()
69+
{
70+
if (!self::$hasCredentials) {
71+
$this->markTestSkipped('No application credentials were found.');
72+
}
73+
if (!$bucketName = getenv('GOOGLE_STORAGE_BUCKET')) {
74+
$this->markTestSkipped('No storage bucket name.');
75+
}
76+
77+
ob_start();
78+
$this->commandTester->execute(
79+
[
80+
'bucket' => $bucketName,
81+
'--prefix' => 'test_data.csv'
82+
],
83+
['interactive' => false]
84+
);
85+
$output = ob_get_clean();
86+
87+
$this->assertEquals(1, substr_count($output, 'Object: '));
88+
}
89+
6890
public function testManageObject()
6991
{
7092
if (!self::$hasCredentials) {

0 commit comments

Comments
 (0)