Skip to content

Commit 8131407

Browse files
committed
readme
1 parent c5ab2b7 commit 8131407

1 file changed

Lines changed: 183 additions & 10 deletions

File tree

README.md

Lines changed: 183 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,206 @@ Install using composer:
1616
composer require utopia-php/storage
1717
```
1818

19+
### Basic Usage
20+
1921
```php
2022
<?php
2123

2224
require_once '../vendor/autoload.php';
2325

2426
use Utopia\Storage\Storage;
2527
use Utopia\Storage\Device\Local;
28+
29+
// Set up a storage device (choose one)
30+
Storage::setDevice('files', new Local('/path/to/storage'));
31+
32+
// Common operations with any device
33+
$device = Storage::getDevice('files');
34+
35+
// Upload a file
36+
$device->upload('/local/path/to/file.png', 'destination/path/file.png');
37+
38+
// Check if file exists
39+
$exists = $device->exists('destination/path/file.png');
40+
41+
// Read file contents
42+
$contents = $device->read('destination/path/file.png');
43+
44+
// Delete a file
45+
$device->delete('destination/path/file.png');
46+
```
47+
48+
## Available Adapters
49+
50+
### Local Storage
51+
52+
Use the local filesystem for storing files.
53+
54+
```php
55+
use Utopia\Storage\Storage;
56+
use Utopia\Storage\Device\Local;
57+
58+
Storage::setDevice('files', new Local('/path/to/storage'));
59+
```
60+
61+
### AWS S3
62+
63+
Store files in Amazon S3.
64+
65+
```php
66+
use Utopia\Storage\Storage;
67+
use Utopia\Storage\Device\AWS;
68+
69+
Storage::setDevice('files', new AWS(
70+
'root', // Root path in bucket
71+
'YOUR_ACCESS_KEY',
72+
'YOUR_SECRET_KEY',
73+
'YOUR_BUCKET_NAME',
74+
AWS::US_EAST_1, // Region (default: us-east-1)
75+
AWS::ACL_PRIVATE // Access control (default: private)
76+
));
77+
```
78+
79+
### Generic S3-Compatible Provider
80+
81+
Store files in any S3-compatible service by providing the bucket host and region.
82+
83+
```php
84+
use Utopia\Storage\Storage;
2685
use Utopia\Storage\Device\S3;
86+
87+
Storage::setDevice('files', new S3(
88+
'root', // Root path in bucket
89+
'YOUR_ACCESS_KEY',
90+
'YOUR_SECRET_KEY',
91+
'YOUR_BUCKET_HOST', // e.g. your-bucket.s3.example.com
92+
'us-east-1',
93+
S3::ACL_PRIVATE
94+
));
95+
```
96+
97+
### DigitalOcean Spaces
98+
99+
Store files in DigitalOcean Spaces.
100+
101+
```php
102+
use Utopia\Storage\Storage;
27103
use Utopia\Storage\Device\DOSpaces;
28104

