From 444939870658efe8300640a9802fa170fff3b492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gediminas=20=C5=A0edbaras?= Date: Sun, 9 Oct 2016 20:29:44 +0200 Subject: [PATCH 1/2] added downloadAsStream method to StorageObject --- src/Storage/StorageObject.php | 34 ++++++++++++++++++ tests/Storage/StorageObjectTest.php | 55 +++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/Storage/StorageObject.php b/src/Storage/StorageObject.php index 20c4d0553216..0ce3e9f6c6c1 100644 --- a/src/Storage/StorageObject.php +++ b/src/Storage/StorageObject.php @@ -588,6 +588,40 @@ public function downloadToFile($path, array $options = []) return $destination; } + /** + * Download an object as a stream. + * + * Example: + * ``` + * $stream = $object->downloadAsStream(); + * echo $stream->getContents(); + * ``` + * + * @param array $options [optional] { + * Configuration Options. + * + * @type string $encryptionKey An AES-256 customer-supplied encryption + * key. It will be neccesary to provide this when a key was used + * during the object's creation. If provided one must also include + * an `encryptionKeySHA256`. + * @type string $encryptionKeySHA256 The SHA256 hash of the + * customer-supplied encryption key. It will be neccesary to + * provide this when a key was used during the object's creation. + * If provided one must also include an `encryptionKey`. + * } + * @return StreamInterface + */ + public function downloadAsStream(array $options = []) + { + return $this->connection->downloadObject( + $this->formatEncryptionHeaders( + $options + + $this->encryptionData + + $this->identity + ) + ); + } + /** * Retrieves the object's details. If no object data is cached a network * request will be made to retrieve it. diff --git a/tests/Storage/StorageObjectTest.php b/tests/Storage/StorageObjectTest.php index 62be20a13cad..b932e69055de 100644 --- a/tests/Storage/StorageObjectTest.php +++ b/tests/Storage/StorageObjectTest.php @@ -23,12 +23,15 @@ use Google\Cloud\Storage\StorageObject; use GuzzleHttp\Psr7; use Prophecy\Argument; +use Prophecy\Prophecy\ObjectProphecy; +use Psr\Http\Message\StreamInterface; /** * @group storage */ class StorageObjectTest extends \PHPUnit_Framework_TestCase { + /** @var ConnectionInterface|ObjectProphecy */ public $connection; public function setUp() @@ -366,6 +369,58 @@ public function testDownloadsToFile() ); } + public function testGetBodyWithoutExtraOptions() + { + $bucket = 'bucket'; + $object = 'object.txt'; + $stream = Psr7\stream_for($string = 'abcdefg'); + $this->connection->downloadObject([ + 'bucket' => $bucket, + 'object' => $object, + 'generation' => null, + ]) + ->willReturn($stream); + + $object = new StorageObject($this->connection->reveal(), $object, $bucket); + + $body = $object->downloadAsStream(); + + $this->assertInstanceOf(StreamInterface::class, $body); + $this->assertEquals($string, $body); + } + + public function testGetBodyWithExtraOptions() + { + $key = 'abcd'; + $hash = '1234'; + $bucket = 'bucket'; + $object = 'object.txt'; + $stream = Psr7\stream_for($string = 'abcdefg'); + $this->connection->downloadObject([ + 'bucket' => $bucket, + 'object' => $object, + 'generation' => null, + 'httpOptions' => [ + 'headers' => [ + 'x-goog-encryption-algorithm' => 'AES256', + 'x-goog-encryption-key' => base64_encode($key), + 'x-goog-encryption-key-sha256' => base64_encode($hash), + ] + ] + ]) + ->willReturn($stream); + + $object = new StorageObject($this->connection->reveal(), $object, $bucket); + + $body = $object->downloadAsStream([ + 'encryptionKey' => $key, + 'encryptionKeySHA256' => $hash + ]); + + $this->assertInstanceOf(StreamInterface::class, $body); + $this->assertEquals($string, $body); + } + public function testGetsInfo() { $objectInfo = [ From 98e6fd1f4e8a503ebff7b6a9acc35717fb6bc750 Mon Sep 17 00:00:00 2001 From: gediminas Date: Tue, 11 Oct 2016 15:20:01 +0200 Subject: [PATCH 2/2] reusing downloadAsStream --- src/Storage/StorageObject.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Storage/StorageObject.php b/src/Storage/StorageObject.php index 0ce3e9f6c6c1..45ed1b90bd1f 100644 --- a/src/Storage/StorageObject.php +++ b/src/Storage/StorageObject.php @@ -536,13 +536,7 @@ public function rename($name, array $options = []) */ public function downloadAsString(array $options = []) { - return (string) $this->connection->downloadObject( - $this->formatEncryptionHeaders( - $options - + $this->encryptionData - + $this->identity - ) - ); + return (string) $this->downloadAsStream($options); } /** @@ -573,13 +567,7 @@ public function downloadToFile($path, array $options = []) $destination = Psr7\stream_for(fopen($path, 'w')); Psr7\copy_to_stream( - $this->connection->downloadObject( - $this->formatEncryptionHeaders( - $options - + $this->encryptionData - + $this->identity - ) - ), + $this->downloadAsStream($options), $destination );