Skip to content

Commit 0ba77af

Browse files
authored
more actions for storage (GoogleCloudPlatform#130)
1 parent 37beaa0 commit 0ba77af

File tree

6 files changed

+259
-1
lines changed

6 files changed

+259
-1
lines changed

storage/api/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ This simple command-line application demonstrates how to invoke Google Cloud Sto
2929
my-project-name-bucket2
3030
my-project-name-bucket3
3131
```
32+
6. Run `php listObjects.php YOUR_PROJECT_NAME YOUR_BUCKET_NAME` where YOUR_PROJECT_NAME is the
33+
project associated with the credentials from **step 2** and YOUR_BUCKET_NAME is a bucket from the list of bucket names in **step 5**.
34+
35+
```sh
36+
$ php listBuckets.php my-project-name my-project-name-bucket1
37+
my-project-name-bucket1-object1
38+
my-project-name-bucket1-object2
39+
my-project-name-bucket1-object3
40+
```
41+
7. Run `php listObjects.php YOUR_PROJECT_NAME YOUR_BUCKET_NAME YOUR_OBJECT_NAME` where YOUR_PROJECT_NAME is the project associated with the credentials from **step 2** and YOUR_BUCKET_NAME is a bucket from the list of bucket names in **step 5** and YOUR_OBJECT_NAME is an object name from the list of objects in **step 6**.
42+
43+
```sh
44+
$ php downloadObject.php my-project-name my-project-name-bucket1 my-project-name-bucket1-object1 myfile.txt
45+
File written to myfile.txt
46+
```
3247

3348

3449
## Contributing changes

storage/api/downloadObject.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2015 Google Inc. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// [START all]
20+
// composer autoloading
21+
require_once __DIR__ . '/vendor/autoload.php';
22+
23+
// grab the first argument
24+
if (empty($argv[3])) {
25+
die("usage: php downloadObject [project_id] [bucket_name] [object_name]"
26+
. " [optional_download_name]\n");
27+
}
28+
29+
$projectId = $argv[1];
30+
$bucketName = $argv[2];
31+
$objectName = $argv[3];
32+
$downloadName = isset($argv[4]) ? $argv[4] : $argv[3];
33+
34+
// Authenticate your API Client
35+
$client = new Google_Client();
36+
$client->useApplicationDefaultCredentials();
37+
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
38+
39+
$storage = new Google_Service_Storage($client);
40+
41+
try {
42+
// Google Cloud Storage API request to retrieve the list of objects in your
43+
// project.
44+
$object = $storage->objects->get($bucketName, $objectName);
45+
} catch (Google_Service_Exception $e) {
46+
// The bucket doesn't exist!
47+
if ($e->getCode() == 404) {
48+
exit(sprintf("Invalid bucket or object names (\"%s\", \"%s\")\n",
49+
$bucketName, $objectName));
50+
}
51+
throw $e;
52+
}
53+
54+
// build the download URL
55+
$uri = sprintf('https://storage.googleapis.com/%s/%s?alt=media&generation=%s',
56+
$bucketName, $objectName, $object->generation);
57+
$http = $client->authorize();
58+
$response = $http->get($uri);
59+
60+
if ($response->getStatusCode() != 200) {
61+
exit('download failed!' . $response->getBody());
62+
}
63+
if (file_exists($downloadName)) {
64+
exit(sprintf("File \"%s\" exists! Cowardly refusing to overwrite it\n",
65+
$downloadName));
66+
}
67+
68+
// write the file
69+
file_put_contents($downloadName, $response->getBody());
70+
echo sprintf("File written to %s\n", $downloadName);
71+
72+
// [END all]

storage/api/listObjects.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2015 Google Inc. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// [START all]
20+
// composer autoloading
21+
require_once __DIR__ . '/vendor/autoload.php';
22+
23+
// grab the first argument
24+
if (empty($argv[2])) {
25+
die("usage: php listBuckets [project_id] [bucket_name]\n");
26+
}
27+
28+
$projectId = $argv[1];
29+
$bucketName = $argv[2];
30+
31+
// Authenticate your API Client
32+
$client = new Google_Client();
33+
$client->useApplicationDefaultCredentials();
34+
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);
35+
36+
$storage = new Google_Service_Storage($client);
37+
38+
try {
39+
// Google Cloud Storage API request to retrieve the list of objects in your project.
40+
$objects = $storage->objects->listObjects($bucketName);
41+
} catch (Google_Service_Exception $e) {
42+
// The bucket doesn't exist!
43+
if ($e->getCode() == 404) {
44+
exit(sprintf("Invalid bucket name \"%s\"\n", $bucketName));
45+
}
46+
throw $e;
47+
}
48+
49+
foreach ($objects['items'] as $object) {
50+
printf("%s\n", $object->getName());
51+
}
52+
// [END all]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
class downloadObjectTest extends PHPUnit_Framework_TestCase
18+
{
19+
protected static $hasCredentials;
20+
21+
public static function setUpBeforeClass()
22+
{
23+
$path = getenv('GOOGLE_APPLICATION_CREDENTIALS');
24+
self::$hasCredentials = $path && file_exists($path) &&
25+
filesize($path) > 0;
26+
}
27+
28+
public function test()
29+
{
30+
if (!self::$hasCredentials) {
31+
$this->markTestSkipped('No application credentials were found.');
32+
}
33+
34+
if (!$bucketName = getenv('GOOGLE_BUCKET_NAME')) {
35+
$this->markTestSkipped('GOOGLE_BUCKET_NAME must be set.');
36+
}
37+
38+
if (!$objectName = getenv('GOOGLE_OBJECT_NAME')) {
39+
$this->markTestSkipped('GOOGLE_OBJECT_NAME must be set.');
40+
}
41+
42+
// Invoke listBuckets.php, and fake first argument.
43+
global $argc, $argv;
44+
$argv[1] = getenv('GOOGLE_PROJECT_ID');
45+
$argv[2] = $bucketName;
46+
$argv[3] = $objectName;
47+
$argv[4] = sprintf('%s/%s-%s', sys_get_temp_dir(), rand(), $objectName);
48+
49+
// Capture stdout.
50+
ob_start();
51+
include __DIR__ . '/../downloadObject.php';
52+
$result = ob_get_contents();
53+
ob_end_clean();
54+
55+
// Make sure it looks correct
56+
$this->assertContains('File written to ' . $argv[4], $result);
57+
$this->assertTrue(file_exists($argv[4]));
58+
}
59+
}

storage/api/test/listBucketsTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function test()
3131
$this->markTestSkipped('No application credentials were found.');
3232
}
3333

34+
if (!$bucketName = getenv('GOOGLE_BUCKET_NAME')) {
35+
$this->markTestSkipped('GOOGLE_BUCKET_NAME must be set.');
36+
}
37+
3438
// Invoke listBuckets.php, and fake first argument.
3539
global $argc, $argv;
3640
$argv[1] = getenv('GOOGLE_PROJECT_ID');
@@ -42,6 +46,6 @@ public function test()
4246
ob_end_clean();
4347

4448
// Make sure it looks correct
45-
$this->assertContains('cloud-samples-tests-php-bucket', $result);
49+
$this->assertContains($bucketName, $result);
4650
}
4751
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
class listObjectsTest extends PHPUnit_Framework_TestCase
18+
{
19+
protected static $hasCredentials;
20+
21+
public static function setUpBeforeClass()
22+
{
23+
$path = getenv('GOOGLE_APPLICATION_CREDENTIALS');
24+
self::$hasCredentials = $path && file_exists($path) &&
25+
filesize($path) > 0;
26+
}
27+
28+
public function test()
29+
{
30+
if (!self::$hasCredentials) {
31+
$this->markTestSkipped('No application credentials were found.');
32+
}
33+
34+
if (!$bucketName = getenv('GOOGLE_BUCKET_NAME')) {
35+
$this->markTestSkipped('GOOGLE_BUCKET_NAME must be set.');
36+
}
37+
38+
if (!$objectName = getenv('GOOGLE_OBJECT_NAME')) {
39+
$this->markTestSkipped('GOOGLE_OBJECT_NAME must be set.');
40+
}
41+
42+
// Invoke listBuckets.php, and fake first argument.
43+
global $argc, $argv;
44+
$argv[1] = getenv('GOOGLE_PROJECT_ID');
45+
$argv[2] = $bucketName;
46+
47+
// Capture stdout.
48+
ob_start();
49+
include __DIR__ . '/../listObjects.php';
50+
$result = ob_get_contents();
51+
ob_end_clean();
52+
53+
// Make sure it looks correct
54+
$this->assertContains($objectName, $result);
55+
}
56+
}

0 commit comments

Comments
 (0)