Skip to content

Commit 56329e3

Browse files
feat: [Storage] complex upload download samples (GoogleCloudPlatform#1606)
1 parent 808d61f commit 56329e3

File tree

6 files changed

+265
-2
lines changed

6 files changed

+265
-2
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
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_download_byte_range]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Download a byte range from Cloud Storage and save it as a local file.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $objectName The name of your Cloud Storage object.
34+
* @param int $startByte The starting byte at which to begin the download.
35+
* @param int $endByte The ending byte at which to end the download.
36+
* @param string $destination The local destination to save the object.
37+
*/
38+
function download_byte_range(
39+
string $bucketName,
40+
string $objectName,
41+
int $startByte,
42+
int $endByte,
43+
string $destination
44+
): void {
45+
// $bucketName = 'my-bucket';
46+
// $objectName = 'my-object';
47+
// $startByte = 1;
48+
// $endByte = 5;
49+
// $destination = '/path/to/your/file';
50+
51+
$storage = new StorageClient();
52+
$bucket = $storage->bucket($bucketName);
53+
$object = $bucket->object($objectName);
54+
$object->downloadToFile($destination, [
55+
'restOptions' => [
56+
'headers' => [
57+
'Range' => "bytes=$startByte-$endByte",
58+
],
59+
],
60+
]);
61+
printf(
62+
'Downloaded gs://%s/%s to %s' . PHP_EOL,
63+
$bucketName,
64+
$objectName,
65+
basename($destination)
66+
);
67+
}
68+
# [END storage_download_byte_range]
69+
70+
// The following 2 lines are only needed to run the samples
71+
require_once __DIR__ . '/../../testing/sample_helpers.php';
72+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/src/download_object.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace Google\Cloud\Samples\Storage;
2525

2626
# [START storage_download_file]
27+
# [START storage_stream_file_download]
2728
use Google\Cloud\Storage\StorageClient;
2829

2930
/**
@@ -43,9 +44,14 @@ function download_object($bucketName, $objectName, $destination)
4344
$bucket = $storage->bucket($bucketName);
4445
$object = $bucket->object($objectName);
4546
$object->downloadToFile($destination);
46-
printf('Downloaded gs://%s/%s to %s' . PHP_EOL,
47-
$bucketName, $objectName, basename($destination));
47+
printf(
48+
'Downloaded gs://%s/%s to %s' . PHP_EOL,
49+
$bucketName,
50+
$objectName,
51+
basename($destination)
52+
);
4853
}
54+
# [END storage_stream_file_download]
4955
# [END storage_download_file]
5056

5157
// The following 2 lines are only needed to run the samples
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
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_file_download_into_memory]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Download an object from Cloud Storage and save into a buffer in memory.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $objectName The name of your Cloud Storage object.
34+
*/
35+
function download_object_into_memory(
36+
string $bucketName,
37+
string $objectName
38+
): void {
39+
// $bucketName = 'my-bucket';
40+
// $objectName = 'my-object';
41+
42+
$storage = new StorageClient();
43+
$bucket = $storage->bucket($bucketName);
44+
$object = $bucket->object($objectName);
45+
$contents = $object->downloadAsString();
46+
printf(
47+
'Downloaded %s from gs://%s/%s' . PHP_EOL,
48+
$contents,
49+
$bucketName,
50+
$objectName
51+
);
52+
}
53+
# [END storage_file_download_into_memory]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/src/upload_object.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
namespace Google\Cloud\Samples\Storage;
2525

2626
# [START storage_upload_file]
27+
# [START storage_stream_file_upload]
2728
use Google\Cloud\Storage\StorageClient;
2829

2930
/**
@@ -47,6 +48,7 @@ function upload_object($bucketName, $objectName, $source)
4748
]);
4849
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
4950
}
51+
# [END storage_stream_file_upload]
5052
# [END storage_upload_file]
5153

5254
// The following 2 lines are only needed to run the samples
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
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_file_upload_from_memory]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Upload an object from memory buffer.
31+
*
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $objectName The name of your Cloud Storage object.
34+
* @param string $contents The contents to upload to the file.
35+
*/
36+
function upload_object_from_memory(
37+
string $bucketName,
38+
string $objectName,
39+
string $contents
40+
): void {
41+
// $bucketName = 'my-bucket';
42+
// $objectName = 'my-object';
43+
// $contents = 'these are my contents';
44+
45+
$storage = new StorageClient();
46+
$stream = fopen('data://text/plain,' . $contents, 'r');
47+
$bucket = $storage->bucket($bucketName);
48+
$bucket->upload($stream, [
49+
'name' => $objectName,
50+
]);
51+
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, $contents, $bucketName, $objectName);
52+
}
53+
# [END storage_file_upload_from_memory]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/ObjectsTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,75 @@ public function testCompose()
180180
$bucket->object($targetName)->delete();
181181
}
182182

183+
public function testUploadAndDownloadObjectFromMemory()
184+
{
185+
$objectName = 'test-object-' . time();
186+
$bucket = self::$storage->bucket(self::$bucketName);
187+
$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
188+
$object = $bucket->object($objectName);
189+
190+
$this->assertFalse($object->exists());
191+
192+
$output = self::runFunctionSnippet('upload_object_from_memory', [
193+
self::$bucketName,
194+
$objectName,
195+
$contents,
196+
]);
197+
198+
$object->reload();
199+
$this->assertTrue($object->exists());
200+
201+
$output = self::runFunctionSnippet('download_object_into_memory', [
202+
self::$bucketName,
203+
$objectName
204+
]);
205+
$this->assertStringContainsString($contents, $output);
206+
}
207+
208+
public function testDownloadByteRange()
209+
{
210+
$objectName = 'test-object-download-byte-range-' . time();
211+
$bucket = self::$storage->bucket(self::$bucketName);
212+
$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
213+
$object = $bucket->object($objectName);
214+
$downloadTo = tempnam(sys_get_temp_dir(), '/tests');
215+
$downloadToBasename = basename($downloadTo);
216+
$startPos = 1;
217+
$endPos = strlen($contents) - 2;
218+
219+
$this->assertFalse($object->exists());
220+
221+
$output = self::runFunctionSnippet('upload_object_from_memory', [
222+
self::$bucketName,
223+
$objectName,
224+
$contents
225+
]);
226+
227+
$object->reload();
228+
$this->assertTrue($object->exists());
229+
230+
$output .= self::runFunctionSnippet('download_byte_range', [
231+
self::$bucketName,
232+
$objectName,
233+
$startPos,
234+
$endPos,
235+
$downloadTo,
236+
]);
237+
238+
$this->assertTrue(file_exists($downloadTo));
239+
$expectedContents = substr($contents, $startPos, $endPos - $startPos + 1);
240+
$this->assertEquals($expectedContents, file_get_contents($downloadTo));
241+
$this->assertStringContainsString(
242+
sprintf(
243+
'Downloaded gs://%s/%s to %s',
244+
self::$bucketName,
245+
$objectName,
246+
$downloadToBasename,
247+
),
248+
$output
249+
);
250+
}
251+
183252
public function testChangeStorageClass()
184253
{
185254
$objectName = uniqid('change-storage-class-');

0 commit comments

Comments
 (0)