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

前端 未结 6 1208
孤城傲影
孤城傲影 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 18:04

    In newer versions of the SDK, you can do something like this (no need to create empty InputStream) to create an empty folder:

    String key = parentKey + newFolderName;
    if (!StringUtils.endsWith(key, "/")) {
        key += "/";
    }
    
    PutObjectRequest putRequest = PutObjectRequest.builder()
            .bucket(parent.getBucket())
            .key(key)
            .acl("public-read")
            .build();
    s3Client.putObject(putRequest, RequestBody.empty());
    

提交回复
热议问题