티스토리 뷰

DEV/JAVA

AWS S3 파일업로드

초록매실원액 2019. 12. 31. 15:41
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class S3Util {
    Regions clientRegion = Regions.AP_NORTHEAST_2;
    final String bucketName = "버킷네임";
    final String accesskey  = "액세스키";
    final String secretkey  = "비밀키";
     
    public String getBucketName() {
        return bucketName;
    }
    public String getAccessKey() {
        return accesskey;
    }
    public String getSecretKey() {
        return secretkey;
    }
    private AmazonS3 conn;
    public S3Util() {
        
        AWSCredentials credentials = new BasicAWSCredentials(this.getAccessKey(), this.getSecretKey());
        this.conn = AmazonS3ClientBuilder.standard()
                .withRegion(clientRegion)
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .build();
        
        enableVersioning();
    }
    
    /**
     * Enable versioning on the bucket.
     */
    public void enableVersioning() {
        BucketVersioningConfiguration configuration = 
                new BucketVersioningConfiguration().withStatus("Enabled");
        
        SetBucketVersioningConfigurationRequest setBucketVersioningConfigurationRequest = 
                new SetBucketVersioningConfigurationRequest(bucketName,configuration);
        
        conn.setBucketVersioningConfiguration(setBucketVersioningConfigurationRequest);
        
        // 2. Get bucket versioning configuration information.
        BucketVersioningConfiguration conf = conn.getBucketVersioningConfiguration(bucketName);
        System.out.println("bucket versioning configuration status:    " + conf.getStatus());
         
    }
    
    /**
     * Upload a file as a new object with ContentType and title specified.
     * @param fileObjKeyName
     * @param fileName
     */
    public void uploadFile(String fileObjKeyName , String fileName) {
        PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType("plain/text");
        metadata.addUserMetadata("x-amz-meta-title""블라블라");
        request.setMetadata(metadata);
        conn.putObject(request);
    }
}
 
 

 

메인

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    /**
     * AWS S3 파일업로드
     * @param fileName
     */
    private void sendAWSFile(String fileName ) {
        
        final String S3FOLDER = "data/";
        if(StringUtils.isBlank(fileName)) {
            return;
        }
        
        String destFileName = fileName;
        if(fileName.indexOf('_'>= 0) {
            destFileName = fileName.substring(fileName.indexOf('_')+1   , fileName.length());
        }
        
        S3Util s3 = new S3Util();
        s3.uploadFile(S3FOLDER+destFileName+".csv", fileName+".csv");
    }
 
 

 

'DEV > JAVA' 카테고리의 다른 글

spring boot + jasypt  (0) 2017.09.15
StringUtils.isEmtpty() vs StringUtils.isBlank()  (0) 2016.03.18
POI 로 엑셀파일 읽기(xls , xlsx)  (0) 2016.03.14
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
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 29 30 31
글 보관함