Reputation: 882
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.
Upvotes: 1
Views: 1565
Reputation: 675
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"
)
);
Upvotes: 1