Skip to content

Commit eaeb0fc

Browse files
committed
Merge pull request #546 from ycombinator/gh-545
Return array of DataObjects if such is requested.
2 parents e8c07d1 + 3ae1c70 commit eaeb0fc

File tree

4 files changed

+102
-9
lines changed

4 files changed

+102
-9
lines changed

docs/userguide/ObjectStore/USERGUIDE.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ $localFileName = '/path/to/local/php-elephant.jpg';
198198
$remoteFileName = 'php-elephant.jpg';
199199

200200
$fileData = fopen($localFileName, 'r');
201-
$container->uploadObject($remoteFileName, $fileData);
201+
$object = $container->uploadObject($remoteFileName, $fileData);
202+
/** @var $object OpenCloud\ObjectStore\Resource\DataObject **/
202203
```
203204
[ [Get the executable PHP script for this example](/samples/ObjectStore/upload-object.php) ]
204205

@@ -258,15 +259,18 @@ $objects = array(
258259
a
259260
);
260261

261-
$container->uploadObjects($objects);
262+
$responses = $container->uploadObjects($objects);
263+
foreach ($responses as $response) {
264+
/** @var $response \Guzzle\Http\Message\Response **/
265+
}
262266
```
263267
[ [Get the executable PHP script for this example](/samples/ObjectStore/upload-multiple-objects.php) ]
264268

265269
In the above example, the contents of two files present on the local filesystem are uploaded as objects to the container referenced by `$container`.
266270

267271
Instead of specifying the `path` key in an element of the `$objects` array, you can specify a `body` key whose value is a string or a stream representation.
268272

269-
Finally, you can pass headers as the second parameter to the `uploadObjects` method. These headers will be applied to every object that is uploaded.
273+
You can pass headers as the second parameter to the `uploadObjects` method. These headers will be applied to every object that is uploaded.
270274

