Accessing private s3 bucket files
Here get_s3_signed_url function returns the url which has access key and it is not recommended. Make a function where it gets the private object object from the bucket and creates a file/an image locally in the server. Use the path of newly created image and delete the image once you are done with it.
Code in Zend:
require_once('Zend/Service/Amazon/S3.php');$awsKey = 'your-key';$awsSecretKey = 'your-secret-key';$s3 = new Zend_Service_Amazon_S3($awsKey, $awsSecretKey);$bucketName = 'your-bucket-name';$objectName = $bucketName . '/image.jpg'; //image path$info = $s3->getInfo($objectName);if (is_array($info)) { header('Content-type: ' . $info['type']); header('Content-length: ' . $info['size']); file_put_contents('image.jpg', file_get_contents($s3->getObject($objectName))); header('Content-Description: File Transfer'); header("Content-Disposition: attachment; filename=\"image.jpg\""); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Expires: 0'); ob_clean(); flush(); readfile('image.jpg'); unlink('image.jpg');} else { header('HTTP/1.0 404 Not Found');}
Code in core php:
require_once('S3.php');$awsKey = 'your-key';$awsSecretKey = 'your-secret-key';$s3 = new S3($awsKey, $awsSecretKey);$bucketName = 'your-bucket-name';** To store/download one image at a time**$objectName = "image.jpg"; //s3 image path$tempFile = "image.jpg"; //temporary/local image path$s3->getObject($bucketName, $objectName, $tempFile); //stores the image if (filesize($tempFile)) { header('Content-Description: File Transfer'); header('Content-Type: image/png'); header("Content-Disposition: attachment; filename=\"" . $tempFile . "\""); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Expires: 0'); header('Content-Length: ' . filesize($tempFile)); ob_clean(); flush(); readfile($tempFile); //downloads the image unlink($tempFile); //deletes the image from local }**To store/download 'n' images at a time**$s3ImagesFolder = 'all_images/'; //folder where all the images are $bucketContents = $s3->getBucket($bucketName);foreach ($bucketContents as $file) {if ((strpos($file['name'], $s3ImagesFolder) > -1)) { $tempFile = end(explode("/", $file['name'])); $s3->getObject($bucketName, $file['name'], $tempFile); // to store //to download if ($file['size']) { header('Content-Description: File Transfer'); header('Content-Type: image/png'); header("Content-Disposition: attachment; filename=\"" . $tempFile . "\""); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Expires: 0'); header('Content-Length: ' . $file['size']); ob_clean(); flush(); readfile($tempFile); //downloads the image unlink($tempFile); //deletes the image from local } }}