【问题标题】:Uploading an Image using AWS SDK for PHP 2使用适用于 PHP 2 的 AWS 开发工具包上传图像
【发布时间】:2013-02-04 20:50:05
【问题描述】:

这让我发疯了——我已经解决这个问题好几天了,但收效甚微。我终于碰壁了,需要帮助。我搜索的很多文章和论坛都不是 AWSSDK for PHP 2。

在过去的几年里,我们一直在使用 Amazon 的 S3 通过 iOS 加载图像。

现在,我需要在浏览器中实现上传。

我已经在我们的 Ubuntu 服务器上下载并成功安装了 AWSSDK for PHP 2。我可以连接到我们的 AWS S3 帐户并显示存储桶的内容。但是我无法将图像放入存储桶中。

AWS 的例外是:
Aws\S3\Exception\NotImplementedException:AWS 错误代码:未实现,状态代码:501,AWS 请求 ID:CEDC4BBAA83CF70C,AWS 错误类型:服务器,AWS 错误消息:您提供的标头暗示未实现的功能。

这是我从中获取以下示例代码的 URL,在名为 Uploading a File to 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'
));

【问题讨论】:

  • 你在使用代理吗?

标签: php image upload amazon-web-services


【解决方案1】:

首先,您缺少一个 s3 'filename' aka key:

'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???

【讨论】:

  • 谢谢,ppostma1。您的回答为我指明了正确的方向。我确实有上面示例中的密钥,但我在测试中犯了 2 个错误。 1) Bucket Name 必须是小写字母。我的产品存储桶是我的开发存储桶,上下浮动(为什么他们让你创建一个你不能使用的存储桶名称?)。 2)我使用 URL 来访问文件名,但从你的帖子中,我意识到我需要使用服务器上的完整物理路径来访问文件。
  • 啊!,当我没有看到路径时,我认为没有钥匙。现在对于任何寻找代理的人来说,附加代码是: $configs = array( 'key' => AWS_KEY, 'secret' => AWS_SECRET_KEY, 'curl.options' => array( CURLOPT_PROXY => 'proxyHostName' , CURLOPT_PROXYPORT => PORT#, CURLOPT_PROXYUSERPWD => 'username:Password', CURLOPT_HTTPHEADER => array("期望:"), CURLOPT_SSL_VERIFYPEER=> FALSE ) ); $amazon = Aws\S3\S3Client::factory($configs);
  • CURLOPT_HTTPHEADER => array("Expect:"),只有“Squid”服务器才需要,如果你有这些,你可能需要 MS 域用户名,即“domainhost\username:密码”
猜你喜欢
  • 1970-01-01
  • 2013-03-21
  • 2018-05-21
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 2023-03-15
  • 1970-01-01
相关资源
最近更新 更多