클라우드에 사진 올리기
누구나 어디서 열던지 클라우드에서 사진을 가져와 볼 수 있다.
버킷 생성
Object Storage - Buket Management
+ 이용 신청
따로 건들거 없이 다음
권한 관리 - 전체 공개로 해야지 다른 사람도 볼 수 있다.
이름 : [ 같은 이름으로 생성 안됨 다른 사람들과도 ]
권한 관리 : 공개
버킷 생성
storage 폴더 생성
사진이 이제 이곳에 올라오게 된다.
네이버 클라우드에 접근할 수 있도록 하는 환경설정 파일을 프로젝트에 만들어야한다.
accessKey 는 보안 때문에 properties 파일에 만들어서 가져와야한다.
properties 파일은 외부에서 들어올 수 없는 파일이기 때문이다.
프로젝트
Package : imageboard.service
Class : NCPObjectStorageService.java
네이버 클라우드에 접근할 수 있도록 하는 환경설정 파일
* 보안 때문에 accessKey 는 원래 properties 파일에 만들어서 가져와야한다.
public class NCPObjectStorageService {
private String accessKey = "[본인보안키]";
private String secretKey = "[본인보안키]"; //보안키
private String regionName = "kr-standard";
private String endPoint = "https://kr.object.ncloudstorage.com";
final AmazonS3 s3;
public NCPObjectStorageService() {
s3 = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
}
public String uploadFile(String bucketName, String directoryPath, File file) {
//String fileName = file.getName();
String fileName = UUID.randomUUID().toString();
FileInputStream fileIn = null;
try {
fileIn = new FileInputStream(file); //파일안에 있는 내용을 읽어와라
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ObjectMetadata objectMetadata = new ObjectMetadata(); //모든 정보를 올려줘야한다
Path path = Paths.get(file.getAbsolutePath());//현재 파일의 확장자 알기 - png jpg ...
String contentType = null;
try {
contentType = Files.probeContentType(path);
} catch (IOException e) {
e.printStackTrace();
}
objectMetadata.setContentType(contentType);
objectMetadata.setContentLength(file.length());
PutObjectRequest putObjectRequest =
new PutObjectRequest(bucketName,
directoryPath + fileName,
fileIn,
objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead); // 리소스에 대한 접근 권한 - 읽기만 가능
// 모든 사용자가 객체를 읽을 수 있지만, 수정/삭제는 불가능
s3.putObject(putObjectRequest); //올리기
return fileName;
}
}
Amason을 사용할건데 jar 파일이 필요함
mvnrepository.com
① aws-java-sdk-s3 검색
② aws-java-sdk-core 검색 - 스프링에서 할때는 자동으로 의존성이 처리되지만, 여기서는 직접 jar를 넣어주어야 한다.
aws-java-sdk-s3-1.12.767.jar
aws-java-sdk-core-1.12.767.jar
commons-logging
jackson-databind
jackson-core
jackson-annotations
httpclient
httpcore
joda-time
jaxb-api
전부 다운받아서 jar 파일 프로젝트에 넣기
( * Spring의 경우 aws-java-sdk-s3만 넣으면 의존관계로 전부 가져오다.)
'----------------' 카테고리의 다른 글
NAVER CLOUD, Cloud DB for MySQL (0) | 2024.10.08 |
---|---|
NAVER CLOUD, 네트워크 설정하기 (0) | 2024.10.08 |