Reputation: 35
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
Upvotes: 0
Views: 2413
Reputation: 6171
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.
Upvotes: 2