0

Hi Cloud Computing Geeks, Is there any way of mounting/connecting S3 bucket with EC2 instances using JAVA AWS SDK (not by using CLI commands/ec2-api-tools). I do have all the JAVA sdks required. I successfully created a bucket using Java AWS SDK, now further want to connect it with my EC2 instances being present in North Virginia region. I didn't find any way of to do it but I hope there must be some way.

Cheers, Hammy

Hammy
  • 35
  • 1
  • 5
  • What are you trying to accomplish? Amazon S3 isn't a filesystem, although there are libraries out there that make it look like one. – Steven Hood Apr 13 '13 at 16:28

1 Answers1

2

You don't "mount S3 buckets", they don't work that way for they are not filesystems. Amazon S3 is a service for key-based storage. You put objects (files) with a key and retrieve them back with the same key. The key is merely a name that you assign arbitrarily and you can include "/" markers in the names to simulate a hierarchy of files. For example, a key could be folder1/subfolder/file1.txt. For illustration I show the basic operations with the java sdk.

First of all you must set up your amazon credentials and get an S3 client:

AWSCredentials credentials = new BasicAWSCredentials("your_accessKey", your_secretKey");
AmazonS3Client s3client = new AmazonS3Client(credentials);

Store a file:

File file = new File("some_local_path/file1.txt");
String fileKey = "folder1/subfolder/file1.txt";
s3client.putObject("bucket_name", fileKey, file);

Retrieve back the file:

 S3ObjectInputStream objectInputStream = s3client.getObject("bucket_name", fileKey).getObjectContent();

You can read the InputStream or you can save it as file.

List the objects of a (simulated) folder: See my answer in another question.

Community
  • 1
  • 1
juanmirocks
  • 4,786
  • 5
  • 46
  • 46