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

Viewed 54694

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 bucket, and how do I upload files to it.

Assume bucketName and dirName correspond to already existing bucket (with public permission) and a new (object) directory which needs to be created within the bucket (i.e. bucketName/dirName/)

I have tried the following:

AmazonS3Client s3 = new AmazonS3Client(
    new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));
s3.createBucket(bucketName + "/" + dirName); //throws exception

which throws an exception on the second line.

A short snippet which creates a sub-directory and uploads files to it will be deeply appreciated.

6 Answers

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());

Leaving this answer here just in case someone stumbles upon this. I have been using aws sdk version - 1.11.875 and the following successfully created a folder for me when trying to upload a file into S3 bucket. I did not have to explicitly create the folder as mentioned in the earlier answer.

private void uploadFileToS3Bucket(final String bucketName, final File file) {
    final String fileName = "parent/child/" + file.getName();
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, file);
    amazonS3.putObject(putObjectRequest);
}

This will create the parent and parent/child folders in the specified S3 bucket and upload the file into child folder.

This worked for me. I used spring boot and file uploaded according to Multipart mechanism. I wanted to save my images inside the photos folder in my aws s3 bucket. My need is save like this photos/mypic.jpg

----controller class method----

@PostMapping("/uploadFile")
    public String uploadFile(@RequestPart(value = "file") MultipartFile file) throws IOException {
        return this.amazonClient.uploadFile(file);
}

----service class (Implementation of controller)----

 public String uploadFile(MultipartFile multipartFile) throws IOException {
    
            try {
                File file = convertMultiPartToFile(multipartFile);
                String fileName = "photoes/"+generateFileName(multipartFile);  //here give any folder name you want
                uploadFileTos3bucket(fileName, file);
            } catch (AmazonServiceException ase) {
                logger.info("Caught an AmazonServiceException from GET requests, rejected reasons:");
            }
            return fileName;
        }

The point is concatenate the folder name you want as prefix of the fileName

additionally I will show you how to delete folder. The point is give the folder name as the keyName(key name is uploaded object name in the s3 bucket.). I will show code snippet also.

----controller class method----

@DeleteMapping("/deleteFile")
    public String deleteFile(@RequestPart(value = "keyName") String keyName) {
        return this.amazonClient.deleteFile(keyName);
    }

----service class (Implementation of controller)----

 public String deleteFile(String keyName){
        try {
            s3client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
        } catch (SdkClientException e) {
            e.printStackTrace();
        }
        return "deleted file successfully!";
    }

for delete photos folder that we created , call method like this. deleteFile("photos/") important:- / is mandatory

Related