我目前在我的Laravel应用程序中有一个设置,其中有一个MySQL表文档,用于存储元数据和存储在S3存储桶中的对象的链接。我拥有存储在 .env 文件中的IAM用户的AdministratorAccess凭据。
我关注this tutorial以设置文件上传,因为要上传的文件非常大。
直接转S3文件上传表单如下:
<form method="post"
action="https://{{ config('filesystems.disks.s3.bucket') }}.s3.amazonaws.com"
enctype="multipart/form-data">
<input type="hidden" name="AWSAccessKeyId" value="{{ config('filesystems.disks.s3.key') }}">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="key" value="{{ $document->user_id }}/{{ $document->id }}_${filename}">
<input type="hidden" name="policy" value="{{ $policy }}">
<input type="hidden" name="success_action_redirect" value="{{ url('/document_upload') }}">
<input type="hidden" name="signature" value="{{ $signature }}">
<input type="file" name="file">
<button type="submit" class="btn btn-success">Upload document</button>
</form>
在我的Laravel应用程序中,文档属于用户。我在文件上传前添加了user_id的文件夹,并在文件名前加上文档的id。我想让我的Laravel应用程序中的用户只能访问其用户ID文件夹中的文件。
上传文件后,它会重定向到此控制器操作,我将来自S3的数据存储在我的MySQL文档表中:
/**
* Upload file to be attached to document (redirected after AWS upload)
*
* @return \Illuminate\Http\Response
*/
public function document_upload(Request $request){
// check to make sure user is an admin
$request->user()->authorizeRoles('admin');
// bucket, key, etag
$awsResponse = request()->all();
// documents stored as: {{ $document->user_id }}/{{ $document->id }}_${filename}
$filename = explode("/", $awsResponse['key'])[1];
$document_id = explode("_", $filename)[0];
// Lookup document and add fields
$document = Document::find($document_id);
$document['file_path'] = $filename;
$document['aws_key'] = $awsResponse['key'];
$document['aws_bucket'] = $awsResponse['bucket'];
$document['etag'] = $awsResponse['etag'];
// set form fields
return view('admin.document', compact('document', 'awsResponse'));
}
只有管理员才能上传文件。第一行检查授权状态,然后将S3上载的信息添加到文档表中。
当我重新呈现文档页面时,我想显示该文件,但是我被拒绝访问。这是我正在使用的刀片模板,显示访问被拒绝错误:
<embed src="https://s3-us-west-2.amazonaws.com/myBucketName/{{ $document->user_id}}/{{ $document->file_path }}" style="width:800px; height:800px;" frameborder="0">
<a target='_blank' href='https://s3-us-west-2.amazonaws.com/myBucketName/{{ $document->user_id}}/{{ $document->file_path }}'>Download Link</a>
我需要设置哪种存储桶策略或身份验证系统才能在页面上显示文件夹中的文件?我有一个授权系统,用户可以登录,用户可以拥有角色。具有管理员角色的用户可以上载文件。具有客户端角色的用户只能查看名称等于其用户ID的文件夹中的文件。
如何允许已登录Laravel应用程序的用户查看文件?