Skip to content

Commit

Permalink
added gotBody method to StorageObject
Browse files Browse the repository at this point in the history
  • Loading branch information
gedimin45 committed Oct 9, 2016
1 parent 6be5570 commit 67bd021
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Storage/StorageObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,39 @@ public function downloadToFile($path, array $options = [])
return $destination;
}

/**
* Download an object to a specified location.
*
* Example:
* ```
* $object->getBody();
* ```
*
* @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 getBody(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->getBody();

$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->getBody([
'encryptionKey' => $key,
'encryptionKeySHA256' => $hash
]);

$this->assertInstanceOf(StreamInterface::class, $body);
$this->assertEquals($string, $body);
}

public function testGetsInfo()
{
$objectInfo = [
Expand Down

0 comments on commit 67bd021

Please sign in to comment.