Amazon Web Services (AWS) S3 Java create a sub directory (object)

前端 未结 6 1201
孤城傲影
孤城傲影 2021-01-31 17:24

I am familiar with AWS Java SDK, I also tried to browse the corresponding Javadoc, but I could not realize how do I create a sub directory, i.e., a directory object within a buc

6条回答
  •  囚心锁ツ
    2021-01-31 17:54

    There are no "sub-directories" in S3. There are buckets and there are keys within buckets.

    You can emulate traditional directories by using prefix searches. For example, you can store the following keys in a bucket:

    foo/bar1
    foo/bar2
    foo/bar3
    blah/baz1
    blah/baz2
    

    and then do a prefix search for foo/ and you will get back:

    foo/bar1
    foo/bar2
    foo/bar3
    

    See AmazonS3.listObjects for more details.


    Update: Assuming you already have an existing bucket, the key under that bucket would contain the /:

    s3.putObject("someBucket", "foo/bar1", file1);
    s3.putObject("someBucket", "foo/bar2", file2);
    ...
    

    Then you can list all keys starting with foo/:

    ObjectListing listing = s3.listObjects("someBucket", "foo/");
    

提交回复
热议问题