How to Configure Amazon Simple Storage Service and Store Files Using Java
Posted By : Vishal Kumar | 29-Jun-2017
Amazon Simple Storage service (Amazon S3) is a simple cloud storage for developers. Developers can configure S3 programmatically. These are the steps for S3 configuration.
Step 1: Setting Up Your Project
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.9.2</version>
</dependency>
Step 2: Authenticate with Amazon S3
AWSCredentials credentials = new BasicAWSCredentials("YourAccessKeyID", "YourSecretAccessKey");
Step 3: Create S3 Client
AmazonS3 s3client = new AmazonS3Client(credentials);
Step 4: Create Bucket
String bucketName = "bitcoinbank";
s3client.createBucket(bucketName);
Step 5: List Buckets
for (Bucket bucket : s3client.listBuckets()) {
System.out.println(" - " + bucket.getName());
}
Step 6: Create Folder in S3 Bucket
public static void createFolder(String bucketName, String folderName, AmazonS3 client) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName + SUFFIX, emptyContent, metadata);
client.putObject(putObjectRequest);
}
Step 7: Upload file in S3
String fileName = folderName + SUFFIX + "bitcoinbank.jpg";
s3client.putObject(new PutObjectRequest(bucketName, fileName,
new File("C:\\Users\\user\\Desktop\\bitcoinbank.jpg")).withCannedAcl(CannedAccessControlList.PublicRead));
Step 8: Deleting Files, Folders and Buckets
s3client.deleteObject(bucketName, fileName);
s3client.deleteBucket(bucketName);
Conclusion : This is the simple code for putting your objects (images, videos, documents etc..) into the S3 bucket. You can make your object public and private also.
Thanks,
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vishal Kumar
Vishal Kumar is Master in Computers Application. He has good technical skills in Java and always motivated to learn new things.