ivanvermeyen/laravel-google-drive-demo

how to get download link from a file

AhmedRyad22 opened this issue · 5 comments

hi sir.
pls can you show me how to get the download link of an uploaded file from drive ?
is there any method for that ?
i want to download a file directly by this link
thanks in advance <3

or how ot get the share link form a file that would be so useful also

Hi,

Is it possible to get the link with this:

Storage::cloud()->url($path);

You may need to share the file first, but not 100% sure about that.
Full example:

Route::get('share', function() {
$filename = 'test.txt';
// Store a demo file
Storage::cloud()->put($filename, 'Hello World');
// Get the file to find the ID
$dir = '/';
$recursive = false; // Get subdirectories also?
$contents = collect(Storage::cloud()->listContents($dir, $recursive));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($filename, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($filename, PATHINFO_EXTENSION))
->first(); // there can be duplicate file names!
// Change permissions
// - https://developers.google.com/drive/v3/web/about-permissions
// - https://developers.google.com/drive/v3/reference/permissions
$service = Storage::cloud()->getAdapter()->getService();
$permission = new \Google_Service_Drive_Permission();
$permission->setRole('reader');
$permission->setType('anyone');
$permission->setAllowFileDiscovery(false);
$permissions = $service->permissions->create($file['basename'], $permission);
return Storage::cloud()->url($file['path']);
});

thank you so much sir

@ivanvermeyen Hi.

From what I gather from the code you posted, it loads all files in a collection and finds the path by comparing the filename. If I have many files wouldn't that create a problem? (The usage I have in mind could reach more than 80,000 files)

Isn't there another way to get the path (when uploading the file for example)?

Thanks

I think you can if you use the underlying package directly.
https://github.com/nao-pon/flysystem-google-drive

Laravel's Storage::put() method doesn't return the path unfortunately.