简体   繁体   中英

Google Cloud Storage with Laravel, how to download a file

I am realy confused about Google Cloud Storage. I am not a programmer so I use Laravel and a Cloud Storage Client for PHP.

https://github.com/googleapis/google-cloud-php-storage

What I am trying to do:

A photographer is uploading a preview file, anyone can view this and it stored in my default bucket.

\Illuminate\Support\Facades\Storage::disk(env('FILESYSTEM_DRIVER'))->put(
    'storage' . DIRECTORY_SEPARATOR . $filename,
    file_get_contents($filename),
    \Illuminate\Contracts\Filesystem\Filesystem::VISIBILITY_PUBLIC
);

Then a photographer is uploading a zipfile with all pics, lets say to a bucket named "downloads". Someone who payed for it can click on a link to download it. How can I mask the filename and make sure the url is not shared? I could make a route where the user is signed in.

Route::group(['middleware' => ['role:user|store']], function() {
    Route::get('download/{orderid}', [
        'as' => 'download-link', 
        'uses' => 'OrderController@download'
    ]);
});

Some totaly psuedo code inside controller action, I don't what I am doing here... I don't even know how to get the object (file)...

$query = DB::table('orders')->where('id', $request->get('orderid'))
    ->where('userid', Auth::user()->id)
    ->firstOrFail()->pluck('realfilename');

$reponse = Storage::disk('gcs')
    ->bucket('downloads')
    ->getObject($query['realfilename']);

return Storage::download('file.jpg', $name, ['Content-Type: application/zip']);

The solution that I imagine to avoid the url sharing is this.

First you need to mount your buckets like Cloud Storage FUSE (it only works on linux) The idea is to have access to your files like any other local file, instead use an API to get the files from the cloud, you could write and read using a GS Fuse drive like any other local directory.

To start to use GS Fuse please check this link

Continuing with the example

This is an example example url

https://example.com/dwonload/pack123.zip

  1. First you need to validate if the customer is signed in or not
    • If not exit
  2. Check if the user bought that pics into your database
    • If not exit
  3. Read the files from the GS Fuse drive instead use Cloud Storage library.
  4. Send the file to the user like any other local resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM