Laravel Quickstart

Installation

To use Rabata.io with Laravel, you'll need to install the AWS SDK for PHP and configure Laravel to work with it.

View markdown

Install AWS SDK

$ composer require aws/aws-sdk-php

Create a Laravel Project (Optional)

If you're starting from scratch, you can create a new Laravel project:

$ composer create-project laravel/laravel my-rabata-laravel-app
$ cd my-rabata-laravel-app
$ composer require aws/aws-sdk-php

Configuration

There are several ways to configure Laravel to work with Rabata.io.

Method 1: Using Laravel's Filesystem Configuration

Laravel provides a convenient way to work with various file systems through its filesystem configuration. You can add Rabata.io as an S3 disk.

First, update your .env file with your Rabata.io credentials:

AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=your-bucket-name
AWS_ENDPOINT=https://s3.us-east-1.rabata.io
AWS_USE_PATH_STYLE_ENDPOINT=true

Then, update your config/filesystems.php file to include the Rabata.io endpoint:

's3' => [
    'driver' => 's3',
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('AWS_DEFAULT_REGION'),
    'bucket' => env('AWS_BUCKET'),
    'url' => env('AWS_URL'),
    'endpoint' => env('AWS_ENDPOINT'),
    'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],

Method 2: Creating a Custom Disk

You can also create a custom disk specifically for Rabata.io:

// In config/filesystems.php
'disks' => [
    // ... other disks

    'rabata' => [
        'driver' => 's3',
        'key' => env('RABATA_ACCESS_KEY_ID'),
        'secret' => env('RABATA_SECRET_ACCESS_KEY'),
        'region' => env('RABATA_REGION', 'us-east-1'),
        'bucket' => env('RABATA_BUCKET'),
        'endpoint' => env('RABATA_ENDPOINT', 'https://s3.us-east-1.rabata.io'),
        'use_path_style_endpoint' => true,
    ],
],

Method 3: Using a Service Provider

For more complex configurations, you can create a service provider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
use Aws\S3\S3Client;

class RabataServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Storage::extend('rabata', function ($app, $config) {
            $client = new S3Client([
                'credentials' => [
                    'key'    => $config['key'],
                    'secret' => $config['secret'],
                ],
                'region' => $config['region'],
                'version' => 'latest',
                'endpoint' => $config['endpoint'],
                'use_path_style_endpoint' => true,
            ]);

            return new Filesystem(new AwsS3Adapter($client, $config['bucket']));
        });
    }

    public function register()
    {
        //
    }
}

Then register this provider in your config/app.php file.

Basic Operations

Here are some common operations you can perform with Laravel's Storage facade and Rabata.io.

File Operations

Upload a File

use Illuminate\Support\Facades\Storage;

// Upload a file to the default s3 disk (configured for Rabata.io)
$path = Storage::disk('s3')->put('file.txt', 'File contents');

// Or using a custom disk
$path = Storage::disk('rabata')->put('file.txt', 'File contents');

// Upload a file from the local filesystem
$path = Storage::disk('s3')->putFile('photos', new File('/path/to/photo.jpg'));

Download a File

// Get the contents of a file
$contents = Storage::disk('s3')->get('file.txt');

// Download a file to the local filesystem
Storage::disk('s3')->download('file.txt', 'local-file.txt');

Check if a File Exists

if (Storage::disk('s3')->exists('file.txt')) {
    // The file exists
}

Delete a File

Storage::disk('s3')->delete('file.txt');

// Delete multiple files
Storage::disk('s3')->delete(['file1.txt', 'file2.txt']);

Directory Operations

List Files in a Directory

$files = Storage::disk('s3')->files('directory');

// Recursively list files
$files = Storage::disk('s3')->allFiles('directory');

Create a Directory

Storage::disk('s3')->makeDirectory('directory');

Delete a Directory

Storage::disk('s3')->deleteDirectory('directory');

Advanced Operations

Here are some more advanced operations you can perform with Laravel and Rabata.io.

Working with File URLs

Get a URL for a File

// Get a temporary URL for a file (valid for a limited time)
$url = Storage::disk('s3')->temporaryUrl('file.txt', now()->addMinutes(5));

// Get a public URL for a file (if the file is publicly accessible)
$url = Storage::disk('s3')->url('file.txt');

File Metadata

Get File Metadata

// Get the size of a file
$size = Storage::disk('s3')->size('file.txt');

// Get the last modified time of a file
$time = Storage::disk('s3')->lastModified('file.txt');

Using Rabata.io in Controllers

Here's an example of how to use Rabata.io in a Laravel controller for file uploads:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class FileController extends Controller
{
    public function upload(Request $request)
    {
        $request->validate([
            'file' => 'required|file|max:10240', // Max 10MB
        ]);

        $path = $request->file('file')->store('uploads', 's3');

        return response()->json([
            'success' => true,
            'path' => $path,
            'url' => Storage::disk('s3')->url($path),
        ]);
    }

    public function download($filename)
    {
        $path = 'uploads/' . $filename;

        if (!Storage::disk('s3')->exists($path)) {
            abort(404);
        }

        return Storage::disk('s3')->download($path, $filename);
    }
}

Using Rabata.io with Laravel Queues

For handling large file uploads or processing, you can use Laravel queues:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;

class ProcessUploadedFile implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $path;

    public function __construct($path)
    {
        $this->path = $path;
    }

    public function handle()
    {
        // Process the file from Rabata.io
        $contents = Storage::disk('s3')->get($this->path);

        // Do something with the file contents

        // Maybe upload a processed version
        Storage::disk('s3')->put('processed/' . basename($this->path), $processedContents);
    }
}
PHP Quickstart Symfony Integration