Method to check if Prefix Exists ?

10 views
Skip to first unread message

helio...@gmail.com

unread,
Oct 24, 2016, 4:06:18 PM10/24/16
to JetS3t Users
Hello everybody,

I am using the jets3t library v0.9.4 for some time now, with a fair amount of success.

I know how to deal with existence or not of objects, but I cannot find an efficient way to deal with how to check for the existence of *prefixes*.

This is what I have:

public static boolean prefixExists(S3Service service, String bucket, String prefix) {

S3Object[] objects = new S3Object[0];
try {
objects = service.listObjects(bucket, prefix, null, 2); // 2 is the maxListingLength. Could possibly be '1' instead.
} catch (S3ServiceException e) {
// some error handling
}
return objects.length > 0;
}

I was hoping that calling this for prefix="results/2016-10-24" would yield true if and only if there are actual objects under it.
Which certainly works, except that my arbitrary limit ( 2 ) does not really get acknowledged.
In fact, i get returned all 3996 objects that lie under the given prefix. This is wasteful and slow, because some other "folders" would list tens of thousands of contained objects, while a single one would have sufficed.

Is there a reason why the limit doesn't work?
Or, better yet, is there a more efficient way to ascertain the existence of a prefix?

Thank you all for reading!

helio...@gmail.com

unread,
Oct 25, 2016, 3:17:20 PM10/25/16
to JetS3t Users, helio...@gmail.com
Replying to my own post, as I figured it out.
Turns out I was just being silly and using the wrong method. The correct way to do the more efficient checking is via the chunk listing, as follows:

public static boolean prefixExists(S3Service service, String bucket, String prefix) {

    StorageObject[] objects = new StorageObject[0];
try {
objects = service.listObjectsChunked(bucket, prefix, null, 1, null, false).getObjects();
} catch (ServiceException e) {

// some error handling
}
return objects.length > 0;
}

Hopes this ends up being of some use to future list members!

James Murty

unread,
Oct 26, 2016, 7:33:51 PM10/26/16
to jets3t...@googlegroups.com, helio...@gmail.com
Hi,

Thanks for following up with a solution to your question. The `listObjectsChunked` method is indeed the right one to use to have more control over the listing operation, and avoid JetS3t automatically performing multiple requests to list *all* the items in the bucket.

You could use the slightly simpler version of `listObjectsChunked` without the last `false`, but it's better to be explicit about what you are doing so what you have is good.

Cheers,
James



--
You received this message because you are subscribed to the Google Groups "JetS3t Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jets3t-users+unsubscribe@googlegroups.com.
To post to this group, send email to jets3t...@googlegroups.com.
Visit this group at https://groups.google.com/group/jets3t-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages