Cloud service

Using a storage bucket in Laravel

Shipmate offers a Composer package that you can use within your Laravel codebase to interact with your storage bucket. The package builds on Laravel's filesystem, meaning you can use your storage bucket just like any other filesystem in Laravel.

Installing the Composer package

To install the package, run the following command in your terminal:

composer require shipmate-io/laravel-shipmate

Next, you must register your storage bucket with Laravel's filesystem. The filesystem's configuration is located in config/filesystems.php. Within this file, you may configure all of your filesystem disks. To register your storage bucket, add the following snippet as a new entry in the disks array.

'disks' => [

    // other disks

    'shipmate' => [
        'driver' => 'shipmate',
        'bucket' => env('SHIPMATE_STORAGE_BUCKET_NAME'),
        'visibility' => 'public', // public or private
    ],

],

The disk configuration consists of 3 properties:

  • driver   This property must be shipmate.
  • bucket   The name of the storage bucket, which you can inject as an environment variable when creating a release in Shipmate.
  • visibility   The default visibility of newly stored files.
    • If you set the property to public, files have a publicly accessible URL.
    • If you set the property to private, files are only accessible by your service.

Finally, to use the newly added disk as your default disk, update the FILESYSTEM_DISK environment variable.

FILESYSTEM_DISK=shipmate

Storing and retrieving files

The following snippet illustrates how you can use the package in your codebase. However, you can use all of Laravel's filesystem's features to interact with your storage bucket.

use Illuminate\Support\Facades\Storage;

$disk = Storage::disk('shipmate');

// write a file
$disk->put('avatars/1.jpg', $fileContents);

// read a file
$fileContents = $disk->get('avatars/1.jpg');

// copy a file
$disk->copy('avatars/1.jpg', 'avatars/2.jpg');