amazon s3 bucket returns only "top level" keys -
i facing problem awssdks listobjects returns keys of "top level folders" (i know there no such things folders in bucket). in bucket store images in different resolutions. "folder structure" in bucket looks like:
year/month/resolution/file in resolution (of image when uploaded)
if original image should updated, images in smaller resolutions should deleted. therefore want occurences of image in bucket. use following code snippet , top level keys 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 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 orp.jpg should get:
- 2015/05/t120x120/orp.jpg - 2015/05/t160x160/orp.jpg - 2015/05/t640x640/orp.jpg
however empty every time.
if set prefix
s3listobjectrequest.prefix = "2015/05";
the 3 commonprefixes returned. (if use '2015' prefix empty, because listobjects searches in 2015/01)
thank help.
i found solution (better workaround) problem. incredible slow solves problem now. still hoping better/faster solution.
i create list of possible prefixes of bucket , check every prefix , filename delimiter occurrences of image.
using (iamazons3 amazons3client = amazon.awsclientfactory.createamazons3client(awsaccesskey, awssecretkey, amazon.regionendpoint.euwest1)) { listobjectsrequest s3listobjectrequest = new listobjectsrequest(); s3listobjectrequest.bucketname = "my.bucket"; s3listobjectrequest.delimiter = "/"; bool doit = true; ilist<string> prefixes = new list<string>(); ilist<string> tocheck = new list<string>(); { listobjectsresponse response = amazons3client.listobjects(s3listobjectrequest); foreach (string prefix in response.commonprefixes) tocheck.add(prefix); if (tocheck.count > 0) { prefixes.add(tocheck[0]); s3listobjectrequest.prefix = tocheck[0]; tocheck.removeat(0); } else doit = false; } while (doit); foreach (string key in prefixes) { s3listobjectrequest.prefix = key; s3listobjectrequest.delimiter = "filename.jpg"; listobjectsresponse response = amazons3client.listobjects(s3listobjectrequest); foreach (string image in response.commonprefixes) { //path of images delete console.writeline(image); } } }
Comments
Post a Comment