Tuesday, August 25, 2015


1 / 2
In the previous post, I discussed that the notion of buckets as a container for similar items is erroneous. Although we are trained to think of a bucket as a collection of folders ( which by itself is a misnomer andshould be read as prefixes), a bucket is essentially a pot-pourri of objects.  
Objects can have prefixes in their keys and name-value pairs in their metadata. Hence there is no organizational structure needed for them and they appear as a flat list within the bucket. This means that we us retrieve collections of objects based on their prefix or metadata. Note that the absence of a hierarchical organizational data structure is more than made up for by the conventions used in the prefix and in the metadata. 
Let us take a look at retrieving a dynamic collection of objects search or metadata query. This is usually done with the help of an iterator. 
First is a prefix based search which we have seen from the browsing of folders. This is possible because  we narrow down the objects retrieved by specifying the prefix.  
For example, to get all objects using PHP we say: 
$iterator = $client->getIterator('ListObjects', array( 
   'Bucket' => $bucket 
)); 

foreach ($iterator as $object) { 
   echo $object['Key'] . "\n"; 
To get all objects in a folder, we say : 
$iterator = $client->getIterator('ListObjects', array( 
   'Bucket' => $bucket, 
   'Prefix' => 'foo' 
)); 

foreach ($iterator as $object) { 
   echo $object['Key'] . "\n"; 

2 / 2
Limit and page_size can also be specified with the iterator.While the prefix limits results to only those that begin with the specified prefix, and delimiter causes list to roll up all keys that share a  common prefix into a single summary list result. 
The purpose of prefix and delimiter parameters is to help you organize and then browse             your keys hierarchically. To do this, first pick a delimiter for your bucket, such as slash (/), that doesn't occurin any of your anticipated key names. Next, construct your key names by concatenating all containing levels of the hierarchy, separating each level with the delimiter. 
The way to do metadata search is by listing keys. Note we are not listing objects. When adding an object to a bucket, you can attach custom headers into the key. This lets you fetch the key names and headerswithout fetching the contents. Then as you retrieve all the keys sequentially, you can filter. 
There is also a built-in pcre regular expression pattern search available on the keys: 
// Instantiate the class 
$s3 = new AmazonS3(); 
$bucket = 'my-bucket' . strtolower($s3->key); 
 
// Get all filenames that match this pattern 
$response = $s3->get_object_list($bucket, array( 
   'pcre' => '/pdf/i' 
)); 
 
// Success? 
var_dump(gettype($response) === 'array'); 
For details, refer here:http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/get_object_list 

No comments:

Post a Comment