Digital Ocean Spaces with Laravel

Martin Oppenheimer
Apr 15 · 2 min read
Laravel — Digital Ocean

Step-by-step Guide

  1. Install via Composer Flysystem Adapter for AWS SDK V3
composer require league/flysystem-aws-s3-v3
do_spaces' => [     
'driver' => 's3',
'key' => env('DO_SPACES_KEY'),
'secret' => env('DO_SPACES_SECRET'),
'endpoint' => env('DO_SPACES_ENDPOINT'),
'region' => env('DO_SPACES_REGION'),
'bucket' => env('DO_SPACES_BUCKET'),
],
DO_SPACES_KEY= 
DO_SPACES_SECRET=
DO_SPACES_ENDPOINT=sfo2.digitaloceanspaces.com (example) DO_SPACES_REGION=SFO2 (example)
DO_SPACES_BUCKET=NAME_OF_YOUR_SPACE

$attribute_name: name of the model's attribute
$disk: name of the disk configured before
$destination_path: name of the folder you want to store the files in Digital Ocean Space
public function setFileAttribute($value) {
$attribute_name = "file";
$disk = "do_spaces";
$destination_path = "name-of-the-folder-inside-dospace";
$this->uploadFileToDisk($value, $attribute_name, $disk,
$destination_path);
}
public function getFile($id){
$document = Document::where('id','=', $id)->firstOrFail();
$file = Storage::disk('do_spaces')->get($document->file);
$mimetype = \GuzzleHttp\Psr7\mimetype_from_filename($document->file);
$headers = [
'Content-Type' => $mimetype,
];
return response($file, 200, $headers);
}

Conclusion

Some of the advantages are:

  • Availability
  • Easy to manage
  • Security
  • Scalability on-demand
  • Regular backups

Original Post:

Light-it

Insights and opinions from software engineers at Light-it.

Martin Oppenheimer

Written by

Co-Founder at Light-it | Entrepreneur. Software Engineer. Business Developer. Sports enthusiast.

Light-it

Light-it

Insights and opinions from software engineers at Light-it.