Added array keys for space and file listing

This commit is contained in:
David Wakelin 2021-08-16 16:00:08 +01:00
parent f9b49002c7
commit cfea1967ab
11 changed files with 41 additions and 27 deletions

View file

@ -15,16 +15,24 @@ Obtain API keys from the [Digital Ocean Applications & API dashboard](https://cl
```php ```php
use SpacesAPI\Spaces; use SpacesAPI\Spaces;
// Connect to a space
$spaces = new Spaces('api-key', 'api-secret'); $spaces = new Spaces('api-key', 'api-secret');
$space = $spaces->space('space-name'); $space = $spaces->space('space-name');
// Download a file
$file = $space->file('remote-file-1.txt'); $file = $space->file('remote-file-1.txt');
$file->download('local/file/path/file.txt'); $file->download('local/file/path/file.txt');
// Upload text to a file
$file2 = $space->uploadText("Lorem ipsum","remote-file-2.txt"); $file2 = $space->uploadText("Lorem ipsum","remote-file-2.txt");
// Get a signed public link, valid for 2 hours
$file2url = $file2->getSignedURL("2 hours"); $file2url = $file2->getSignedURL("2 hours");
// Make a copy
$file3 = $file2->copy('remote-file-3.txt'); $file3 = $file2->copy('remote-file-3.txt');
// Make a file public and get the URL
$file3->makePublic(); $file3->makePublic();
$file3url = $file3->getURL(); $file3url = $file3->getURL();
``` ```
@ -32,7 +40,7 @@ $file3url = $file3->getURL();
See more examples in [docs/Examples.md](docs/Examples.md) See more examples in [docs/Examples.md](docs/Examples.md)
## Upgrading? ## Upgrading?
Version 3 has many changes over verison 2, so we have written a [migration guide](docs/Upgrade2-3.md) Version 3 has many changes over version 2, so we have written a [migration guide](docs/Upgrade2-3.md)
## API reference ## API reference
* [\SpacesAPI\Spaces](docs/Spaces.md) * [\SpacesAPI\Spaces](docs/Spaces.md)

View file

@ -10,6 +10,7 @@ use SpacesAPI\Exceptions\FileDoesntExistException;
* You wouldn't normally instantiate this class directly, * You wouldn't normally instantiate this class directly,
* Rather obtain an instance from `\SpacesAPI\Space::list()`, `\SpacesAPI\Spaces::file()`, `\SpacesAPI\Spaces::uploadText()` or `\SpacesAPI\Spaces::uploadFile()` * Rather obtain an instance from `\SpacesAPI\Space::list()`, `\SpacesAPI\Spaces::file()`, `\SpacesAPI\Spaces::uploadText()` or `\SpacesAPI\Spaces::uploadFile()`
* *
* @property string $filename
* @property string $expiration * @property string $expiration
* @property string $e_tag * @property string $e_tag
* @property int $last_modified * @property int $last_modified

View file

@ -252,16 +252,16 @@ class Space
]) ])
); );
if (!isset($data['Contents'])) {
return ['files' => []];
}
$files = [ $files = [
'files' => $data['Contents'], 'files' => [],
]; ];
foreach ($files['files'] as $index => $fileInfo) { if (!isset($data['Contents'])) {
$files['files'][$index] = new File($this, $fileInfo['Key'], $fileInfo); return $files;
}
foreach ($data['Contents'] as $fileInfo) {
$files['files'][$fileInfo['Key']] = new File($this, $fileInfo['Key'], $fileInfo);
} }
if (isset($data["NextContinuationToken"]) && $data["NextContinuationToken"] != "") { if (isset($data["NextContinuationToken"]) && $data["NextContinuationToken"] != "") {

View file

@ -61,7 +61,7 @@ class Spaces
$spaces = []; $spaces = [];
foreach (Result::parse($this->s3->listBuckets()['Buckets']) as $bucket) { foreach (Result::parse($this->s3->listBuckets()['Buckets']) as $bucket) {
$spaces[] = new Space($this->s3, $bucket['Name']); $spaces[$bucket['Name']] = new Space($this->s3, $bucket['Name']);
} }
return $spaces; return $spaces;

View file

@ -1,7 +1,7 @@
{ {
"name": "sociallydev/spaces-api", "name": "sociallydev/spaces-api",
"description": "Library for accessing Digital Ocean spaces", "description": "Library for accessing Digital Ocean spaces",
"version":"3.0.0", "version":"3.1.0",
"type": "library", "type": "library",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [

14
composer.lock generated
View file

@ -4,20 +4,20 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "c76b703f4b3b49f8421bcb693ece4aa8", "content-hash": "77328f11e2ce84c590e531213fe931c6",
"packages": [ "packages": [
{ {
"name": "aws/aws-sdk-php", "name": "aws/aws-sdk-php",
"version": "3.187.2", "version": "3.190.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/aws/aws-sdk-php.git", "url": "https://github.com/aws/aws-sdk-php.git",
"reference": "0ec4ae120cfae758efa3c283dc56eb20602f094c" "reference": "8ca6a5f9834de3eb3fb84b28fc12c9088bc01293"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/0ec4ae120cfae758efa3c283dc56eb20602f094c", "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/8ca6a5f9834de3eb3fb84b28fc12c9088bc01293",
"reference": "0ec4ae120cfae758efa3c283dc56eb20602f094c", "reference": "8ca6a5f9834de3eb3fb84b28fc12c9088bc01293",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -92,9 +92,9 @@
"support": { "support": {
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
"issues": "https://github.com/aws/aws-sdk-php/issues", "issues": "https://github.com/aws/aws-sdk-php/issues",
"source": "https://github.com/aws/aws-sdk-php/tree/3.187.2" "source": "https://github.com/aws/aws-sdk-php/tree/3.190.2"
}, },
"time": "2021-08-04T18:12:21+00:00" "time": "2021-08-13T18:12:28+00:00"
}, },
{ {
"name": "guzzlehttp/guzzle", "name": "guzzlehttp/guzzle",

View file

@ -332,6 +332,7 @@ List all files in the space (recursively)
`array` `array`
> An array of `\SpacesAPI\File` instances indexed by the file name
<hr /> <hr />

View file

@ -116,7 +116,7 @@ List all your spaces
`array` `array`
> An array of \SpacesAPI\Space instances > An array of `\SpacesAPI\Space` instances indexed by the space name
<hr /> <hr />

View file

@ -20,7 +20,7 @@ class FileTest extends TestCase
$spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']); $spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']);
try { try {
$spaces->space('spaces-api-test')->destroySpace(); $spaces->space('spaces-api-test')->destroy();
} catch (SpaceDoesntExistException $e) { } catch (SpaceDoesntExistException $e) {
} }
@ -30,7 +30,7 @@ class FileTest extends TestCase
public static function tearDownAfterClass(): void public static function tearDownAfterClass(): void
{ {
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace(); (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy();
} }
public function testCanUpdatePrivacy() public function testCanUpdatePrivacy()

View file

@ -20,7 +20,7 @@ class SpaceTest extends TestCase
$spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']); $spaces = new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']);
try { try {
$spaces->space('spaces-api-test')->destroySpace(); $spaces->space('spaces-api-test')->destroy();
} catch (SpaceDoesntExistException $e) { } catch (SpaceDoesntExistException $e) {
} }
@ -29,7 +29,7 @@ class SpaceTest extends TestCase
public static function tearDownAfterClass(): void public static function tearDownAfterClass(): void
{ {
// (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace(); // (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy();
} }
public function testCanUpdateSpacePrivacy() public function testCanUpdateSpacePrivacy()
@ -102,7 +102,11 @@ class SpaceTest extends TestCase
$files = self::$space->listFiles()['files']; $files = self::$space->listFiles()['files'];
$this->assertIsArray($files); $this->assertIsArray($files);
$this->assertCount(2, $files); $this->assertCount(2, $files);
$this->assertInstanceOf(File::class, $files[0]); $this->assertInstanceOf(File::class, $files[array_key_first($files)]);
foreach ($files as $filename => $file) {
$this->assertEquals($file->filename, $filename);
}
foreach ($files as $file) { foreach ($files as $file) {
$file->delete(); $file->delete();

View file

@ -17,14 +17,14 @@ class SpacesTest extends TestCase
$dotenv->required(['SPACES_KEY', 'SPACES_SECRET']); $dotenv->required(['SPACES_KEY', 'SPACES_SECRET']);
try { try {
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace(); (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy();
} catch (SpaceDoesntExistException $e) { } catch (SpaceDoesntExistException $e) {
} }
} }
public static function tearDownAfterClass(): void public static function tearDownAfterClass(): void
{ {
(new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroySpace(); (new Spaces($_ENV['SPACES_KEY'], $_ENV['SPACES_SECRET']))->space('spaces-api-test')->destroy();
} }
public function testAuthenticationCanFail() public function testAuthenticationCanFail()
@ -68,8 +68,8 @@ class SpacesTest extends TestCase
$this->assertIsArray($list); $this->assertIsArray($list);
$spaceFound = false; $spaceFound = false;
foreach ($list as $space) { foreach ($list as $name => $space) {
if ($space->getName() == 'spaces-api-test') { if ($name == 'spaces-api-test' && $space->getName() == 'spaces-api-test') {
$spaceFound = true; $spaceFound = true;
} }
} }