0

I have some folders in spaces. I want to create a zip of a folder that has some sub-folders. But I can not do that. I am able to do it in local storage. But from spaces I can not do. My project is in php/laravel. Can anyone help me out in this please?

$zipFile->addDirRecursive(Storage::path('public/'.$path))
        ->saveAsFile($outputFilename);
return $zipFile->outputAsSymfonyResponse($outputFilename);

This is the code I do zip from my local storage. How to do it from spaces?

ash
  • 11
  • 3

1 Answers1

0

// Step 1: Install package

composer require league/flysystem-aws-s3-v3

// Step 2: Add this code in config/filesystems.php

'spaces' => [
    'driver' => 's3',
    'key' => env('DO_SPACES_KEY'),
    'secret' => env('DO_SPACES_SECRET'),
    'region' => env('DO_SPACES_REGION'),
    'endpoint' => env('DO_SPACES_ENDPOINT'),
    'bucket' => env('DO_SPACES_BUCKET'),
    'url' => env('DO_SPACES_URL'),
],

// Step 3: Add this code in .env file

DO_SPACES_KEY=your_key
DO_SPACES_SECRET=your_secret
DO_SPACES_REGION=your_region
DO_SPACES_ENDPOINT=https://your_region.digitaloceanspaces.com
DO_SPACES_BUCKET=your_bucket
DO_SPACES_URL=https://your_bucket.your_region.digitaloceanspaces.com

// Step 4: Add this code in your controller

use Illuminate\Support\Facades\Storage;

public function download($file)
{
    $path = Storage::disk('spaces')->getDriver()->getAdapter()->getClient()->getObjectUrl(env('DO_SPACES_BUCKET'), $file);
    return redirect($path);
}

// Step 5: Add this code in your route

Route::get('/download/{file}', [YourController::class, 'download'])->name('download');

// Step 6: Add this code in your view

<a href="{{ route('download', $file) }}">Download</a>

I tried to explain in detail, I hope it helps :)