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
- Go to Linode cloud console.
- Go to Object Storage and select Create Bucket.
- From the Object storage dashboard, go to Access key tab and select create access key.
- Give it a name, and give it Read/Write access for the bucket created in the previous step.
- 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.
To use the S3 driver we need to install the
flysystempackage in Laravel. Use the following command to install the package
composer require league/flysystem-aws-s3-v3 "^3.0"
We need to create some environment variables in the
.envfile 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.
config/filesystems.phpconfig file and add the following in the
<?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', ], ... ]
Make sure you have
linodeset as the default file system disk in the
.envfile. Otherwise you have to specify each time you call a method.
Storing files in Linode
Now you can use the available methods in Laravel to manage the files in Linode Object Storage.
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'