29-
// Instantiating local storage
30-
Storage::setDevice('files', new Local('path'));
105+
Storage::setDevice('files', new DOSpaces(
106+
'root', // Root path in bucket
107+
'YOUR_ACCESS_KEY',
108+
'YOUR_SECRET_KEY',
109+
'YOUR_BUCKET_NAME',
110+
DOSpaces::NYC3, // Region (default: nyc3)
111+
DOSpaces::ACL_PRIVATE // Access control (default: private)
112+
));
113+
```
114+
115+
### Backblaze B2
116+
117+
Store files in Backblaze B2 Cloud Storage.
118+
119+
```php
120+
use Utopia\Storage\Storage;
121+
use Utopia\Storage\Device\Backblaze;
122+
123+
Storage::setDevice('files', new Backblaze(
124+
'root', // Root path in bucket
125+
'YOUR_ACCESS_KEY',
126+
'YOUR_SECRET_KEY',
127+
'YOUR_BUCKET_NAME',
128+
Backblaze::US_WEST_004, // Region (default: us-west-004)
129+
Backblaze::ACL_PRIVATE // Access control (default: private)
130+
));
131+
```
132+
133+
### Linode Object Storage
134+
135+
Store files in Linode Object Storage.
136+
137+
```php
138+
use Utopia\Storage\Storage;
139+
use Utopia\Storage\Device\Linode;
140+
141+
Storage::setDevice('files', new Linode(
142+
'root', // Root path in bucket
143+
'YOUR_ACCESS_KEY',
144+
'YOUR_SECRET_KEY',
145+
'YOUR_BUCKET_NAME',
146+
Linode::EU_CENTRAL_1, // Region (default: eu-central-1)
147+
Linode::ACL_PRIVATE // Access control (default: private)
148+
));
149+
```
150+
151+
### Wasabi Cloud Storage
152+
153+
Store files in Wasabi Cloud Storage.
154+
155+
```php
156+
use Utopia\Storage\Storage;
157+
use Utopia\Storage\Device\Wasabi;
158+
159+
Storage::setDevice('files', new Wasabi(
160+
'root', // Root path in bucket
161+
'YOUR_ACCESS_KEY',
162+
'YOUR_SECRET_KEY',
163+
'YOUR_BUCKET_NAME',
164+
Wasabi::EU_CENTRAL_1, // Region (default: eu-central-1)
165+
Wasabi::ACL_PRIVATE // Access control (default: private)
166+
));
167+
```
31168

32-
// Or you can use AWS S3 storage
33-
Storage::setDevice('files', new S3('path', AWS_ACCESS_KEY, AWS_SECRET_KEY,AWS_BUCKET_NAME, AWS_REGION, AWS_ACL_FLAG));
169+
## Common Operations
34170

35-
// Or you can use DigitalOcean Spaces storage
36-
Storage::setDevice('files', new DOSpaces('path', DO_SPACES_ACCESS_KEY, DO_SPACES_SECRET_KEY, DO_SPACES_BUCKET_NAME, DO_SPACES_REGION, AWS_ACL_FLAG));
171+
All storage adapters provide a consistent API for working with files:
172+
173+
```php
174+
use Utopia\Storage\Storage;
37175

38176
$device = Storage::getDevice('files');
39177

40-
//upload
41-
$device->upload('file.png','path');
178+
// Upload a file
179+
$device->upload('/path/to/local/file.jpg', 'remote/path/file.jpg');
180+
181+
// Upload data from memory
182+
$device->uploadData($binary, 'remote/path/file.bin', 'application/octet-stream');
183+
184+
// Check if file exists
185+
$exists = $device->exists('remote/path/file.jpg');
186+
187+
// Get file size
188+
$size = $device->getFileSize('remote/path/file.jpg');
189+
190+
// Get file MIME type
191+
$mime = $device->getFileMimeType('remote/path/file.jpg');
192+
193+
// Get file MD5 hash
194+
$hash = $device->getFileHash('remote/path/file.jpg');
195+
196+
// Read file contents
197+
$contents = $device->read('remote/path/file.jpg');
198+
199+
// Read partial file contents
200+
$chunk = $device->read('remote/path/file.jpg', 0, 1024); // Read first 1KB
201+
202+
// Create directory
203+
$device->createDirectory('remote/new-directory');
204+
205+
// List files in directory
206+
$files = $device->getFiles('remote/directory');
207+
208+
// Delete file
209+
$device->delete('remote/path/file.jpg');
210+
211+
// Delete directory and its contents
212+
$device->deletePath('remote/directory');
42213

43-
//delete
44-
$device->delete('path');
214+
// Transfer files between storage devices
215+
$sourceDevice = Storage::getDevice('source');
216+
$targetDevice = Storage::getDevice('target');
45217

218+
$sourceDevice->transfer('source/path.jpg', 'target/path.jpg', $targetDevice);
46219
```
47220

48221
## System Requirements

0 commit comments

Comments
 (0)