dimanche 31 mai 2015

amazon s3 bucket returns only "top level" keys

I am facing a problem that AWSSDKs ListObjects only returns keys of "top level folders" (I know there are no such things like folders in a bucket). In my bucket I store images in different resolutions. The "folder structure" in the bucket looks like:

year/month/resolution/file in this resolution (of the image when its uploaded)

If an original image should be updated, all images in smaller resolutions should be deleted. Therefore I want to get all occurences of an image in the bucket. I use the following code snippet to do this and for top level keys it works fine.

using (IAmazonS3 amazonS3Client =     Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey, Amazon.RegionEndpoint.EUWest1))
{
    ListObjectsRequest S3ListObjectRequest = new ListObjectsRequest();
    S3ListObjectRequest.BucketName = "my_bucket";
    S3ListObjectRequest.Delimiter = "LL.jpg";

    ListObjectsResponse listObjectRequest = amazonS3Client.ListObjects(S3ListObjectRequest);

    foreach (string S3BucketDir in listObjectRequest.CommonPrefixes)
    {
        //delete image
    }
}

for LL.jpg i get the following CommonPrefixes:

 - 2014/07/T120x120/LL.jpg
 - 2014/07/T160x160/LL.jpg
 - 2014/07/T320x320/LL.jpg
 - 2014/07/T640x640/LL.jpg
 - 2014/07/T76x76/LL.jpg
 - 2014/07/T80x80/LL.jpg

but for orp.jpg I should get:

 - 2015/05/T120x120/orp.jpg
 - 2015/05/T160x160/orp.jpg
 - 2015/05/T640x640/orp.jpg

however it is empty every time.

If I set the prefix

S3ListObjectRequest.Prefix = "2015/05";

the 3 CommonPrefixes are returned. (If I only use '2015' as prefix its also empty, because ListObjects only searches in 2015/01)

Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire