当我的s3的x-amz-server-side-encryption是Laravel 5.1的AES256时,如何使用Storage :: disk('s3') - > put()?

时间:2015-08-06 08:24:29

标签: laravel amazon-web-services amazon-s3 storage

RT。

这是我的s3文件系统配置:

's3' => [
        'driver' => 's3',
        'key'    => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

这是我的composer.json:

"require": {
        "laravel/framework": "5.1.*",
        "barryvdh/laravel-ide-helper": "~2.0",
        "predis/predis": "~1.0",
        "guzzlehttp/guzzle": "~5.0",
        "league/flysystem-aws-s3-v3": "~1.0",
        "raven/raven": "0.12.*"
    },

这是我的s3的存储策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "DenyUnEncryptedObjectUploads",
            "Effect": "Deny",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::*****bucket_name*****/*",
            "Condition": {
                "StringNotEquals": {
                    "s3:x-amz-server-side-encryption": "AES256"
                }
            }
        }
    ]
}

是的,我使用"s3:x-amz-server-side-encryption": "AES256"作为PutObject的条件,但我想像这段代码一样使用:

Storage::disk('s3')->put('test.log','123');

但是当我运行它时,我会得到这样的回应:

[Aws\S3\Exception\S3Exception]                                                                                                                                    
Error executing "HeadObject" on "https://s3-ap-northeast-1.amazonaws.com/****bucket_name****/test.log"; AWS HTTP error: Client error response [url]https://s3-ap-northeast-1.amazonaws.com/****bucket_name****/test.log [status code] 403 [reason phrase] Forbidden  (client): 403 Forbidden (Request-ID: 39C30C8512E5ED16) -

[GuzzleHttp\Exception\ClientException]                                                                                                                     
Client error response [url] https://s3-ap-northeast-1.amazonaws.com/****bucket_name****/test.log [status code] 403 [reason phrase] Forbidden

那么,我该怎么做呢? 谢谢!

2 个答案:

答案 0 :(得分:4)

(Laravel 5.3)如果您的存储桶策略要求对所有对象进行服务器端加密,而不是访问S3驱动程序并将参数传递给它,我可以通过将其设置为通用来启用S3 SSE配置中的一个选项:

配置/ filesystems.php

...

's3' => [
                'driver' => 's3',
                'key'    => env('AWS_ACCESS_KEY_ID'),
                'secret' => env('AWS_SECRET_ACCESS_KEY'),
                'region' => env('AWS_REGION'),
                'bucket' => env('AWS_S3_BUCKET'),
                'options' => [
                    'ServerSideEncryption' => 'AES256',
                ]
            ],

...

在配置中设置ServerSideEncryption选项后,我可以直接在“磁盘”上进行方法调用。

$s3 = Storage::disk('s3');
$s3->putFileAs($prefix, new File($path), $filename);

答案 1 :(得分:1)

如果您检索S3驱动程序,则可以提供参数:

Storage::disk('s3')->getDriver()->put('filename', 'randomdata', ['ServerSideEncryption' => 'AES256']);