Repository navigation
Expand file tree
/
Copy path01-basic.php
More file actions
117 lines (83 loc) · 3 KB
/
Copy path01-basic.php
File metadata and controls
117 lines (83 loc) · 3 KB
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
<?php declare(strict_types=1);
/**
* Object Store - storing large objects (files) in NATS.
*
* Objects are automatically split into chunks and read back lazily.
* Supports: put (bytes/string/file/stream), get, delete, metadata.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
use Sofascore\Nats\Connection;
use Sofascore\Nats\Enum\StorageType;
use Sofascore\Nats\ObjectStore\ObjectMeta;
use Sofascore\Nats\ObjectStore\ObjectStoreConfig;
$conn = Connection::connect();
$js = $conn->jetStream();
// --- Creating an Object Store bucket ---
$store = $js->createObjectStore(new ObjectStoreConfig(
bucket: 'files',
description: 'Application file storage',
maxBytes: 1024 * 1024 * 100, // 100MB limit
storage: StorageType::File,
replicas: 1,
maxChunkSize: 131072, // 128KB chunks
));
// --- Put bytes ---
$info = $store->putBytes('config.json', json_encode([
'database' => ['host' => 'localhost', 'port' => 5432],
'cache' => ['ttl' => 3600],
]));
echo "Stored: {$info->name}, size={$info->size}, chunks={$info->chunks}\n";
echo "Digest: {$info->digest}\n";
// --- Put string ---
$info = $store->putString('readme.txt', 'This is the content of the readme file.');
echo "Stored: {$info->name}\n";
// --- Put file ---
// $info = $store->putFile('/path/to/large-file.zip');
// echo "Stored file: {$info->name}, size={$info->size}\n";
// --- Put with metadata ---
$meta = new ObjectMeta(
name: 'report-2024.pdf',
description: 'Annual report',
headers: new \Sofascore\Nats\Headers(['X-Author' => 'Team']),
);
$info = $store->put($meta, 'PDF content here...');
echo "Stored with meta: {$info->name}, desc={$info->description}\n";
// --- Get ---
$result = $store->get('config.json');
echo "Object info: name={$result->info()->name}, size={$result->info()->size}\n";
while (($chunk = $result->read(8)) !== '') {
echo "Chunk: {$chunk}\n";
}
$result->close();
// --- Get bytes/string ---
$content = $store->getBytes('config.json');
echo "Bytes: {$content}\n";
$text = $store->getString('readme.txt');
echo "String: {$text}\n";
// --- Get to file ---
// Streams object contents directly to disk instead of buffering the whole object first.
// $store->getFile('report-2024.pdf', '/tmp/report.pdf');
// --- Update metadata ---
$updatedInfo = $store->updateMeta('config.json', new ObjectMeta(
name: 'config.json',
description: 'Updated description',
));
echo "Updated meta: {$updatedInfo->description}\n";
// --- List ---
$objects = $store->list();
foreach ($objects as $obj) {
echo "Object: {$obj->name}, size={$obj->size}, chunks={$obj->chunks}\n";
}
// Full metadata history remains available explicitly when you need it.
// $history = $store->list(includeHistory: true);
// --- Status ---
$status = $store->status();
echo "Bucket: {$status->bucket}\n";
echo "Size: {$status->size}\n";
echo "Objects: {$status->objects}\n";
// --- Delete ---
$store->delete('config.json');
echo "Deleted config.json\n";
// --- Cleanup ---
$js->deleteObjectStore('files');
$conn->close();