Pular para o conteúdo principal

AWS S3

Amazon S3 (Simple Storage Service) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This driver allows you to interact with S3 and S3-compatible storage services ( like MinIO, Ceph) through a KeyValue interface.

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://access_key:secret_key@region/bucket');

The full connection string format:

s3://access_key:secret_key@region/bucket?option1=value1&option2=value2

Example:

s3://AKA12345678899:aaaaaaaaaaaaaaaaaaaaaaaaa@us-east-1/mybucket

Connection Options

You can add any additional parameters supported by the S3 API to the query string. For a comprehensive list, refer to:

Custom Endpoint

One common parameter is endpoint, which allows you to set a custom endpoint for working with S3-compatible services like MinIO, Ceph, or a local test environment:

s3://access_key:secret_key@us-east-1/mybucket?endpoint=http://localhost:9000

Bucket Creation

Auto-create Bucket

The library provides a special parameter create that will automatically create the bucket if it doesn't exist:

s3://access_key:secret_key@us-east-1/mybucket?create=true

You can combine multiple parameters:

s3://access_key:secret_key@us-east-1/mybucket?create=true&endpoint=http://localhost:9000

Basic Operations

List all objects

List all objects in the bucket:

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$iterator = $s3->getIterator();
print_r($iterator->toArray());

You can filter objects by prefix and set other options:

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$iterator = $s3->getIterator([
'Prefix' => 'folder/', // List objects with this prefix
'MaxKeys' => 100 // Maximum number of keys to retrieve
]);
print_r($iterator->toArray());

Inserting/Updating data

Store an object with a specific key:

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$s3->put("object_name", "value");

You can also specify additional options:

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$s3->put(
"object_name",
"value",
[
'ContentType' => 'text/plain',
'ACL' => 'public-read'
]
);

Checking if a key exists

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
if ($s3->has("object_name")) {
echo "Object exists!";
}

Retrieving a value

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$value = $s3->get("object_name");

With options:

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$value = $s3->get("object_name", [
'ResponseContentType' => 'application/json'
]);

Retrieving portions of a large object

For large objects, you can retrieve specific chunks to manage memory usage:

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');

// Get 1024 bytes starting from offset 0
$chunk1 = $s3->getChunk("object_name", [], 1024, 0);
// Get the next 1024 bytes (from offset 1024)
$chunk2 = $s3->getChunk("object_name", [], 1024, 1024);

// Example of reading a large file in chunks
$size = 1024;
$offset = 0;
$data = "";
do {
$chunk = $s3->getChunk("object_name", [], $size, $offset);
$data .= $chunk;
$offset += $size;
} while (strlen($chunk) == $size);

Removing a value

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$s3->remove("object_name");

Renaming a key

<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$s3->rename("old_key_name", "new_key_name");

Further Reading


Open source ByJG