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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图