这让我发疯了 - 我已经在这个问题上工作了好几天而收效甚微。我终于打了一堵砖墙,需要帮助。我搜索过的很多文章和论坛都不适用于PHP 2的AWSSDK。
过去几年,我们一直在使用亚马逊的S3通过iOS加载图像。
现在,我需要在浏览器中实现上传。
我已经在Ubuntu服务器上下载并成功安装了PHP 2的AWSSDK。我可以连接到我们的AWS S3帐户并显示存储桶的内容。但我无法将图像放入桶中。
AWS的例外情况是:
Aws \ S3 \ Exception \ NotImplementedException:AWS错误代码:未实现,状态代码:501,AWS请求ID:CEDC4BBAA83CF70C,AWS错误类型:服务器,AWS错误消息:您提供的标头意味着未实现的功能。
以下是我从以下文档中获取以下示例代码的URL:“将文件上载到Amazon S3:https://github.com/aws/aws-sdk-php#quick-start
我根据此更新了我的代码:AWS PHP SDK Version 2 S3 putObject Error
但它仍然无效。
这是我的代码:
<?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;
}
?>
有人可以请我提供一个示例,以便我可以使用AWSSDK for PHP 2将JPG图像上传到S3上的存储桶中吗?
解决方案: 从ppostma1的回复中,我修改了我的代码如下,它现在有效:
$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'
));
答案 0 :(得分:5)
首先,你缺少一个s3'文件名'又名密钥:
'Key' => '/files/imgage/fuzzykitten.jpg'
接下来,我的并发症少得多:
//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)
能够找到类文件。每当我尝试包含./common/aws或./s3/s3client时,它就开始给我“找不到Aws \ S3 \ Aws \ S3Client”,这让我在wt ???