如何降低AWS S3数据传输成本?

问题描述

我有一个AWS S3存储桶,用于存储laravel应用的公共文件和私有文件的组合,如下所示:

AWS存储桶和文件夹:

myapp
    |____private
    |____public

在我的刀片文件中,图像显示如下:

<img src="http://myapp.com/files/10"/>

这是我的路线:

Route::get('files/{id}','FileController@show');

这是我用于显示和上传文件的控制器方法:

public function show(Request $request,$id)
{
    $file = \App\File::findOrFail($id);

    $user = auth()->check() ? auth()->user() : new User;

    $this->authorizeForUser($user,'show',$file);

    return Image::make(Storage::disk('s3')->get($file->path))->response();

} 


public function store(FileRequest $request)
{

    $file = $request->file('file');
    $fileType = strtolower($request->input('file_type'));
    $filePath = $fileType . '/' . Str::random(40) . '.' .  $file->getClientOriginalExtension();
   
    $user = auth()->user();

    DB::beginTransaction();

    try
    {
        $this->authorizeForUser($user,'store',\App\File::class);

        $newFile = \App\File::create([
               "name"          => $file->getClientOriginalName(),"path"          => $filePath,"size"          => $file->getSize(),"mime_type"     => $file->getMimeType(),"type"          => $fileType,"user_id"       => $user->id
        ]);

        $image = Image::make($file->getRealPath())->resize(360,180);
        
        Storage::disk('s3')->put($filePath,$image->stream());
       
        $content = "<textarea data-type=\"application/json\">{\"ok\": true,\"message\": \"Success\",\"path\": \"" . $newFile->path . "\",\"id\": \"" . $newFile->id . "\",\"name\": \"" . $newFile->name . "\" }</textarea>";

        DB::commit();

    } catch (\Exception $e)
    {
        DB::rollback();

        $error =  'There was a problem uploading your file';
        $content = "<textarea data-type=\"application/json\">{\"ok\": false,\"message\": \"" . $error . "\" }</textarea>";

    }

    return response($content);

}

这是文件策略,通过身份验证的用户可以访问公共文件和私有文件,而来宾用户可以 仅访问公共文件:

public function show(User $user,File $file)
{
     if ($file->type == 'public' || (!is_null($user->getKey()) && $file->type == 'private'))
          return true;
     }
}

我想添加缓存支持,因为即使是少数应用程序用户也会在AWS S3上生成数百甚至是大量的GET请求。在存储桶为私有桶(即只能通过IAM用户访问)的情况下,如何缓存文件以降低S3成本?

我已经阅读了一些建议使用Cloudfront的帖子,但这不只是增加了另一笔费用,我如何使用上面具有的控制器方法以及在根级存储桶是私有的情况下实现Cloudfront?

使用laravels文件缓存是否合适?

我想知道我可以使用S3和laravel的哪些技术,最好是一些步骤和指导代码确实会有所帮助。

PS。我已经看过以下帖子。 Laravel AWS S3 Storage image cache

解决方法

最简单的选择是使用CloudFront,如果您的问题是成本,那么当请求转到CloudFront时,AWS S3不会收取数据传输费用。

使用S3设置CloudFront非常重要,您可以将Bucket保持私有状态,只需要创建OAI。

在下面检查这些链接:

https://aws.amazon.com/pt/premiumsupport/knowledge-center/cloudfront-https-requests-s3/

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html

由于CloudFront是一项公共服务,因此您需要将当前身份验证更改为另一种格式(请检查此链接cloudfront-authorization-at-edge),或者可以使用签名URL或签名Cookie来提供私人内容(请检查此其他链接) PrivateContent);并且您可以使用现有的IAM用户来生成这些签名URL /签名Cookie。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...