I know how to create a share link
in google drive for each image manually and then use it in an img src element
for display but I want to know if there is a way to automate this with laravel.
I've been reading that one approach seems to be to download the image from drive and convert it into base64, send it to the frontend and display it but I want to know if there is a more convenient way. Can the google api for example create a display link
on its own, which would negate the need of converting the image to base64?
I'm using masbug/flysystem-google-drive-ext adapter for Laravel 9
. My GoogleDriveServiceProvider.php
looks like this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Storage;
use Google\Client;
use Google\Service\Drive;
use Masbug\Flysystem\GoogleDriveAdapter;
use League\Flysystem\Filesystem;
use Illuminate\Filesystem\FilesystemAdapter;
class GoogleDriveServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
try {
Storage::extend('google', function($app, $config) {
$options = [];
if (!empty($config['teamDriveId'] ?? null)) {
$options['teamDriveId'] = $config['teamDriveId'];
}
$client = new Client();
$client->setClientId($config['clientId']);
$client->setClientSecret($config['clientSecret']);
$client->refreshToken($config['refreshToken']);
$service = new Drive($client);
$adapter = new GoogleDriveAdapter($service, $config['folder'] ?? '/', $options);
$driver = new Filesystem($adapter);
return new FilesystemAdapter($driver, $adapter);
});
} catch(\Exception $e) {
return $e->getMessage();
}
}
}
which gives me the ability to use google drive
with the Storage
facade like so:
$imageId = Storage::disk('google')->put('', $request->image);
With a local filesystem I would do:
$url = Storage::url($imageId);
but it only concats the imageId
from google drive with a /storage
string:
/storage/bmca03toolPziaMxlGNZluP2Svgg0Jvwd7hJAycA.png
which can't be used in an img src element
. Is there a way to generate the url
from the google drive api?