Skip to content

Commit

Permalink
Merge pull request #3 from byjg/4.1.1
Browse files Browse the repository at this point in the history
4.1.1
  • Loading branch information
byjg committed Jul 24, 2020
2 parents bd82eef + 0b494b2 commit a3b0962
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -16,8 +16,11 @@ services:

before_install:
- docker run --name mongodb-container --rm -e TZ=America/Winnipeg -p 27017:27017 -d mongo:3
- docker run -p 9000:9000 -e "MINIO_ACCESS_KEY=aaa" -e "MINIO_SECRET_KEY=12345678" -d minio/minio server /data
- docker run -p 8000:8000 -d amazon/dynamodb-local
- echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

- export S3_CONNECTION="s3://aaa:12345678@us-east-1/mybucket?create=true&endpoint=http://127.0.0.1:9000"
- export DYNAMODB_CONNECTION="dynamodb://access_key:secret_key@us-east-1/tablename?endpoint=http://127.0.0.1:8000"

install:
- php -i
Expand Down
13 changes: 13 additions & 0 deletions AwsDynamoDbKeyValue.md
Expand Up @@ -11,6 +11,19 @@ The full connection string can be:
dynamodb://AKA12345678899:aaaaaaaaaaaaaaaaaaaaaaaaa@us-east-1/mytable
```

You can add any extra arguments supported by the DynamoDB api. You can get a full list here:
- https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.AwsClient.html#___construct

One of the most populars is the parameter `endpoint` where we can set a custom endpoint to access
an DynamoDB compatible interface.

An example can be:

```
s3://AKA12345678899:aaaaaaaaaaaaaaaaaaaaaaaaa@us-east-1/tablename?endpoint=http://localhost:8000
```


# Preparing to use DynamoDb

DynamoDb stores the information slightly different than a model dto structure.
Expand Down
24 changes: 24 additions & 0 deletions AwsS3KeyValue.md
Expand Up @@ -10,6 +10,30 @@ The full connection string can be:
```
s3://AKA12345678899:aaaaaaaaaaaaaaaaaaaaaaaaa@us-east-1/mybucket
```

You can add any extra arguments supported by the S3 api. You can get a full list here:
- https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.AwsClient.html#___construct
- https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.S3.S3Client.html#___construct

One of the most populars is the parameter `endpoint` where we can set a custom endpoint to access
an S3 compatible interface.

An example can be:

```
s3://AKA12345678899:aaaaaaaaaaaaaaaaaaaaaaaaa@us-east-1/mybucket?endpoint=http://localhost:9000
```

There is a specific parameter called `create` from `anydataset/no-sql` that permit create a bucket if
it doesn't exist.

Example:

```
s3://AKA12345678899:aaaaaaaaaaaaaaaaaaaaaaaaa@us-east-1/mybucket?create=true
```


# List all objects

```php
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -46,7 +46,7 @@ vendor/bin/phpunit testsdb/MongoDbDriverTest.php

You need setup your environment with:

- DYNAMODB_CONNECTION = "dynamodb://access_key:secret_key@region/bucketname"
- DYNAMODB_CONNECTION = "dynamodb://access_key:secret_key@region/tablename"

Once defined:

Expand Down
19 changes: 17 additions & 2 deletions src/AwsDynamoDbDriver.php
Expand Up @@ -34,14 +34,21 @@ public function __construct($connectionString)
{
$uri = new Uri($connectionString);

$this->dynamoDbClient = new DynamoDbClient([
$defaultParameters = [
'version' => 'latest',
'region' => $uri->getHost(),
'credentials' => [
'key' => $uri->getUsername(),
'secret' => $uri->getPassword(),
],
]);
];

$extraParameters = [];
parse_str($uri->getQuery(), $extraParameters);

$dynamoDbParameters = array_merge($defaultParameters, $extraParameters);

$this->dynamoDbClient = new DynamoDbClient($dynamoDbParameters);

$this->table = preg_replace('~^/~', '', $uri->getPath());
}
Expand Down Expand Up @@ -214,4 +221,12 @@ public function removeBatch($key, $options = [])
{
// TODO: Implement removeBatch() method.
}

public function getTablename() {
return $this->table;
}

public function client() {
return $this->dynamoDbClient;
}
}
28 changes: 28 additions & 0 deletions src/AwsS3Driver.php
Expand Up @@ -45,11 +45,35 @@ public function __construct($connectionString)
$extraParameters = [];
parse_str($uri->getQuery(), $extraParameters);

$createBucket = false;
if (isset($extraParameters["create"])) {
$createBucket = ($extraParameters["create"] == "true");
unset($extraParameters["create"]);
}

$s3Parameters = array_merge($defaultParameters, $extraParameters);

$this->s3Client = new S3Client($s3Parameters);

$this->bucket = preg_replace('~^/~', '', $uri->getPath());

try {
$result = $this->s3Client->headBucket([
'Bucket' => $this->bucket,
]);
} catch (\Aws\S3\Exception\S3Exception $ex) {
if (strpos($ex->getMessage(), "404") !== false && $createBucket) {
$this->s3Client->createBucket([
'ACL' => 'private',
'Bucket' => $this->bucket,
'CreateBucketConfiguration' => [
'LocationConstraint' => $uri->getHost(),
],
]);
} else {
throw $ex;
}
}
}

/**
Expand Down Expand Up @@ -167,4 +191,8 @@ public function removeBatch($key, $options = [])
{
// TODO: Implement removeBatch() method.
}

public function client() {
return $this->s3Client;
}
}
32 changes: 32 additions & 0 deletions testsdb/AwsDynamoDbDriverTest.php
Expand Up @@ -2,6 +2,7 @@

namespace TestsDb\AnyDataset;

use Aws\DynamoDb\Exception\DynamoDbException;
use ByJG\AnyDataset\NoSql\AwsDynamoDbDriver;
use ByJG\AnyDataset\NoSql\Factory;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -44,11 +45,42 @@ protected function setUp()
$awsConnection = getenv("DYNAMODB_CONNECTION");
if (!empty($awsConnection)) {
$this->object = Factory::getKeyValueInstance($awsConnection);

$this->createTable();

$this->object->remove(1, $this->options);
$this->object->remove(2, $this->options);
}
}

protected function createTable()
{
try {
$this->object->client()->describeTable(['TableName' => $this->object->getTablename()]);
} catch (DynamoDbException $ex) {
// table doesn't exist, create it below
$this->object->client()->createTable([
'TableName' => $this->object->getTablename(),
'KeySchema' => [
[
'AttributeName' => 'key',
'KeyType' => 'HASH'
]
],
'AttributeDefinitions' => [
[
'AttributeName' => 'key',
'AttributeType' => 'N'
],
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 10
]
]);
}
}

protected function tearDown()
{
if (!empty($this->object)) {
Expand Down

0 comments on commit a3b0962

Please sign in to comment.