Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 751
user3363603 Avatar asked Mar 10 '14 22:03

user3363603


4 Answers

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

like image 127
Mike Avatar answered Sep 26 '22 09:09

Mike


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
like image 35
wes.hysell Avatar answered Sep 23 '22 09:09

wes.hysell


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

like image 20
Sony Kadavan Avatar answered Sep 23 '22 09:09

Sony Kadavan


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

like image 33
user602525 Avatar answered Sep 22 '22 09:09

user602525