To Store a file in an Amazon S3 Bucket you should have an AWS account.
After login to the AWS account, need to get four things from there.
1. bucket name
2. region
3. amazon secret key
4. amazon access key
Add this dependancy in your pom file:
<!– https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 –>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.708</version>
</dependency>
Sample Java Code:-
import java.io.File;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class UploadFileToS3 {
public static void main(String[]args){
String secretKey = “**secret key**”;
String accessKey = “**access key**”;
String bucketName = “**bucket name**”;
String region = “**region name**”;
String fileObjKeyName = “**File object key name**”;
String fileName = “**Path of the file to upload**”;
Regions clientRegion = Regions.valueOf(region);
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(clientRegion)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(“image”+”/”+”.jpg”);
request.setMetadata(metadata);
s3Client.putObject(request.withCannedAcl(CannedAccessControlList.PublicRead));
String url = s3Client.getUrl(bucketName, fileObjKeyName).toString();
}
}
Code Explanation:-
1 Get the Region enum By providing region-name
Regions clientRegion = Regions.valueOf(region);
2. To get the BasicAWSCredentials object, provide the access key and secret key in the constructor-
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
This will provide you authentication
3. To get the AmazonS3 you need to add below code:
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(clientRegion)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
This will return object of AmazonS3 which play the important role to upload the file.
4. You need to provide the metadata of a file. This means you need to provide additional information like what type of file it is or what is the extension of the file.
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType([*fileType]/[*extension]));
*Replace fileType with the type of file like image, video, etc.
*Replace extension with the type file extension like .mp4, .jpg, .png, etc.
5. PutObjectRequest is the object that is going save in the Amazon S3 bucket. You need to provide the bucketName, fileObjKeyName, and the object of the file you want to save. And set the metadata in this PutObjectRequest.
PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
request.setMetadata(metadata);
*fileObjKeyName is the path of your file in the S3 bucket.
6. When you save the file in the S3 storage they save as private permission that means you can’t access them from outside the bucket. To provide permission to the files, add the request object with cannedACl with public permission or as you like.
s3Client.putObject(request.withCannedAcl(CannedAccessControlList.PublicRead));
This will save your file in the S3 Bucket.
7. To get the URL of the saved file call the getUrl method and pass the bucketName and file path in the bucket (Not your local file path). This will return you a URL of the saved file. You can access the file anywhere by this URL if the file has public permission.
String url = s3Client.getUrl(bucketName, fileObjKeyName).toString;
You can check the file in the S3 bucket by login into the console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.
Goto your Amazone S3 bucket and see folder structure there and find your file.