DEV Community

Cover image for How to use Linode Object Storage with Laravel
Ishaan Sheikh
Ishaan Sheikh

Posted on • Edited on

How to use Linode Object Storage with Laravel

What is Linode Object storage?

Linode Object Storage is a highly available S3 compatible object storage.
It is used to manage unstructured data such as images, videos, etc.

Setting up Object Storage in Linode

  1. Go to Linode cloud console.
  2. Go to Object Storage and select Create Bucket.
    creating object storage bucket in linode

  3. From the Object storage dashboard, go to Access key tab and select create access key.
    create access key

  4. Give it a name, and give it Read/Write access for the bucket created in the previous step.
    give access to bucket

  5. It will show you the access and secret keys, copy and save it securely.

Configuring Laravel application

Since Linode Object Storage is a S3 compatible, we can use the S3 driver in Laravel for managing files.

  1. To use the S3 driver we need to install the flysystem package in Laravel. Use the following command to install the package

    composer require league/flysystem-aws-s3-v3 "^3.0"
    
  2. We need to create some environment variables in the .env file of Laravel application as follows

    LINODE_KEY="your-linode-access-key"
    LINODE_SECRET="your-linode-access-secret"
    LINODE_ENDPOINT="https://ap-south-1.linodeobjects.com"
    LINODE_REGION="ap-south-1"
    LINODE_BUCKET="dev-to"
    LINODE_BUCKET_URL="https://dev-to.ap-south-1.linodeobjects.com/"
    

    You can get the bucket url from the details of the bucket in linode cloud console.

  3. Go to config/filesystems.php config file and add the following in the disks array.

    <?php
    
    'disks' => [
      ...
     'linode' => [
         'driver' => 's3',
         'key' => env('LINODE_KEY'),
         'secret' => env('LINODE_SECRET'),
         'endpoint' => env('LINODE_ENDPOINT'),
         'region' => env('LINODE_REGION'),
         'bucket' => env('LINODE_BUCKET'),
         'visibility' => 'public',
      ],
      ...
    ]
    
  4. Make sure you have linode set as the default file system disk in the .env file. Otherwise you have to specify each time you call a method.

    FILESYSTEM_DISK=linode
    

Storing files in Linode

Now you can use the available methods in Laravel to manage the files in Linode Object Storage.

For example,

use Illuminate\Support\Facades\Storage;

Storage::disk('linode')->put('avatars/1', $content);

$contents = Storage::get('avatars/user.jpg'); // No need to specify disk name if the default is set to 'linode'

Enter fullscreen mode Exit fullscreen mode

Top comments (0)