Skip to content

Commit 073a6cc

Browse files
feat: new sample for storage upload chunked stream (GoogleCloudPlatform#1653)
1 parent b08bc07 commit 073a6cc

File tree

3 files changed

+95
-8
lines changed

3 files changed

+95
-8
lines changed

storage/src/upload_object.php

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

2626
# [START storage_upload_file]
27-
# [START storage_stream_file_upload]
2827
use Google\Cloud\Storage\StorageClient;
2928

3029
/**
@@ -48,7 +47,6 @@ function upload_object($bucketName, $objectName, $source)
4847
]);
4948
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
5049
}
51-
# [END storage_stream_file_upload]
5250
# [END storage_upload_file]
5351

5452
// The following 2 lines are only needed to run the samples
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_stream_file_upload]
27+
use Google\Cloud\Storage\StorageClient;
28+
use Google\Cloud\Storage\WriteStream;
29+
30+
/**
31+
* Upload a chunked file stream.
32+
*
33+
* @param string $bucketName The name of your Cloud Storage bucket.
34+
* @param string $objectName The name of your Cloud Storage object.
35+
* @param string $contents The contents to upload via stream chunks.
36+
*/
37+
function upload_object_stream(string $bucketName, string $objectName, string $contents): void
38+
{
39+
// $bucketName = 'my-bucket';
40+
// $objectName = 'my-object';
41+
// $contents = 'these are my contents';
42+
43+
$storage = new StorageClient();
44+
$bucket = $storage->bucket($bucketName);
45+
$writeStream = new WriteStream(null, [
46+
'chunkSize' => 1024 * 256, // 256KB
47+
]);
48+
$uploader = $bucket->getStreamableUploader($writeStream, [
49+
'name' => $objectName,
50+
]);
51+
$writeStream->setUploader($uploader);
52+
$stream = fopen('data://text/plain,' . $contents, 'r');
53+
while (($line = stream_get_line($stream, 1024 * 256)) !== false) {
54+
$writeStream->write($line);
55+
}
56+
$writeStream->close();
57+
58+
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, $contents, $bucketName, $objectName);
59+
}
60+
# [END storage_stream_file_upload]
61+
62+
// The following 2 lines are only needed to run the samples from the CLI
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/ObjectsTest.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ObjectsTest extends TestCase
3030

3131
private static $bucketName;
3232
private static $storage;
33+
private static $contents;
3334

3435
public static function setUpBeforeClass(): void
3536
{
@@ -38,6 +39,7 @@ public static function setUpBeforeClass(): void
3839
self::requireEnv('GOOGLE_STORAGE_BUCKET')
3940
);
4041
self::$storage = new StorageClient();
42+
self::$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
4143
}
4244

4345
public function testListObjects()
@@ -184,12 +186,36 @@ public function testUploadAndDownloadObjectFromMemory()
184186
{
185187
$objectName = 'test-object-' . time();
186188
$bucket = self::$storage->bucket(self::$bucketName);
187-
$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
188189
$object = $bucket->object($objectName);
189190

190191
$this->assertFalse($object->exists());
191192

192193
$output = self::runFunctionSnippet('upload_object_from_memory', [
194+
self::$bucketName,
195+
$objectName,
196+
self::$contents,
197+
]);
198+
199+
$object->reload();
200+
$this->assertTrue($object->exists());
201+
202+
$output = self::runFunctionSnippet('download_object_into_memory', [
203+
self::$bucketName,
204+
$objectName,
205+
]);
206+
$this->assertStringContainsString(self::$contents, $output);
207+
}
208+
209+
public function testUploadAndDownloadObjectStream()
210+
{
211+
$objectName = 'test-object-stream-' . time();
212+
// contents larger than atleast one chunk size
213+
$contents = str_repeat(self::$contents, 1024 * 10);
214+
$bucket = self::$storage->bucket(self::$bucketName);
215+
$object = $bucket->object($objectName);
216+
$this->assertFalse($object->exists());
217+
218+
$output = self::runFunctionSnippet('upload_object_stream', [
193219
self::$bucketName,
194220
$objectName,
195221
$contents,
@@ -200,7 +226,7 @@ public function testUploadAndDownloadObjectFromMemory()
200226

201227
$output = self::runFunctionSnippet('download_object_into_memory', [
202228
self::$bucketName,
203-
$objectName
229+
$objectName,
204230
]);
205231
$this->assertStringContainsString($contents, $output);
206232
}
@@ -209,19 +235,18 @@ public function testDownloadByteRange()
209235
{
210236
$objectName = 'test-object-download-byte-range-' . time();
211237
$bucket = self::$storage->bucket(self::$bucketName);
212-
$contents = ' !"#$%&\'()*,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
213238
$object = $bucket->object($objectName);
214239
$downloadTo = tempnam(sys_get_temp_dir(), '/tests');
215240
$downloadToBasename = basename($downloadTo);
216241
$startPos = 1;
217-
$endPos = strlen($contents) - 2;
242+
$endPos = strlen(self::$contents) - 2;
218243

219244
$this->assertFalse($object->exists());
220245

221246
$output = self::runFunctionSnippet('upload_object_from_memory', [
222247
self::$bucketName,
223248
$objectName,
224-
$contents
249+
self::$contents,
225250
]);
226251

227252
$object->reload();
@@ -236,7 +261,7 @@ public function testDownloadByteRange()
236261
]);
237262

238263
$this->assertTrue(file_exists($downloadTo));
239-
$expectedContents = substr($contents, $startPos, $endPos - $startPos + 1);
264+
$expectedContents = substr(self::$contents, $startPos, $endPos - $startPos + 1);
240265
$this->assertEquals($expectedContents, file_get_contents($downloadTo));
241266
$this->assertStringContainsString(
242267
sprintf(

0 commit comments

Comments
 (0)