Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?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
}
}