Jump to content

Anyone familiar with Amazon S3 image hosting?


galvin

Recommended Posts

I am using the great script from this site  (http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/comment-page-1/#comments) and I have everything working properly (i.e. I can upload images from a form on my site into my S3 account).

 

My plan was that a new "bucket" would be created by each user who visits my site.  Then I found out S3 limits accounts to only 100 buckets :(.  So my original plan is no longer an option. I will  have to use basically ONE bucket and just carefully name the image files.

 

Now my problem is figuring how to retrieve only certain images from what is going to be a HUGE bucket of images.  Below is the code that grabs ALL images from a bucket and loops through them. 

 

Does anyone familiar with S3 and/or this script know how I could tell it to, for example, only retrieve images with a filename that starts with "11111"? 

 

<?php  
// Get the contents of our bucket  
$bucket_contents = $s3->getBucket("jurgens-nettuts-tutorial");  
  
foreach ($bucket_contents as $file){  
  
    $fname = $file['name'];  
    $furl = "http://jurgens-nettuts-tutorial.s3.amazonaws.com/".$fname;  
  
    //output a link to the file  
    echo "<a href=\"$furl\">$fname</a><br />";  
}  
?>  

 

 

/*
* Get contents for a bucket
*
* If maxKeys is null this method will loop through truncated result sets
*
* @param string $bucket Bucket name
* @param string $prefix Prefix
* @param string $marker Marker (last file listed)
* @param string $maxKeys Max keys (maximum number of keys to return)
* @return array | false
*/
public static function getBucket($bucket, $prefix = null, $marker = null, $maxKeys = null) {
	$rest = new S3Request('GET', $bucket, '');
	if ($prefix !== null && $prefix !== '') $rest->setParameter('prefix', $prefix);
	if ($marker !== null && $prefix !== '') $rest->setParameter('marker', $marker);
	if ($maxKeys !== null && $prefix !== '') $rest->setParameter('max-keys', $maxKeys);
	$response = $rest->getResponse();
	if ($response->error === false && $response->code !== 200)
		$response->error = array('code' => $response->code, 'message' => 'Unexpected HTTP status');
	if ($response->error !== false) {
		trigger_error(sprintf("S3::getBucket(): [%s] %s", $response->error['code'], $response->error['message']), E_USER_WARNING);
		return false;
	}

	$results = array();

	$lastMarker = null;
	if (isset($response->body, $response->body->Contents))
		foreach ($response->body->Contents as $c) {
			$results[(string)$c->Key] = array(
				'name' => (string)$c->Key,
				'time' => strToTime((string)$c->LastModified),
				'size' => (int)$c->Size,
				'hash' => substr((string)$c->ETag, 1, -1)
			);
			$lastMarker = (string)$c->Key;
			//$response->body->IsTruncated = 'true'; break;
		}


	if (isset($response->body->IsTruncated) &&
	(string)$response->body->IsTruncated == 'false') return $results;

	// Loop through truncated results if maxKeys isn't specified
	if ($maxKeys == null && $lastMarker !== null && (string)$response->body->IsTruncated == 'true')
	do {
		$rest = new S3Request('GET', $bucket, '');
		if ($prefix !== null) $rest->setParameter('prefix', $prefix);
		$rest->setParameter('marker', $lastMarker);

		if (($response = $rest->getResponse(true)) == false || $response->code !== 200) break;
		if (isset($response->body, $response->body->Contents))
			foreach ($response->body->Contents as $c) {
				$results[(string)$c->Key] = array(
					'name' => (string)$c->Key,
					'time' => strToTime((string)$c->LastModified),
					'size' => (int)$c->Size,
					'hash' => substr((string)$c->ETag, 1, -1)
				);
				$lastMarker = (string)$c->Key;
			}
	} while ($response !== false && (string)$response->body->IsTruncated == 'true');

	return $results;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.