티스토리 뷰

# 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();
}
참고
https://gist.github.com/kykkyn2/052382599134adf504abfa096282b816


# uploading file to Amazon S3 (S3에 파일 업로드, S3Helper)

입력받은 byteArray값을 ByteArrayInputStream으로 변환한 뒤 저장한다

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, // Bucket
test", // key
“txt/plain”, // fileContentType
CannedAccessControlList.PublicRead
);


댓글
최근에 올라온 글
«   2024/11   »
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
Total
Today
Yesterday