To create a folder, we need to have a bucket. We cannot create a folder at the root level. Folders can be nested. To create a folder under the bucket, we need to create an object with its content length metadata as
0 and an empty content so that it can be recognized as a folder:
ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); InputStream emptyContent = new ByteArrayInputStream(new byte); PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName + "/", emptyContent, metadata); s3.putObject(putObjectRequest);
In the preceding code, you can see that the content length is set to
0, and an empty byte array input has been added. Apart from this, we provided the suffix
"/" to the folder name ...