I have the following folder structure in S3.
photos/id1/gallery/pic1
photos/id2/gallery/pic2
photos/id3/menu/pic3
I have written the following snippet to list the pics in each of the id
var restids=['id1','id2','id3'];
for(var i=0;i<restids.length;i++)
{
(function(i){
setTimeout(function(){
var getparams ={Bucket:'photos' , Marker:restids[i]+'/'};
(function(par){
setTimeout(function(){
s3.listObjects(par,function(err,data){
if(err){
console.log('err fetching images from s3 '+err);
}
else{
console.log(_.pluck(data.Contents,'Key'));
}
});
});
})(getparams);
});
})(i);
}
The expected result for the above snippet is
['id1/gallery/pic1']
['id2/gallery/pic2']
['id3/menu/pic3']
But the actual result is
['id1/gallery/pic1']
['id1/gallery/pic1','id2/gallery/pic2']
['id1/gallery/pic1','id2/gallery/pic2','id3/menu/pic3']
Each of the return value contains the values from the previous calls.
I could not understand this weird behavior.
Is it due to the callbacks or is there a problem with the API itself or is there a problem with my code?
I would really appreciate if anyone could explain me this.
Thanks in advance.
Wrong parameters in listObjects. The API call should have been like this
var getparams ={Bucket:'photos' , Prefix:restids[i]+'/'}
instead of
var getparams ={Bucket:'photos' , Marker:restids[i]+'/'}