Anviam

Anviam is your one-stop solution to a variety of tech services and consultation custom- suited for your business needs. From building robust applications to understanding scalability in the future, integrating efficient systems and developing modern solutions, you'll find it all with us! We're here to break down scalability for you, guide you through large-scale web application development, and equip you with practical solutions ...First is software, design decisions and IT infrastructure. The second is the scalability of teams and processes. It is difficult to build ... Developing a robust, scalable, and efficient system can be daunting . However, understanding the key concepts and components can make the ...

美國威而鋼VIAGRA

性功能障礙是男性和女性中常見的問題。它可以由生理問題和醫學狀況引起

威而鋼的效果可能因個體差異而有所不同。大樹藥局威而鋼。 預防藥:用於預防特定疾病,如疫苗或抗瘧疾藥物。

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.

Share This