Amazon S3图像无法正确上传

时间:2012-07-18 16:22:58

标签: php zend-framework amazon-s3 image-uploading

我正在为我的网站编写后端页面编码,以允许管理员间接编辑数据库条目和上传照片。我正在使用Amazon S3托管图像以允许它们加载更快,因此当我上传图像时,我必须先将它们上传到服务器,然后从那里将它们发送到Amazon服务器。图像正确上传到服务器,但它们没有正确到达亚马逊的服务器。当我尝试访问Amazon副本时,它似乎不存在。这是我用来上传图片的代码:

//Set up image validator
$upload = new Zend_File_Transfer();
$upload->addValidator('ImageSize', false, array('minwidth'  => 100,
                                                  'maxwidth'  => 1000,
                                                  'minheight' => 100,
                                                  'maxheight' => 1000), 'image')
       ->addValidator('Extension', false, 'jpg')
       ->addValidator('Count', false, array('min'=>0, 'max'=>2));

//Set up Amazon class
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);

if($upload->isUploaded('image')){ //Pic was uploaded
    if($upload->isValid('image')){ //Pic is valid
        echo "Pic provided is valid.";          
        $upload->addFilter('Rename',array('target'=>BASE_PATH . "/public_html/items/{$item_id}_nsa.jpg",'overwrite'=>true));
        $upload->receive();
        $pic = BASE_PATH . "/public_html/items/{$item_id}_nsa.jpg";
        try{
            $s3->putObject("media.completeset.com/images/items/{$item_id}_nsa.jpg", $pic,
                                        array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ,
                                              Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER => "image/jpeg"));
            echo "Pic is uploaded.";
        }
        catch(Exception $e){
            echo "Exception: ".$e->getMessage();
        }
    }
    else{
        echo "Pic is invalid.";
    }
}
else{
    echo "Pic isn't uploaded";
} 

我的错误的例子。 The local working copythe Amazon copy.我之前从未与亚马逊合作过,因此我不知道问题是什么,如何弄清楚它是什么,或者如何解决它。任何这三点的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果put操作失败,您应该获得异常。在不知道异常是什么的情况下,很难解决问题。

我能看到的至少一个问题是putObject()期望数据是字符串(blob)或资源。也许这会解决问题:

$s3->putObject(
    "media.completeset.com/images/items/{$item_id}_nsa.jpg", 
    file_get_contents($pic),
    array(
      Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ,
      Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER => "image/jpeg"
    )
);