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

后端 未结 4 1810
孤城傲影
孤城傲影 2021-02-18 22:17

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.<

相关标签:
4条回答
  • 2021-02-18 22:24

    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

    0 讨论(0)
  • 2021-02-18 22:30

    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
    
    0 讨论(0)
  • 2021-02-18 22:31

    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

    0 讨论(0)
  • 2021-02-18 22:36

    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

    0 讨论(0)
提交回复
热议问题