我已经决定利用amazon新的服务器端加密与s3,但是,我遇到了一个问题,我无法解决。
我使用s3 PHP类在这里找到:https://github.com/tpyo/amazon-s3-php-class
我原来一直使用这段代码来放置对象(它是工作的):
S3::putObjectFile($file, $s3_bucket_name, $file_path, S3::ACL_PRIVATE,
array(),
array(
"Content-Disposition" => "attachment; filename=$filename",
"Content-Type" => "application/octet-stream"
)
);
然后我按照这里的指示做了:http://docs.amazonwebservices.com/AmazonS3/latest/API/index.html?RESTObjectPUT.html并添加了'x-amz-server -encryption'请求头。但是现在,当我尝试放置一个对象时,它会失败,没有错误。
我的新代码是: S3::putObjectFile($file, $s3_bucket_name, $file_path, S3::ACL_PRIVATE,
array(),
array(
"Content-Disposition" => "attachment; filename=$filename",
"Content-Type" => "application/octet-stream",
"x-amz-server-side-encryption" => "AES256"
)
);
有人尝试过这个新功能吗?或者有人看到代码中的错误吗?
欢呼。
标题应该是$metaHeaders
数组的一部分,而不是$requestHeaders
数组的一部分。
S3::putObjectFile($file, $s3_bucket_name, $file_path, S3::ACL_PRIVATE,
array(
"x-amz-server-side-encryption" => "AES256"
),
array(
"Content-Disposition" => "attachment; filename=$filename",
"Content-Type" => "application/octet-stream"
)
);
下面是来自文档的方法定义:
putObject (mixed $input,
string $bucket,
string $uri,
[constant $acl = S3::ACL_PRIVATE],
[array $metaHeaders = array()],
[array $requestHeaders = array()])
你也可以考虑使用SDK的PHP?
我们可以使用下面的代码上传加密文件$s3->create_object($bucket_name,$destination,array(
'acl'=>AmazonS3::ACL_PUBLIC,
'fileUpload' => $file_local,
'encryption'=>"AES256"));
你可以从这里下载最新的sdk
使用官方SDK:
use Aws'S3'S3Client;
$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk
$filepath = '*** Your File Path ***';
// Instantiate the client.
$s3 = S3Client::factory();
// Upload a file with server-side encryption.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ServerSideEncryption' => 'AES256',
));
更改现有对象的服务器端加密(复制操作)
use Aws'S3'S3Client;
$sourceBucket = '*** Your Source Bucket Name ***';
$sourceKeyname = '*** Your Source Object Key ***';
$targetBucket = '*** Your Target Bucket Name ***';
$targetKeyname = '*** Your Target Object Key ***';
// Instantiate the client.
$s3 = S3Client::factory();
// Copy an object and add server-side encryption.
$result = $s3->copyObject(array(
'Bucket' => $targetBucket,
'Key' => $targetKeyname,
'CopySource' => "{$sourceBucket}/{$sourceKeyname}",
'ServerSideEncryption' => 'AES256',
));
来源:http://docs.aws.amazon.com/AmazonS3/latest/dev/SSEUsingPHPSDK.html
使用laravel 5+可以通过filesystems.php配置轻松完成,您不需要获取驱动程序或低级对象。
's3' => [
'driver' => 's3',
'key' => "Your Key",
'secret' => "Your Secret",
'region' => "Bucket Region",
'bucket' => "Bucket Name",
'options' => [
'ServerSideEncryption' => 'AES256',
]
],
//Code
$disk->put("filename", "content", "public"); // will have AES for file