pom.xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.901</version>
</dependency>
AWSS3.java
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSCredentials;
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.CopyObjectRequest;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import java.io.File;
public class AWSS3 {
private AmazonS3 s3Client;
final private String accessKey = "IAM에서 발급받은 accessKey";
final private String secretKey = "IAM에서 발급받은 secretKey";
private Regions clientRegion = Regions.설정한 지역;
private String bucket = "설정한 버킷 이름";
private AwsS3() {
createS3Client();
}
static private AwsS3 instance = null;
public static AwsS3 getInstance() {
if (instance == null) {
return new AwsS3();
} else {
return instance;
}
}
private void createS3Client() {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
this.s3Client = AmazonS3ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(clientRegion)
.build();
}
public void upload(File file, String key) {
uploadToS3(new PutObjectRequest(this.bucket, key, file));
}
public void upload(InputStream is, String key, String contentType, long contentLength) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(contentType);
objectMetadata.setContentLength(contentLength);
uploadToS3(new PutObjectRequest(this.bucket, key, is, objectMetadata));
}
private void uploadToS3(PutObjectRequest putObjectRequest) {
try {
this.s3Client.putObject(putObjectRequest);
System.out.println(String.format("[%s] upload complete", putObjectRequest.getKey()));
} catch (AmazonServiceException e) {
e.printStackTrace();
} catch (SdkClientException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void copy(String orgKey, String copyKey) {
try {
CopyObjectRequest copyObjRequest = new CopyObjectRequest(
this.bucket,
orgKey,
this.bucket,
copyKey
);
this.s3Client.copyObject(copyObjRequest);
System.out.println(String.format("Finish copying [%s] to [%s]", orgKey, copyKey));
} catch (AmazonServiceException e) {
e.printStackTrace();
} catch (SdkClientException e) {
e.printStackTrace();
}
}
public void delete(String key) {
try {
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(this.bucket, key);
this.s3Client.deleteObject(deleteObjectRequest);
System.out.println(String.format("[%s] deletion complete", key));
} catch (AmazonServiceException e) {
e.printStackTrace();
} catch (SdkClientException e) {
e.printStackTrace();
}
}
}
Main.java
import java.io.File;
public class Main {
public AwsS3 awsS3 = AwsS3.getInstance();
public static void main(String[] args) {
Main main = new Main();
File file = new File("D://sdaff.png");
String key = "img/my-img.png";
String copyKey = "img/my-img-copy.png";
main.upload(file, key);
}
public void upload(File file, String key) {
awsS3.upload(file, key);
}
public void copy(String orgKey, String copyKey) {
awsS3.copy(orgKey, copyKey);
}
public void delete(String key) {
awsS3.delete(key);
}
}