Skip to content
Snippets Groups Projects
minio.php 3.48 KiB
Newer Older
<?php

namespace SmartDataContext\Persistence;

use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\StreamInterface;

/**
 * Simple MiniIO client using AWS SDK
 */
class MinIOClient
{
    private S3Client $s3Client;

    public function __construct(?string $endpoint = null, ?string $username = null, ?string $password = null, string $region = 'us-east-1')
    {
        $endpoint = !empty($endpoint) ? $endpoint : (getenv('MINIO_HOST') ?: 'minio');
        $username = !empty($username) ? $username : (getenv('MINIO_USERNAME') ?: 'minio');
        $password = !empty($password) ? $password : (getenv('MINIO_PASSWORD') ?: 'minio');

        $this->s3Client = new S3Client([
            'version' => 'latest',
            'region' => $region, // Region is required by AWS SDK, use any valid value
            'endpoint' => $endpoint,
            'credentials' => [
                'key' => $username,
                'secret' => $password,
            ],
            'use_path_style_endpoint' => true, // Required for MinIO
        ]);
    }

    /**
     * Create a new bucket.
     *
     */
    public function createBucket(string $bucketName): bool
    {
        if (! $this->s3Client->doesBucketExist($bucketName)) {
            $this->s3Client->createBucket(['Bucket' => $bucketName]);
        }
        return true;
    }

    /**
     * Store a new object in a bucket using a stream.
     *
     */
    public function putObject(string $bucketName, string $objectKey, StreamInterface $stream): bool
    {
        $data = $stream->getContents();
        try {
            $this->s3Client->putObject([
                'Bucket' => $bucketName,
                'Key' => $objectKey,
                'Body' => $data
            ]);
            return true;
        } catch (AwsException $e) {
            echo "Error uploading object: " . $e->getMessage() . PHP_EOL;
            return false;
        }
    }

    /**
     * Retrieve an object from a bucket as a stream.
     *
     */
    public function getObject(string $bucketName, string $objectKey):StreamInterface
    {
        try {
            $result = $this->s3Client->getObject([
                'Bucket' => $bucketName,
                'Key' => $objectKey,
            ]);
            $result['Body']->rewind();
            return Utils::streamFor($result['Body']);
        } catch (AwsException $e) {
            throw $e;
        }
    }

    /**
     * Delete an object from a bucket.
     *
     */
    public function deleteObject(string $bucketName, string $objectKey): bool
    {
        try {
            $this->s3Client->deleteObject([
                'Bucket' => $bucketName,
                'Key' => $objectKey,
            ]);
            return true;
        } catch (AwsException $e) {
            echo "Error deleting object: " . $e->getMessage() . PHP_EOL;
            return false;
        }
    }

    /**
     * Create a bucket and store a new object using a stream.
     *
     */
    public function storeUnstructuredData(string $domain, $stream): ?string
    {
        // Create the bucket
        if (!$this->createBucket($domain)) {
            return null; // Return null if bucket creation fails
        }

        // Generate a unique object ID
        $objectId = UniqueID::generateID();

        // Store the object in the bucket
        if ($this->putObject($domain, $objectId, $stream)) {
            return $objectId; // Return the object ID on success
        }

        return null; // Return null on failure
    }
}