Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get previous page of list objects in Amazon S3

Tags:

php

amazon-s3

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

like image 592
Khawer Zeshan Avatar asked Jan 10 '13 23:01

Khawer Zeshan


People also ask

Can you get previous versions of objects stored on Galaxy S3?

To restore previous object versionsIn the Objects list, choose the name of the object. Choose Versions. Amazon S3 shows all the versions for the object. Select the check box next to the Version ID for the versions that you want to retrieve.

What does listobjectsv2 return?

By default the action returns up to 1,000 key names. The response might contain fewer keys but will never contain more.

What does S3 Listobjects return?

Returns some or all (up to 1,000) of the objects in a bucket. You can use the request parameters as selection criteria to return a subset of the objects in a bucket.


1 Answers

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];
// here you will add values respectively to the columns request_hash, marker_key, previous_marker_key
$dbProvider
     ->addNewMarker($requestHash, $markerForNextPage, $_GET['current_marker']);
like image 170
origaminal Avatar answered Oct 27 '22 01:10

origaminal