diff --git a/.travis.yml b/.travis.yml index e324ce2..8151ab3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/AwsDynamoDbKeyValue.md b/AwsDynamoDbKeyValue.md index ed9e02a..9dafb86 100644 --- a/AwsDynamoDbKeyValue.md +++ b/AwsDynamoDbKeyValue.md @@ -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. diff --git a/AwsS3KeyValue.md b/AwsS3KeyValue.md index 3dd36f8..b5f4187 100644 --- a/AwsS3KeyValue.md +++ b/AwsS3KeyValue.md @@ -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 diff --git a/README.md b/README.md index abccb67..1b015e7 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/AwsDynamoDbDriver.php b/src/AwsDynamoDbDriver.php index 7324b47..d4f0e8b 100644 --- a/src/AwsDynamoDbDriver.php +++ b/src/AwsDynamoDbDriver.php @@ -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()); } @@ -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; + } } diff --git a/src/AwsS3Driver.php b/src/AwsS3Driver.php index ee10917..9148794 100644 --- a/src/AwsS3Driver.php +++ b/src/AwsS3Driver.php @@ -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; + } + } } /** @@ -167,4 +191,8 @@ public function removeBatch($key, $options = []) { // TODO: Implement removeBatch() method. } + + public function client() { + return $this->s3Client; + } } diff --git a/testsdb/AwsDynamoDbDriverTest.php b/testsdb/AwsDynamoDbDriverTest.php index 821e811..0d09389 100644 --- a/testsdb/AwsDynamoDbDriverTest.php +++ b/testsdb/AwsDynamoDbDriverTest.php @@ -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; @@ -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)) {