271275
```
272276
$metadata = array('author' => 'Jane Doe');
@@ -281,6 +285,17 @@ $container->uploadObjects($objects, $allHeaders);
281285

282286
In the example above, every object referenced within the `$objects` array will be uploaded with the same metadata.
283287

288+
Finally, if you want the `uploadObjects` method to return an array of `OpenCloud\ObjectStore\Resource\DataObject` objects instead of an array of `Guzzle\Http\Message\Response` objects, you can specify a third parameter to the `uploadObjects` method:
289+
290+
```
291+
use OpenCloud\ObjectStore\Enum\ReturnType;
292+
293+
$dataObjects = $container->uploadObjects($objects, $allHeaders, ReturnType::DATA_OBJECT_ARRAY);
294+
foreach ($dataObjects as $dataObject) {
295+
/** @var $dataObject OpenCloud\ObjectStore\Resource\DataObject **/
296+
}
297+
```
298+
284299
### Large Objects
285300

286301
If you want to upload objects larger than 5GB in size, you must use a different upload process.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright 2012-2014 Rackspace US, 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+
namespace OpenCloud\ObjectStore\Enum;
19+
20+
/**
21+
* Enumerated types for return types
22+
*
23+
* @package OpenCloud\ObjectStore\Enum
24+
*/
25+
class ReturnType
26+
{
27+
const RESPONSE_ARRAY = 'RESPONSE_ARRAY';
28+
const DATA_OBJECT_ARRAY = 'DATA_OBJECT_ARRAY';
29+
}

lib/OpenCloud/ObjectStore/Resource/Container.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OpenCloud\ObjectStore\Exception\ObjectNotFoundException;
3131
use OpenCloud\ObjectStore\Upload\DirectorySync;
3232
use OpenCloud\ObjectStore\Upload\TransferBuilder;
33+
use OpenCloud\ObjectStore\Enum\ReturnType;
3334

3435
/**
3536
* A container is a storage compartment for your data and provides a way for you
@@ -471,11 +472,14 @@ public function uploadObject($name, $data, array $headers = array())
471472
* `path' Path to an existing file, OR
472473
* `body' Either a string or stream representation of the file contents to be uploaded.
473474
* @param array $headers Optional headers that will be sent with the request (useful for object metadata).
475+
* @param string $returnType One of OpenCloud\ObjectStore\Enum\ReturnType::RESPONSE_ARRAY (to return an array of
476+
* Guzzle\Http\Message\Response objects) or OpenCloud\ObjectStore\Enum\ReturnType::DATA_OBJECT_ARRAY
477+
* (to return an array of OpenCloud\ObjectStore\Resource\DataObject objects).
474478
*
475479
* @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
476-
* @return \Guzzle\Http\Message\Response
480+
* @return Guzzle\Http\Message\Response[] or OpenCloud\ObjectStore\Resource\DataObject[] depending on $returnType
477481
*/
478-
public function uploadObjects(array $files, array $commonHeaders = array())
482+
public function uploadObjects(array $files, array $commonHeaders = array(), $returnType = ReturnType::RESPONSE_ARRAY)
479483
{
480484
$requests = $entities = array();
481485

@@ -514,11 +518,22 @@ public function uploadObjects(array $files, array $commonHeaders = array())
514518

515519
$responses = $this->getClient()->send($requests);
516520

517-
foreach ($entities as $entity) {
518-
$entity->close();
521+
if (ReturnType::RESPONSE_ARRAY === $returnType) {
522+
foreach ($entities as $entity) {
523+
$entity->close();
524+
}
525+
return $responses;
526+
} else {
527+
// Convert responses to DataObjects before returning
528+
$dataObjects = array();
529+
foreach ($responses as $index => $response) {
530+
$dataObjects[] = $this->dataObject()
531+
->populateFromResponse($response)
532+
->setName($files[$index]['name'])
533+
->setContent($entities[$index]);
534+
}
535+
return $dataObjects;
519536
}
520-
521-
return $responses;
522537
}
523538

524539
/**

tests/OpenCloud/Tests/ObjectStore/Resource/ContainerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Guzzle\Http\Message\Response;
2121
use OpenCloud\Common\Constants\Size;
2222
use OpenCloud\Tests\ObjectStore\ObjectStoreTestCase;
23+
use OpenCloud\ObjectStore\Enum\ReturnType;
2324

2425
class ContainerTest extends ObjectStoreTestCase
2526
{
@@ -225,6 +226,39 @@ public function test_Upload_Multiple()
225226
));
226227
}
227228

229+
public function test_Upload_Multiple_Return_DataObject_Array()
230+
{
231+
$tempFileName = tempnam(sys_get_temp_dir(), "php-opencloud-test-");
232+
233+
$tempFile = fopen($tempFileName, 'w+');
234+
fwrite($tempFile, 'BAZQUX');
235+
236+
$container = $this->container;
237+
238+
$dataObjects = $container->uploadObjects(array(
239+
array('name' => 'test1', 'body' => 'FOOBAR'),
240+
array('name' => 'test2', 'path' => $tempFileName),
241+
array('name' => 'test2', 'body' => 'BARBAR')
242+
), array(), ReturnType::DATA_OBJECT_ARRAY);
243+
fclose($tempFile);
244+
unlink($tempFileName);
245+
246+
$this->assertInstanceOf('OpenCloud\ObjectStore\Resource\DataObject', $dataObjects[0]);
247+
$this->assertEquals('test1', $dataObjects[0]->getName());
248+
$this->assertInstanceOf('Guzzle\Http\EntityBody', $dataObjects[0]->getContent());
249+
$this->assertEquals('FOOBAR', (string) $dataObjects[0]->getContent());
250+
251+
$this->assertInstanceOf('OpenCloud\ObjectStore\Resource\DataObject', $dataObjects[1]);
252+
$this->assertEquals('test2', $dataObjects[1]->getName());
253+
$this->assertInstanceOf('Guzzle\Http\EntityBody', $dataObjects[1]->getContent());
254+
$this->assertEquals('BAZQUX', (string) $dataObjects[1]->getContent());
255+
256+
$this->assertInstanceOf('OpenCloud\ObjectStore\Resource\DataObject', $dataObjects[2]);
257+
$this->assertEquals('test2', $dataObjects[2]->getName());
258+
$this->assertInstanceOf('Guzzle\Http\EntityBody', $dataObjects[2]->getContent());
259+
$this->assertEquals('BARBAR', (string) $dataObjects[2]->getContent());
260+
}
261+
228262
public function test_Upload()
229263
{
230264
$this->assertInstanceOf(

0 commit comments

Comments
 (0)