Amazon S3 (Cloud Storage)
The artiframe add s3 extension liberates your projects from the limitations of the local file system (disk space issues, backup costs) and connects them to Object Storage providers like AWS S3, DigitalOcean Spaces, MinIO, or Cloudflare R2.
It abstracts the operations of the complex aws/aws-sdk-php package and exposes them as smooth methods under the src/Service/S3Storage.php class.
To install the extension, type the following command in your terminal:
$ artiframe add s3
Then enter your access credentials obtained from your Storage provider into your .env file:
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_DEFAULT_REGION=eu-central-1
AWS_BUCKET=my-app-storage
# Optional: If you are using a non-AWS S3 service (MinIO, DO Spaces):
# AWS_ENDPOINT=https://fra1.digitaloceanspaces.com
AWS_ENDPOINT variable, ArtiFrame's S3 service automatically begins to integrate seamlessly with any S3-compatible cloud provider (Cloudflare R2, Minio, etc.).
It's possible to transfer a physical file on your server to the cloud, or directly write text-based data (without creating a temporary file) to the cloud.
<?php
use Src\Service\S3Storage;
$s3 = new S3Storage();
// Save the avatar.jpg on the local server as 'users/avatars/1.jpg' on the cloud
// The 3rd Parameter (public-read) allows the file to be read via a direct URL
$result = $s3->upload('/var/www/tmp/avatar.jpg', 'users/avatars/1.jpg', 'public-read');
if ($result['status'] === true) {
echo "Uploaded! URL: " . $result['url'];
} else {
echo "Error: " . $result['error'];
}
Sometimes you need to directly send a dynamically generated JSON, CSV, or HTML data to the cloud without saving it to the disk.
<?php
$json = json_encode(['name' => 'Ali', 'age' => 30]);
// Directly write the string to the cloud without creating a file on disk (application/json)
$result = $s3->uploadFromString($json, 'exports/data.json', 'application/json', 'private');
If you uploaded the file with public permissions (e.g., Logos, CSS files):
<?php
$publicUrl = $s3->url('users/avatars/1.jpg');
// Output: https://bucket-name.s3.eu-central-1.amazonaws.com/users/avatars/1.jpg
Secure links meant for private files (e.g., purchased e-book, personal invoice) that become inaccessible once the URL expires.
<?php
// This link will only be accessible for 15 minutes
$temporaryLink = $s3->signedUrl('private/invoices/INV-100.pdf', 15);
echo "<a href='{$temporaryLink}'>Download Invoice</a>";
For pulling data from the cloud to your server or deleting it from the cloud (to free up space).
<?php
// 1. Download the file from the cloud to the local server
$s3->download('exports/data.json', '/var/www/downloads/data.json');
// 2. Single file deletion
$s3->delete('users/avatars/1.jpg');
// 3. Bulk Delete - Sends bulk requests to avoid hitting S3 API limits
$s3->deleteMany([
'logs/old-log-1.txt',
'logs/old-log-2.txt'
]);
| Method | Parameters | Description |
|---|---|---|
| exists() | string remotePath | Checks if the file exists in the cloud (returns bool). |
| list() | string prefix | Lists the files under the specified folder. Returns an array: [ ['key'=>'...', 'size'=>..., 'last_modified'=>'...'], ... ] |
| copy() | string from, string to | Copies a file in the cloud to another name/folder. |
| move() | string from, string to | First copies (copy), then deletes the old one (delete). Effectively renames/moves it. |
| size() | string remotePath | Returns the file size in bytes. It is extremely fast because it only checks the Header (headObject) without downloading the file. |