rakk7 / directo
一个库,用于生成签名,以便直接上传到Amazon S3
Requires
- php: >=5.5.0
Requires (Dev)
- php: >=7.1.0
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ~7.0
This package is auto-updated.
Last update: 2024-09-12 18:28:50 UTC
README
一个库,用于生成AWS Signature V4和补充表单输入,以便直接上传到Amazon S3。同时提供Laravel的桥梁。
安装
$ composer require kfirba/directo
先决条件 - CORS & Bucket Policy
为了确保上传成功并且不被Amazon S3拒绝,我们需要配置CORS选项和bucket策略。
要更新CORS和bucket策略,请登录您的AWS账户并转到S3。现在选择您的bucket,点击右上角的属性。点击权限。
要更新CORS配置,点击编辑CORS配置按钮。我推荐以下CORS配置
<?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <AllowedMethod>POST</AllowedMethod> <AllowedMethod>PUT</AllowedMethod> <MaxAgeSeconds>3000</MaxAgeSeconds> <AllowedHeader>*</AllowedHeader> </CORSRule> </CORSConfiguration>
要更新bucket策略,点击编辑bucket策略按钮。我推荐以下bucket策略
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": "*", "Action": [ "s3:PutObject", "s3:PutObjectAcl" ], "Resource": "arn:aws:s3:::BUCKET_NAME/*" } ] }
别忘了将BUCKET_NAME
改为您bucket的名称。
注意:如果您不打算使用除private
之外的其他ACL上传任何文件,您可能希望省略"s3:PutObjectAcl"
。
Laravel用户
如果您是Laravel用户,该软件包包含一个为您提供的桥梁。
更新您的app.php
中的providers
数组并添加
Kfirba\Directo\Support\DirectoServiceProvider::class,
此外,更新您的aliases
数组并添加
'Directo' => Kfirba\Directo\Support\Facades\Directo::class,
如果您需要更改一些默认设置,您可以通过发布directo.php
配置来使更改在每次尝试生成签名时全局生效,或者使用Directo提供的方便的setter。
$ php artisan vendor:publish --tag=directo
现在您应该能够在config/
目录中找到一个包含所有默认选项的directo.php
文件。
// You can also change the options on-the-fly Directo::setOptions(['acl' => 'private'])->inputsAsArray();
即时更改非常有用,特别是当您的文档中可能有多个表单,而其中一个可能需要与另一个不同的选项时。例如,一个用于上传某些私有指标数据的表单,您可能想要将acl
设置为private
,而另一个用于上传个人照片的表单,您可能希望将acl
设置为至少public-read
。
该软件包读取在config/filesystems.php
下指定的disks.s3
数组中的S3配置。要更改配置,您可以在您的.env
文件中更改以下键
S3_KEY=myS3Key
S3_SECRET=myS3Secret
S3_REGION=eu-central-1
S3_BUCKET=bucket
Laravel将读取这些环境变量,并将配置设置在filesystems.disks.s3
中,这是Directo使用的。
用法
use Kfirba\Directo\Directo; require_once __DIR__ . "/vendor/autoload.php"; $directo = new Directo('bucket', 'region', 'key', 'secret', $options = []);
然后,使用我们刚刚创建的对象,我们可以生成表单的URL和所有需要的隐藏输入。
<form action="<?php echo $directo->formUrl()?>" method="post" enctype="multipart/form-data"> <?php echo $directo->inputsAsHtml() ?> <input type="file" name="file"> </form>
Laravel用户用法
如果您使用laravel,可以使用Directo
外观。您还可以在任何自动解析的类中类型提示Directo
类,例如任何Controller
,它将自动解析。
// Facade: Directo::formUrl(); Directo::inputsAsHtml(); // Type-hinted // SomeController.php: use Kfirba\Directo\Directo; // ... public function index(Directo $directo) { dd($directo->signature()); }
签名策略
有时,在文件上传插件(如FineUploader)的使用中,该插件会将策略发送到服务器以进行签名。一旦策略被签名,插件将提交策略和签名作为AWS认证密钥。
Directo还提供了签名给定策略的能力。策略应该是有效的json编码策略或base64编码策略(解码为有效的json编码策略)。
如果您已经有一个Directo
对象,您只需调用sign()
方法即可
$directo->sign($policy); // => // ['policy' => 'base64-encoded policy...', 'signature => 'the signature...']
如果您没有 Directo
对象,您可以选择创建一个新的对象或使用底层的 Signature
对象。
$signature = new Signature($secret, $region, $policy); $signature->sign($policy); // => // ['policy' => 'base64-encoded policy...', 'signature => 'the signature...']
Signature
对象所需的参数是生成 SigningKey
所必需的。该 Signature
对象将解析给定的策略,并提取生成 SigningKey
所需的任何附加参数。
如果您是 Laravel 用户,您可以在任何地方使用 Directo
面具。
Directo::sign($policy); // => // ['policy' => 'base64-encoded policy...', 'signature => 'the signature...']
sign()
方法返回一个包含以下键的数组:
policy
- 策略的 base64 编码字符串。signature
- 上述策略的签名。
选项
可用的选项有
例如
$directo = new Directo("bucket", "region", "key", "secret", [ 'acl' => 'private', 'max_file_size' => 10, 'additional_inputs' => [ 'Content-Disposition' => 'attachment' ] ]);
可用方法
点赞 👍
感谢 Edd Turtle 的 优秀文章。本软件包的目的是使签名生成过程更加模块化,从而提供更多功能,并为 Laravel 用户建立桥梁。
许可证
Directo 是开源软件,使用 MIT 许可证 许可。