1

I understand the concept of folders and objects in Aws S3 bucket, that there is no folder and all are objects. But I need to create an empty folder where user can enter and upload their files. I have tried several things like adding a test .txt file with a path which extends the empty folder's name after "/" and it do create the folder but it keeps the test.txt file which if I delete also deletes the folder created.

So any idea about how to create an zero size empty folder object as is created in the web console of S3 storage? Any help would be great

abby
  • 350
  • 4
  • 21

2 Answers2

1

Use your SDK of choice to create an S3 object named, for example, folder1/folder2/cats/ with a size of 0 bytes. This assumes that your preferred folder separator is /, which is obviously the most commonly used separator, but you could use anything you like.

Here is a generic Java upload an object example. Modify this so that the object key is folder1/folder2/cats/ and no content is uploaded.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • thank u for replying. Ok I create an object named something ending with "/" but the upload method takes 2 arguments one the path in the s3 storage and the other a java file. Now how to create that java file without any extension, zero length and the name as stated?..Any code sample would be helpful or link – abby Jan 29 '20 at 13:39
  • There are numerous `putObject` methods. You don’t need to supply a file. The best option is probably the method that allows you to to supply an empty content string. – jarmod Jan 29 '20 at 13:53
  • 1
    Thanxx a ton Sir, actually I was using their latest transferUtility method which was taking those arguments, was unaware of this numerous putObject methods – abby Jan 30 '20 at 04:21
  • how can I delete this folder using AWS amplify kotlin? – Nensi Kasundra Sep 03 '20 at 11:46
  • @NensiKasundra the ‘folder’ is just another S3 object. Delete it as you would any S3 object. – jarmod Sep 03 '20 at 11:48
  • nope it's not working like that I am referring this one only https://docs.amplify.aws/lib/storage/remove/q/platform/android – Nensi Kasundra Sep 03 '20 at 12:00
  • passing foldername/ and it's not deleting from AWS – Nensi Kasundra Sep 03 '20 at 12:01
  • I don’t know what you mean by “it’s not working”. What do you have, what did you do, what did you expect to happen, and what actually happened? Note that if you still have objects whose keys begin with foldername/ then those have to be deleted independently. Deleting a ‘folder’ from S3 doesn’t delete any other objects with the same folder prefix. It just deletes the object representing the folder. – jarmod Sep 03 '20 at 12:03
1

Despite the long time, perhaps this code can help someone with the same question. I am using the SDK for Java. I will assume that you have a clients3 already configured and working. So I'm going to simulate his injection here. See the createFolder method:

   @Inject
    private AmazonS3 s3client;//Your configurated s3Client  
    private final String SUFFIX = "/";
    
    //Create a conceitual folder into bucketName
    public void createFolder(String bucketName, String folderName) {
        try {
            // Create metadata with content 0 
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(0L);
            
            // Empty content
            InputStream inputStream = new ByteArrayInputStream(new byte[0]);
            
            // Creates a PutObjectRequest by passing the folder name with the suffix
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
                        folderName.toString() + SUFFIX, inputStream, metadata);
            
            //Send the request to s3 to create the folder
            s3client.putObject(putObjectRequest);
            
        }catch (Exception e) {
            throw new RepositoryException("Error creating folder "+folderName, 
                    ErrorCode.SERVER_ERROR.getCode());
        }
        
    }
    
Tarcísio Rosa
  • 56
  • 1
  • 1
  • 7