下载 Laravel 中的 S3 文件链接

Download S3 file links in Laravel

我正在尝试在 VueJs 中构建一个小型应用程序作为前端,在 Laravel 中作为后端,我将管理部分中的文件上传到我的 aws-s3,同时上传我将该文件的 link 存储在数据库中。每个操作都由 api 调用维护,现在我想为我的最终用户提供这些下载的选项,所以我正在像这样进行 axios 调用:

downloadPDF(docs){
    const documents = {
        document: docs
    }
    axios.post('api/documents-download', documents, {headers: getHeader()}).then(response => {
        if(response.status === 200)
        {
            console.log('Downloaded')
        }
    })
},

在我的控制器中我有这样的东西:

public function download(Request $request)
{
    $headers = [
        'Content-Type' => 'application/pdf',
        'Content-Description' => 'File Transfer',
        'Content-Disposition' => "attachment; filename=filename.pdf",
    ];

    return response()->download($request->document, 'filename.pdf', $headers);
}

但它向我抛出错误:

The file "https://s3-us-west-2.amazonaws.com/noetic-dev/2_Project/shiven-affordable-housing-surat/3_Document/Form+1/Form1.pdf" does not exist

此文件显然存在并制作成 public 正如您在上面看到的 url 显示文档为 linked.

帮我解决这个问题。谢谢

我记得在我的项目中实现了这个.. 让我和你分享一个示例代码...... Laravel 已经在 documentation

中提供了有关 s3Storage 的详细信息

代码:

use Illuminate\Support\Facades\Response as Download;

public function download_config(Config $config)
    {
        $headers = [
            'Content-Type'        => 'Content-Type: application/zip',
            'Content-Disposition' => 'attachment; filename="'. $config->name .'"',
        ];

        return Download::make(Storage::disk('s3')->get($config->path), Response::HTTP_OK, $headers);
    }

我假设您可能将 $config->path(文件路径)存储在您的数据库中。 要了解更多信息,您可以访问 this

此代码非常适合从 Laravel 7 中的 S3 下载:

// $filePath should look like this: some-directory/filename.zip
return redirect(Storage::disk('s3')->temporaryUrl(
                    $filePath,
                    now()->addHour(),
                    ['ResponseContentDisposition' => 'attachment']
                ));

感谢:https://sutherlandboswell.com/force-file-download-from-aws-s3-in-laravel/