du22399 2012-07-18 16:22
浏览 43
已采纳

Amazon S3图像无法正确上传

I'm currently working on coding up a back end page for my site to allow admins to indirectly edit database entries and upload photos. I'm using Amazon S3 to host images to allow them to load faster, so when I upload images I have to first upload them to the server and then from there send them to the Amazon server. The images are correctly being uploaded to the server, but they aren't getting to Amazon's servers correctly. When I try to access the Amazon copy it seems to not exist. Here is the code I am using to upload images:

//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";
} 

And examples of my error. The local working copy and the Amazon copy. I've never worked with Amazon before so I have no idea what the problem is, how to figure out what it is, or how to solve it. Any help on any of those 3 points would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douyan8772 2012-07-18 20:08
    关注

    If the put operation fails, you should be getting an exception. Without knowing what the exception is, it is difficult to solve the problem.

    At least one of the issues i can see, is that putObject() expects the data to be a string (blob) or resource. Perhaps this will do the trick:

    $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"
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题