首页 > 解决方案 > 从谷歌驱动器(文件系统)或(隐藏链接中的 api 密钥)下载 Laravel

问题描述

第一种下载方式:

  1. https://www.googleapis.com/drive/v3/files/1YIH4zfM0P1xa-_mfZNGiIY8qZrIEt-rF/?key=API_KEY&alt=media

将我的 API KEY 放入链接中,我可以直接从我的谷歌驱动器下载文件。但我不想将我的 API_KEY 提供给用户。

2.我也可以通过其他方式访问我的驱动器:( using nao-pon/flysystem-google-drive)

Route::get('/download/{rest?}', function ($rest) {
    $metadata = Storage::cloud()->getMetadata($rest);
    $readStream = Storage::cloud()->getDriver()->readStream($rest);

    return response()->stream(function () use ($readStream) {
        fpassthru($readStream);
    }, 200, [
        'Content-Type' => $metadata['mimetype'],
        'Content-disposition' => 'attachment; filename="'.$metadata['name'].'"', // force download?
    ]);
})->where('rest','(.*)');

这样我就不必使用 api_key 但服务器必须下载整个文件,它是一个流,但仍然使用服务器资源。

另一方面https://www.googleapis.com/drive/v3/files/1YIH4zfM0P1xa-_mfZNGiIY8qZrIEt-rF/?key=API_KEY&alt=media需要内容类型和文件名,因为它没有。而且我不知道如何隐藏 api 密钥。

那么你有什么建议。有没有其他方法不响应()->流不通过服务器逐流下载整个文件,然后将其发送给用户?多个用户下载文件会占用所有带宽,所以下载速度下降如此之快。

标签: laravelgoogle-drive-apiapi-key

解决方案


推荐阅读