AWS S3上传图像获取损坏 [英] AWS S3 uploaded images are getting corrupted
问题描述
我工作的AWS EC2 Ubuntu的机器。我的code是CakePHP中。当我尝试上传任何图片AWS S3是会得到损坏。
I am working on AWS ec2 ubuntu machine. My code is in cakephp. When I try to upload any image to AWS S3 it will get corrupted.
而这是工作的罚款核心PHP code。
while it is working fine in core php code.
这里是我的控制器code
here is my controller code
if ($this->User->saveAll($this->request->data)) {
// upload on s3
//create file name
// echo "<pre>"; print_r($_FILES); die;
$temp = explode(".", $_FILES["data"]["name"]["User"]['image']);
$newfilename = round(microtime(true)) . '.' . end($temp);
$filepath = $_FILES['data']['tmp_name']['User']['image'];
$id = $this->request->data['User']['id'];
try {
$result = $this->Amazon->S3->putObject(array(
'Signature' => 'v4',
'Bucket' => 'abc.sample',
'ACL' => 'authenticated-read',
'Key' => 'files/user/image/' . $id . "/" . $newfilename,
'ServerSideEncryption' => 'aws:kms',
'SourceFile' => $filepath,
'Body' => $filepath,
'ContentType' => $_FILES['data']['type']['User']['image'],
));
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
}
还有一件事,如果我没有使用体
参数,那么它显示了我下面的错误
One more thing if I didn't use body
parameter then it is showing me following error
您必须指定机构或一个的SourceFile非空值 参数。
You must specify a non-null value for the Body or SourceFile parameters.
在以下code工作正常进行的测试中核心的PHP
While following code is working fine for test in core php
$filepath = "/var/www/html/for_testing_aws/assets/img/avtar.png";
try {
$result = $s3->putObject(array(
'Bucket' => $bucketName,
'ACL' => 'authenticated-read',
'Key' => "avtar-auth.png",
'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";
}
我创建一个自定义组件访问SDK的所有功能。与 https://github.com/Ali1/cakephp-amazon-aws-sdk参考。
检查此
适当节省我的EC2存储图像。图像EC2服务器上上传我使用这个插件 https://github.com/josegonzalez/cakephp-upload
Images saving properly on my ec2 storage. for image uploading on ec2 server I am using this plugin https://github.com/josegonzalez/cakephp-upload
我尝试 putobject
用简单的形式上传的做法,也为我工作
这里是code
I try putobject
with simple form uploading approach which also work for me
here is the code
require 'aws-autoloader.php';
$credentials = new Aws\Credentials\Credentials('key 1', 'key2');
$bucketName = "";
$s3 = new Aws\S3\S3Client([
// 'signature' => 'v4',
'version' => 'latest',
'region' => 'ap-southeast-1',
'credentials' => $credentials,
'http' => [
'verify' => '/home/ubuntu/cacert.pem'
],
'Statement' =>[
'Action '=> "*",
],
// 'debug' => [
// 'logfn' => function ($msg) {
// echo $msg . "\n";
// },
// 'stream_size' => 0,
// 'scrub_auth' => true,
// 'http' => true,
// ]
]);
$result = $s3->listBuckets();
foreach ($result['Buckets'] as $bucket) {
// Each Bucket value will contain a Name and CreationDate
$bucketName = $bucket['Name'];
}
<form name="uploadimage" id="uploadimage" method="post" action="saveimg.php" enctype="multipart/form-data">
<input type="file" name="file" value="file"/>
<input type="submit" name="submit" value="submit" />
</form>
和saveimg.php是
and saveimg.php is
$filepath = $_FILES['file']['tmp_name'];
try {
$result = $s3->putObject(array(
'Bucket' => $bucketName,
'ACL' => 'authenticated-read',
'Key' => $_FILES['file']['name'],
'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";
}
当我试图打开下面的消息,文件显示。
When I try to open that file following message has shown.
推荐答案
尝试阅读您的文件 - 要传递的路径,而不是文件内容:
Try reading your file - you are passing the path and not the file contents:
'Body' => $filepath,
应
'Body' => file_get_contents($filepath),
这篇关于AWS S3上传图像获取损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!