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.
Creating a folder Instead, upload an empty folder and specify these settings in the upload configuration. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to create a folder in.
In Amazon S3, folders are used to group objects and organize files. Unlike a traditional file system, Amazon S3 doesn't use hierarchy to organize its objects and files. For the sake of organizational simplicity, Amazon S3 console supports the folder concept as a means of grouping objects.
There are no "sub-directories" in S3. There are buckets and there are keys within buckets.
You can emulate traditional directories by using prefix searches. For example, you can store the following keys in a bucket:
foo/bar1 foo/bar2 foo/bar3 blah/baz1 blah/baz2
and then do a prefix search for foo/
and you will get back:
foo/bar1 foo/bar2 foo/bar3
See AmazonS3.listObjects
for more details.
Update: Assuming you already have an existing bucket, the key under that bucket would contain the /
:
s3.putObject("someBucket", "foo/bar1", file1); s3.putObject("someBucket", "foo/bar2", file2); ...
Then you can list all keys starting with foo/
:
ObjectListing listing = s3.listObjects("someBucket", "foo/");
S3 doesn't see directories in the traditional way we do this on our operating systems. Here is how you can create a directory:
public static void createFolder(String bucketName, String folderName, AmazonS3 client) { // create meta-data for your folder and set content-length to 0 ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); // create empty content InputStream emptyContent = new ByteArrayInputStream(new byte[0]); // create a PutObjectRequest passing the folder name suffixed by / PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName + SUFFIX, emptyContent, metadata); // send request to S3 to create folder client.putObject(putObjectRequest); }
As casablanca already said you can upload files to directories like this:
s3.putObject("someBucket", "foo/bar1", file1);
Read the whole tutorial here for details, and the most important thing is you will find info how to delete the directories.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With