Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a folder/uploading a file in amazon S3 bucket using API

I am a total newbie to amazon and java. I am trying two things here.

1st - I am trying to create a folder in my Amazon S3 bucket that i have already created and have got the credentials for.

2nd - I am trying to upload a file to this bucket.

As per my understanding i can use putObjectRequest() method for acheiving both of my tasks.

PutObjectRequest(bucketName, keyName, file) 

for uploading a file.

I am not sure if i should use this method

PutObjectRequest(String bucketName, String key, InputStream input,
        ObjectMetadata metadata) 

for just creating a folder. I am struggeling with InputSteam and ObjectMetadata. I don't know what exactly is this for and how can i use it.

Any help would be greatly appreciated. :)

like image 219
Shaonline Avatar asked Jan 29 '16 08:01

Shaonline


1 Answers

You do not need to create a folder in Amazon S3. In fact, folders do not exist!

Rather, the Key (filename) contains the full path and the object name.

For example, if a file called cat.jpg is in the animals folder, then the Key (filename) is: animals/cat.jpg

Simply Put an object with that Key and the folder is automatically created. (Actually, this isn't true because there are no folders, but it's a nice simple way to imagine the concept.)

As to which function to use... always use the simplest one that meets your needs. Therefore, just use PutObjectRequest(bucketName, keyName, file).

like image 76
John Rotenstein Avatar answered Sep 22 '22 15:09

John Rotenstein