How can you tell if an object is a folder on AWS S3

16,246

Solution 1

I don´t know if this response it will be usefull after so long, but here we go:

To resolve this problem you have to do this in java:

        List<S3ObjectSummary> files = s3client.listObjects(bucket.getName()).getObjectSummaries();
        for (S3ObjectSummary file : files) {
            if (file.getKey().endsWith("/"))
                System.out.println("----" + file.getKey() + " (ES CARPETA)");
            else 
                System.out.println("----" + file.getKey() + " NO NO NO");
        }

With the method "endsWith("/")" you can detect if the S3ObjectSummary is a folder or not.

Hope this can helps someone.

Mike

Solution 2

3 years into the future of the OP, I am providing my answer since this appeared in a recent Google Search.

The concept of "directories" is a little more refined in S3 these days, with these objects receiving a content-type of "application/x-directory".

As such, if you're using the AWS Ruby SDK (version 1), you can simply use:

s3_client.buckets['some_bucket'].objects['some_directory_object'].content_type

Solution 3

There is no folder concept on S3. All objects have a key (like name) and keys can contain special characters like / (slash). This gives us a feel of folders.

When you list the bucket contents it returns the list of all the objects (and the keys). Then you can see if the key string contains slash (/). If it contains, then understand the object is in a "folder like" structure. That way you get the full details.

"A response can contain CommonPrefixes only if you specify a delimiter. When you do, CommonPrefixes contains all (if there are any) keys between Prefix and the next occurrence of the string specified by delimiter. In effect, CommonPrefixes lists keys that act like subdirectories in the directory specified by Prefix. For example, if prefix is notes/ and delimiter is a slash (/), in notes/summer/july, the common prefix is notes/summer/. All of the keys rolled up in a common prefix count as a single return when calculating the number of returns. See MaxKeys." http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html

Solution 4

Objects are not folders. From the docs:

An Amazon S3 bucket has no directory hierarchy such as you would find in a typical computer file system. You can, however, create a logical hierarchy by using object key names that imply a folder structure.

The best you can do is use GET Bucket (list objects) to get some info about the contents of the bucket:

http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html

Share:
16,246

Related videos on Youtube

user3363603
Author by

user3363603

Updated on June 04, 2022

Comments

  • user3363603
    user3363603 almost 2 years

    I doesn't appear in the meta data--whether an object is a folder or not. Is there a specific method you guys on SO know of? I can't find anything of worth in Google search.

  • unknownprotocol
    unknownprotocol over 4 years
    Curious as to what one can even do, with a directory "object" as returned from a getBucketListing() call ... I suppose query for directory/* ?
  • ansh sachdeva
    ansh sachdeva over 3 years
    works like wonders! thanks for this . also, for the non spanish speakers , ES CARPETA translates to IT'S A FOLDER.