AWS SDK Java jar ํŒŒ์ผ ๋‹ค์šด ๋ฐ›๊ธฐ

AWS-SDK for Java - https://aws.amazon.com/ko/sdk-for-java/

์œ„ ๋งํฌ๋ฅผ ๋”ฐ๋ผ ๋“ค์–ด๊ฐ€๋ฉด ์šฐ์ธก ์ƒ๋‹จ์— SDK๋‹ค์šด๋กœ๋“œ ๋˜๋Š” ์ดํด๋ฆฝ์Šค์šฉ ํˆดํ‚ท์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋ฉ”์ด๋ธ์„ ์‚ฌ์šฉํ• ๊ฑด๋ฐ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ฑฐ๋‚˜ ํˆดํ‚ท์„ ์จ๋ณด๊ณ  ์‹ถ์œผ์‹  ๋ถ„๋“ค์€ ์œ„ ๋งํฌ๋ฅผ ์ด์šฉํ•ด์ฃผ์„ธ์š”.


maven์„ ์ด์šฉํ•œ๋‹ค๋ฉด 

1
2
3
4
5
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.386</version>
</dependency>


์ด ๊ธ€์ด ํฌ์ŠคํŒ… ๋˜๋Š” ์‹œ์ ์— ์ตœ์‹ ๋ฒ„์ „์€ 1.11.388์ž…๋‹ˆ๋‹ค. minor ๋ฒ„์ „์ด ํ•˜๋ฃจ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ฆ๊ฐ€ํ•˜๊ณ ์žˆ์–ด์š” ใ…Ž



  AWS s3 ํŒŒ์ผ ์—…๋กœ๋“œ ํ•˜๊ธฐ(AWS S3 Upload for java )

์‹ค์ œ๋กœ S3์— ์—…๋กœ๋“œ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” S3์— ๊ถŒํ•œ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ถŒํ•œ์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ACCESS_KEY, SECRET_KEY ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด๋Š” ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

# Access Key, Secret Key ์ƒ์„ฑ๋ฐฉ๋ฒ• - http://keichee.tistory.com/298



์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜์ฒ˜๋Ÿผ ํŒŒ์ผ์„ ์ „๋‹ฌ๋ฐ›์•„ ์•„๋งˆ์กด S3์— accesskey์™€  secretkey๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ถŒํ•œ์„ ์–ป๊ณ  ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class AWSService {
    private static final String BUCKET_NAME = "bucket_name";
    private static final String ACCESS_KEY = "ACCESS_KEY";
    private static final String SECRET_KEY = "SECRET_KEY";
    private AmazonS3 amazonS3;
 
    public AWSService() {
        AWSCredentials awsCredentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
        amazonS3 = new AmazonS3Client(awsCredentials);
    }
 
    public void uploadFile(File file) {
        if (amazonS3 != null) {
            try {
                PutObjectRequest putObjectRequest =
                        new PutObjectRequest(BUCKET_NAME + "/sub_dir_name"/*sub directory*/, file.getName(), file);
                putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead); // file permission
                amazonS3.putObject(putObjectRequest); // upload file
 
            } catch (AmazonServiceException ase) {
                ase.printStackTrace();
            } finally {
                amazonS3 = null;
            }
        }
    }
}