Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added downloadAsStream method to StorageObject #199

Merged
merged 2 commits into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/Storage/StorageObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
);

Expand All @@ -588,6 +576,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.
Expand Down
55 changes: 55 additions & 0 deletions tests/Storage/StorageObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 = [
Expand Down