참고
https://gist.github.com/kykkyn2/052382599134adf504abfa096282b816
# uploading file to Amazon S3 (S3에 파일 업로드, S3Helper)
입력받은 byteArray값을 ByteArrayInputStream으로 변환한 뒤 저장한다
# JAVA String to ByteArrayOutputStream
DataOutputStream 파라미터에 String ByteArray 값을 넣어주고, byteArrayOutputStream이 가지고 있는 ByteArray값을 리턴한다
public byte[] getByteArray(String data) {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();DataOutputStream out = new DataOutputStream(byteArrayOutputStream);try {out.write(data.getBytes());byteArrayOutputStream.flush();byteArrayOutputStream.close();} catch (IOException e) {e.printStackTrace();}return byteArrayOutputStream.toByteArray();}
import com.amazonaws.services.s3.AmazonS3;public PutObjectResult putObject(byte[] bytes,Buckets buckets,String key,String fileContentType,CannedAccessControlList cannedAccessControlList) {ObjectMetadata objectMetadata = new ObjectMetadata();objectMetadata.setContentType(fileContentType);PutObjectRequest putObjectRequest = new PutObjectRequest(buckets.getBucketName(),key,byteArrayToFileInputStream(bytes),objectMetadata);putObjectRequest.withCannedAcl(cannedAccessControlList);return amazonS3.putObject(putObjectRequest);}private InputStream byteArrayToFileInputStream(byte[] bytes) {return new ByteArrayInputStream(bytes);}
# Uploading String to Amazon S3
저장할 String 문자열을 위의 코드들을 이용해 변환한 뒤 text file로 S3에 저장하게 된다
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();DataOutputStream out = new DataOutputStream(byteArrayOutputStream);String word = "kkuma"try {out.write(word.getBytes());byteArrayOutputStream.close();} catch (IOException e) {e.printStackTrace();}byte[] bytes = byteArrayOutputStream.toByteArray();s3Helper.putObject(bytes,Buckets.TEST, // Buckettest", // key“txt/plain”, // fileContentTypeCannedAccessControlList.PublicRead);
[Spring] How to get list objects in AWS S3 bucket (0) | 2019.01.12 |
---|---|
[Java] Collections.sort()를 이용한 List 정렬 (0) | 2018.12.30 |
[Spring] Reading S3 text file as a string (0) | 2018.12.19 |
[Spring] AOP(Aspect Oriented Programming) (0) | 2018.10.28 |
[Spring] 응답보안헤더 (0) | 2018.10.25 |