上传使用AWS SDK为PHP 2图像 [英] Uploading an Image using AWS SDK for PHP 2
问题描述
这是推动我疯了 - 我一直在研究这个问题,但收效甚微好几天。我终于撞到南墙和需要帮助。很多我已经搜查了文章和论坛都没有对AWSSDK为PHP 2
我们一直在使用Amazon的S3加载通过IOS映像,在过去的几年。
现在,我需要实现上载在浏览器中。
我已经下载了,我们的Ubuntu的服务器上成功安装了AWSSDK为PHP 2。我可以连接到我们的AWS的S3帐户,并显示桶的内容。但我无法将图像水桶。
这是AWS的例外是:
AWS \ S3 \异常\ NotImplementedException:AWS错误code:NotImplemented,状态code:501,AWS请求ID:CEDC4BBAA83CF70C,AWS错误类型:服务器,AWS错误消息:您提供一个头暗示功能,不实现。
下面是我得到了下面的例子code从,标题叫做上传文件到Amazon S3下的网址:<一href="https://github.com/aws/aws-sdk-php#quick-start">https://github.com/aws/aws-sdk-php#quick-start
和我更新了我的code在此基础上: AWS PHP SDK版本2 S3 putObject错误
但它仍然无法正常工作。
下面是我的code:
&LT; PHP
require_once(../ config.php文件); //本地配置瓦尔
require_once('AWSSDKforPHP / aws.phar');
使用AWS \ S3 \ S3Client;
使用AWS \公用\ Enum的\地区;
使用AWS \共同\ AWS;
使用AWS \ S3 \ Enum的\ CannedAcl;
使用AWS \ S3 \异常\ S3Exception;
使用狂饮\ HTTP \ EntityBody;
//获取$ S3对象
$配置=阵列(
'键'=&GT; AMAZON_ACCESS_KEY,
秘密=&GT; AMAZON_ACCESS_SECRET,
'区域'=&GT;地区:: US_EAST_1
);
$ S3 = S3Client ::工厂($配置);
尝试 {
$ bucketname ='my_bucket_name';在S3 //我斗的名字
$文件名='filename.jpg';我的服务器上//我的形象
$ PATH ='http://my.website.com/app/cache/remote'; //在图像所在的路径
$ fullfilename = $路径/$文件名。;
//这个成功列出了斗我感兴趣的内容
的foreach($ S3-&GT; getIterator('ListBuckets')为$桶){
的foreach($ S3-&GT; getIterator('ListObjects',阵列('斗'=&GT; $斗['名称']))为$对象){
如果($桶['名称'] == $ bucketname){
回声$水桶['名称']。 '/'。 $对象['关键']。 PHP_EOL;
}
}
}
//这里我在这里,请!这是code抛出异常
$ S3-&GT; putObject(阵列(
'斗'=&GT; $ bucketname,
'关键'=&GT; $文件名,
身体=&GT; EntityBody ::工厂(FOPEN($ fullfilename,'R')),
ACL=&GT; CannedAcl :: PUBLIC_READ_WRITE,
'的ContentType'=&GT; 为image / jpeg
));
}赶上(S3Exception $ E){
回声$ê;
}
?&GT;
有人请我提供一个例子,所以我可以上传JPG图片到我们的S3存储使用AWSSDK为PHP 2?
解决方案: 从ppostma1的答复,我已经修改了我的code以下,现在它的工作原理:
$ bucketname ='my_bucket_name'; //必须全部小写
$文件名='filename.jpg';我的服务器上//我的形象
$ PATH ='无功/网络/ HTML /根网站的文件夹/图片/'; //物理路径图像所在的位置
$ fullfilename = $ PATH $文件名。
$ S3-&GT; putObject(阵列(
'斗'=&GT; $ bucketname,
'关键'=&GT; $文件名,
身体=&GT; EntityBody ::工厂(FOPEN($ fullfilename,'R')),
ACL=&GT; CannedAcl :: PUBLIC_READ_WRITE,
'的ContentType'=&GT; 为image / jpeg
));
首先,你缺少一个S3文件名又名键:
键''=&GT; /files/imgage/fuzzykitten.jpg
接下来,我有少得多的并发症有:
//使用AWS \ S3 \ S3Client;
使用AWS \公用\ Enum的\地区;
//使用AWS \共同\ AWS;
使用AWS \ S3 \ Enum的\ CannedAcl;
使用AWS \ S3 \异常\ S3Exception;
使用狂饮\ HTTP \ EntityBody;
$亚马逊AWS = \ S3 \ S3Client ::工厂($配置)
与能够找到的类文件。每次我尝试包括./common/aws或./s3/s3client,就开始给我找不到AWS \ S3 \ AWS \ S3Client这让我在重量???
This is driving me crazy -- I have been working on this problem for several days with little success. I've finally hit a brick wall and need help. A lot of the articles and forums that I have searched are not for AWSSDK for PHP 2.
We have been using Amazon's S3 to load images through iOS for the past couple of years.
Now, I need to implement the upload in a browser.
I have downloaded and successfully installed AWSSDK for PHP 2 on our Ubuntu server. I can get connected to our AWS S3 account and display the contents of the buckets. But I am unable to put an image in a bucket.
The Exception from AWS is:
Aws\S3\Exception\NotImplementedException: AWS Error Code: NotImplemented, Status Code: 501, AWS Request ID: CEDC4BBAA83CF70C, AWS Error Type: server, AWS Error Message: A header you provided implies functionality that is not implemented.
Here is the URL that I got the below example code from, under the heading called Uploading a File to Amazon S3: https://github.com/aws/aws-sdk-php#quick-start
And I updated my code based on this: AWS PHP SDK Version 2 S3 putObject Error
But it still doesn't work.
Here is my code:
<?php
require_once("../config.php"); //local config vars
require_once('AWSSDKforPHP/aws.phar');
use Aws\S3\S3Client;
use Aws\Common\Enum\Region;
use Aws\Common\Aws;
use Aws\S3\Enum\CannedAcl;
use Aws\S3\Exception\S3Exception;
use Guzzle\Http\EntityBody;
//get the $s3 object
$config = array(
'key' => AMAZON_ACCESS_KEY,
'secret' => AMAZON_ACCESS_SECRET,
'region' => Region::US_EAST_1
);
$s3 = S3Client::factory($config);
try {
$bucketname = 'my_bucket_name'; //my bucket name on s3
$filename = 'filename.jpg'; //my image on my server
$path = 'http://my.website.com/app/cache/remote'; //the path where the image is located
$fullfilename = $path."/".$filename;
//this successfully lists the contents of the bucket I am interested in
foreach ($s3->getIterator('ListBuckets') as $bucket) {
foreach ($s3->getIterator('ListObjects', array('Bucket' => $bucket['Name'])) as $object) {
if ( $bucket['Name'] == $bucketname ) {
echo $bucket['Name'] . '/' . $object['Key'] . PHP_EOL;
}
}
}
//HERE ME HERE, PLEASE! this is the code that throws the exception
$s3->putObject(array(
'Bucket' => $bucketname,
'Key' => $filename,
'Body' => EntityBody::factory(fopen($fullfilename, 'r')),
'ACL' => CannedAcl::PUBLIC_READ_WRITE,
'ContentType' => 'image/jpeg'
));
} catch (S3Exception $e) {
echo $e;
}
?>
Can someone please provide me with an example so I can upload the JPG image into our bucket on S3 using AWSSDK for PHP 2?
RESOLUTION: From ppostma1's reply, I have modified my code as follows, and it now works:
$bucketname = 'my_bucket_name'; //must be all lowercase
$filename = 'filename.jpg'; //my image on my server
$path = 'var/www/html/root-website-folder/images/'; //the physical path where the image is located
$fullfilename = $path.$filename;
$s3->putObject(array(
'Bucket' => $bucketname,
'Key' => $filename,
'Body' => EntityBody::factory(fopen($fullfilename, 'r')),
'ACL' => CannedAcl::PUBLIC_READ_WRITE,
'ContentType' => 'image/jpeg'
));
First, you are missing a s3 'filename' aka key:
'Key' => '/files/imgage/fuzzykitten.jpg'
Next, I have had far fewer complications with:
//use Aws\S3\S3Client;
use Aws\Common\Enum\Region;
//use Aws\Common\Aws;
use Aws\S3\Enum\CannedAcl;
use Aws\S3\Exception\S3Exception;
use Guzzle\Http\EntityBody;
$amazon = Aws\S3\S3Client::factory($config)
with being able to find the class files. Every time I try to include ./common/aws or ./s3/s3client, it start giving me "cannot find Aws\S3\Aws\S3Client" which leaves me at wt???
这篇关于上传使用AWS SDK为PHP 2图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!