Skip to content

Commit

Permalink
S3
Browse files Browse the repository at this point in the history
  • Loading branch information
lennyrouanet committed May 18, 2023
1 parent a9f30d0 commit 28ae47b
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 66 deletions.
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,35 @@ $exist = $clientMySQL->existByColumnValue(
### S3

```php
use Phant\Client\S3 as ClientS3;
use Phant\Client\Service\S3 as ClientS3;

$clientS3 = new ClientS3(
$region,
$endpoint,
$accessKey,
$secretKey,
);
$clientS3->setObject($bucket, 'foo', 'bar');
$bar = $clientS3->getObject($bucket, 'foo');
$clientS3->deleteObject($bucket, 'foo');
```

### S3 Bucket

```php
use Phant\Client\Service\S3 as ClientS3;
use Phant\Client\Service\S3\Object as ClientS3Object;

$clientS3Object = new ClientS3Object(
new ClientS3(
$region,
$endpoint,
$accessKey,
$secretKey,
),
$bucket
);
$clientS3->set('foo', 'bar');
$bar = $clientS3->get('foo');
$clientS3->delete('foo');
$clientS3Object->set('foo', 'bar');
$bar = $clientS3Object->get('foo');
$clientS3Object->delete('foo');
```
24 changes: 24 additions & 0 deletions component/Port/S3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Phant\Client\Port;

interface S3
{
public function setObject(
string $bucket,
string $key,
string $body
): void;

public function getObject(
string $bucket,
string $key
): string;

public function deleteObject(
string $bucket,
string $key
): void;
}
21 changes: 21 additions & 0 deletions component/Port/S3/Object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Phant\Client\Port\S3;

interface Object
{
public function set(
string $key,
string $body
): void;

public function get(
string $key
): string;

public function delete(
string $key
): void;
}
62 changes: 0 additions & 62 deletions component/S3.php

This file was deleted.

74 changes: 74 additions & 0 deletions component/Service/S3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Phant\Client\Service;

use Aws\S3\S3Client;
use Phant\Error\NotFound;

class S3 implements \Phant\Client\Port\S3
{
protected S3Client $S3Client;

public function __construct(
string $region,
string $endpoint,
string $accessKey,
string $secretKey,
string $version = '2006-03-01'
) {
$this->S3Client = new S3Client([
'region' => $region,
'endpoint' => $endpoint,
'credentials' => [
'key' => $accessKey,
'secret' => $secretKey,
],
'version' => $version,
'use_path_style_endpoint' => true
]);
}

public function setObject(
string $bucket,
string $key,
string $body
): void {
$this->S3Client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'Body' => $body,
]);
}

public function getObject(
string $bucket,
string $key
): string {
try {
$object = $this->S3Client->getObject([
'Bucket' => $bucket,
'Key' => $key,
]);

return $object['Body']->getContents();
} catch (\Exception $e) {
throw new NotFound('Object : ' . $key);
}
}

public function deleteObject(
string $bucket,
string $key
): void {
try {
$this->S3Client->deleteObject([
'Bucket' => $bucket,
'Key' => $key,
]);
} catch (\Exception $e) {
throw new NotFound('Object : ' . $key);
}
}
}
45 changes: 45 additions & 0 deletions component/Service/S3/Object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Phant\Client\Service\S3;

use Phant\Client\Service\S3;

class Object implements \Phant\Client\Port\S3\Object
{
public function __construct(
protected readonly S3 $client,
protected readonly string $bucket
) {
}

public function set(
string $key,
string $body
): void {
$this->client->setObject(
$this->bucket,
$key,
$body,
);
}

public function get(
string $key
): string {
return $this->client->getObject(
$this->bucket,
$key,
);
}

public function delete(
string $key
): void {
$this->client->deleteObject(
$this->bucket,
$key,
);
}
}

0 comments on commit 28ae47b

Please sign in to comment.