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

冷暖自知 提交于 2019-12-21 07:12:11

问题


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.


回答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




回答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



回答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




回答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



来源:https://stackoverflow.com/questions/22312833/how-can-you-tell-if-an-object-is-a-folder-on-aws-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!