Tuesday, October 10, 2017

How to get previous page of list objects in Amazon S3

Leave a Comment

I have successfully created the next button for listing objects in Amazon S3 bucket. Every time user will click on NEXT button only two keys will be returned. The code is as follows

    $response = $s3->list_objects('bucket_name', array(                 'prefix' => 'small/',                 'max-keys' => 2,                 'marker' => 'small/blah.jpg'             )); 

The above code will return 2 keys after the marker. But i am unable to create the previous button. Is there is anything that could return previous 2 keys before the marker? Or can you suggest any better solution. I am using PHP SDK. Thanks

1 Answers

Answers 1

AWS S3 API do not have any parameters that specify to return items before the marker. In the newest version of API the marker was renamed to the start-after parameter that stresses the fact you get only items next to the parameter value.

To implement Previous button you should rather store a sequential list of markers for a specific request in DB or cache. There could be 3 columns for example:

request_hash | marker_key | previous_marker_key 

Processing the current request you could add the rows to table and fetch the rows. Here is some pseudo-code how it could look like:

$requestHash = sha1(serialize([$bucketName, $prefix, $delimiter])); $markerForPreviousPage = $dbProvider      ->fetchPreviousMarker($requestHash, $_GET['current_marker']); $markerForNextPage = $keysFromS3[count($keysFromS3) - 1]; // there you will add values respectively to the columns request_has, marker_key, previous_marker_key $dbProvider      ->addNewMarker($requestHash, $markerForNextPage, $_GET['current_marker']); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment