通过url获取图像时,AWS S3访问被拒绝

时间:2015-09-02 17:26:25

标签: php amazon-web-services amazon-s3 amazon-ec2 aws-sdk

我正在开发 AWS EC2 Ubuntu计算机并尝试从 AWS S3 获取图片,但每次都向我显示以下错误。

<Error>
<Code>InvalidArgument</Code>
<Message>
Requests specifying Server Side Encryption with AWS KMS managed keys require AWS Signature Version 4.
</Message>
<ArgumentName>Authorization</ArgumentName>
<ArgumentValue>null</ArgumentValue>
<RequestId>7C8B4BF1CE2FDC9E</RequestId>
<HostId>
/L5kjuOET4XFgGter2eFHX+aRSvVm/7VVmIBqQE/oMLeQZ1ditSMZuHPOlsMaKi8hYRnGilTqZY=
</HostId>
</Error>

这是我的存储桶政策

{
 "Version": "2012-10-17",
 "Id": "Policy1441213815928",
 "Statement": [
  {
   "Sid": "Stmt1441213813464",
   "Effect": "Allow",
   "Principal": "*",
   "Action": "s3:GetObject",
   "Resource": "arn:aws:s3:::mytest.sample/*"
  }
 ]
}

enter image description here

以下是代码

require 'aws-autoloader.php';

$credentials = new Aws\Credentials\Credentials('key', 'key');
$bucketName = "mytest.sample";
$s3 = new Aws\S3\S3Client([
    'signature' => 'v4',
    'version' => 'latest',
    'region' => 'ap-southeast-1',
    'credentials' => $credentials,
    'http' => [
        'verify' => '/home/ubuntu/cacert.pem'
    ],
    'Statement' => [
        'Action ' => "*",
    ],

  ]);

$result = $s3->getObject(array(
'Bucket' => $bucketName,
'Key' => 'about_us.jpg',
    ));

HTML

<img src="<?php echo $result['@metadata']['effectiveUri']; ?>" />
  迈克尔的

编辑 - sqlbot:这里我使用默认的KMS。

   try {
        $result = $this->Amazon->S3->putObject(array(
            'Bucket' => 'mytest.sample',
            'ACL' => 'authenticated-read',
            'Key' =>  $newfilename,
            'ServerSideEncryption' => 'aws:kms',
            'SourceFile' => $filepath,
            'ContentType' => mime_content_type($filepath),
            'debug' => [
                'logfn' => function ($msg) {
                    echo $msg . "\n";
                },
                'stream_size' => 0,
                'scrub_auth' => true,
                'http' => true,
            ],
        ));
    } catch (S3Exception $e) {
        echo $e->getMessage() . "\n";
    }

如果您需要更多信息,请与我们联系。

2 个答案:

答案 0 :(得分:4)

PHP sdk v2

  1. 凭据包是Aws\Common\Credentials
  2. 创建S3Client您需要工厂
  3. 尝试这样的事情

    use Aws\S3\S3Client;
    use Aws\Common\Credentials\Credentials;
    
    $credentials = new Credentials('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');
    
    // Instantiate the S3 client with your AWS credentials
    $s3Client = S3Client::factory(array(
        'signature' => 'v4',
        'region' => 'ap-southeast-1',
        'credentials' => $credentials,
        .....
      ]);
    )
    

    如果这不起作用,您可能会尝试明确声明SignatureV4对象

    use Aws\S3\S3Client;
    use Aws\Common\Credentials\Credentials;
    use Aws\Common\Signature\SignatureV4;
    
    $credentials = new Credentials('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');
    
    // Instantiate the S3 client with your AWS credentials
    $s3Client = S3Client::factory(array(
        'signature' => new SignatureV4(),
        'region' => 'ap-southeast-1',
        'credentials' => $credentials,
        .....
      ]);
    )
    

    如果您升级到sdk v3

    1. 申报s3客户端时,您需要signature_version(而不是signature)作为参数
    2. Statement似乎不是有效参数(http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/configuration.html#signature-version
    3. 如果问题可以打开debug参数以获得更多输出
    4. 这看起来像这样

      $s3 = new Aws\S3\S3Client([
          'signature_version' => 'v4',
          'version' => 'latest',
          'region' => 'ap-southeast-1',
          'credentials' => $credentials,
          'http' => [
              'verify' => '/home/ubuntu/cacert.pem'
          ],
          'debug'   => true
      
        ]);
      

      请点击此处查看可用参数的完整列表

答案 1 :(得分:1)

我还使用aws:kms encyrption密钥来解决此问题,我建议如果您想使用kms密钥,则必须在kms key中创建IAM section of AWS Console。我喜欢推荐AES256服务器端加密,这里S3在获取对象时自动加密和解密数据。请通过以下链接: S3 Server Side encryption with AES256

我的解决方案是更改此行'ServerSideEncryption' => 'aws:kms' with 'ServerSideEncryption' => 'AES256'

 try {
    $result = $this->Amazon->S3->putObject(array(
        'Bucket' => 'mytest.sample',
        'ACL' => 'authenticated-read',
        'Key' =>  $newfilename,
        'ServerSideEncryption' => 'AES256',
        'SourceFile' => $filepath,
        'ContentType' => mime_content_type($filepath),
        'debug' => [
            'logfn' => function ($msg) {
                echo $msg . "\n";
            },
            'stream_size' => 0,
            'scrub_auth' => true,
            'http' => true,
        ],
    ));
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

请同时使用以下json更新您的存储分区政策,这会阻止您上传没有AES256加密的对象

{
        "Sid": "DenyUnEncryptedObjectUploads",
        "Effect": "Deny",
        "Principal": "*",
        "Action": "s3:PutObject",
        "Resource": "arn:aws:s3:::yourbucketname/*",
        "Condition": {
            "StringNotEquals": {
                "s3:x-amz-server-side-encryption": "AES256"
            }
        }
    }