Listing files in a specific “folder” of a AWS S3 bucket

前端 未结 7 639
耶瑟儿~
耶瑟儿~ 2020-12-24 04:51

I need to list all files contained in a certain folder contained in my S3 bucket.

The folder structure is the following

/my-bucket/users/

        
7条回答
  •  臣服心动
    2020-12-24 05:10

    As other have already said, everything in S3 is an object. To you, it may be files and folders. But to S3, they're just objects.

    If you don't need objects which end with a '/' you can safely delete them e.g. via REST api or AWS Java SDK (I assume you have write access). You will not lose "nested files" (there no files, so you will not lose objects whose names are prefixed with the key you delete)

    AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion("region").build();
    amazonS3.deleteObject(new DeleteObjectRequest("my-bucket", "users//contacts//"));
    

    Please note that I'm using ProfileCredentialsProvider so that my requests are not anonymous. Otherwise, you will not be able to delete an object. I have my AWS keep key stored in ~/.aws/credentials file.

提交回复
热议问题