Skip to content

Commit

Permalink
Merge pull request #20 from Kkokkomu/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gouyeonch authored Aug 1, 2024
2 parents 659fd40 + 2247682 commit 962c5c7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 122 deletions.
Binary file modified src/.DS_Store
Binary file not shown.
Binary file modified src/main/.DS_Store
Binary file not shown.
56 changes: 28 additions & 28 deletions src/main/java/com/kkokkomu/short_news/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package com.kkokkomu.short_news.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class S3Config {
@Value("${cloud.aws.credentials.access-key}")
private String accessKey;
@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;
@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials awsCredentials= new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
//package com.kkokkomu.short_news.config;
//
//import com.amazonaws.auth.AWSStaticCredentialsProvider;
//import com.amazonaws.auth.BasicAWSCredentials;
//import com.amazonaws.services.s3.AmazonS3Client;
//import com.amazonaws.services.s3.AmazonS3ClientBuilder;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Configuration
//public class S3Config {
// @Value("${cloud.aws.credentials.access-key}")
// private String accessKey;
// @Value("${cloud.aws.credentials.secret-key}")
// private String secretKey;
// @Value("${cloud.aws.region.static}")
// private String region;
//
// @Bean
// public AmazonS3Client amazonS3Client() {
// BasicAWSCredentials awsCredentials= new BasicAWSCredentials(accessKey, secretKey);
// return (AmazonS3Client) AmazonS3ClientBuilder.standard()
// .withRegion(region)
// .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
// .build();
// }
//}
82 changes: 41 additions & 41 deletions src/main/java/com/kkokkomu/short_news/config/YouTubeConfig.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package com.kkokkomu.short_news.config;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;

@Component
@Slf4j
public class YouTubeConfig {
@Value("classpath:youtube/kkokkomu-mvp-31a5934a9b06.json")
private Resource serviceAccountKey;

private static final String APPLICATION_NAME = "kkokkomu-mvp";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

public YouTube getService() throws GeneralSecurityException, IOException {
log.info("youtube get service");
GoogleCredentials credentials = ServiceAccountCredentials.fromStream(serviceAccountKey.getInputStream())
.createScoped(Collections.singleton("https://www.googleapis.com/auth/youtube.upload"));
// Credential 내용 로그에 출력
log.info("Credential: {}", credentials);

return new YouTube.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, new HttpCredentialsAdapter(credentials))
.setApplicationName(APPLICATION_NAME)
.build();
}
}
//package com.kkokkomu.short_news.config;
//
//import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
//import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
//import com.google.api.client.json.JsonFactory;
//import com.google.api.client.json.jackson2.JacksonFactory;
//import com.google.api.services.youtube.YouTube;
//import com.google.auth.http.HttpCredentialsAdapter;
//import com.google.auth.oauth2.GoogleCredentials;
//import com.google.auth.oauth2.ServiceAccountCredentials;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.io.Resource;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Service;
//
//import java.io.IOException;
//import java.security.GeneralSecurityException;
//import java.util.Collections;
//
//@Component
//@Slf4j
//public class YouTubeConfig {
// @Value("classpath:youtube/kkokkomu-mvp-31a5934a9b06.json")
// private Resource serviceAccountKey;
//
// private static final String APPLICATION_NAME = "kkokkomu-mvp";
// private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
//
// public YouTube getService() throws GeneralSecurityException, IOException {
// log.info("youtube get service");
// GoogleCredentials credentials = ServiceAccountCredentials.fromStream(serviceAccountKey.getInputStream())
// .createScoped(Collections.singleton("https://www.googleapis.com/auth/youtube.upload"));
//// Credential 내용 로그에 출력
// log.info("Credential: {}", credentials);
//
// return new YouTube.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, new HttpCredentialsAdapter(credentials))
// .setApplicationName(APPLICATION_NAME)
// .build();
// }
//}
106 changes: 53 additions & 53 deletions src/main/java/com/kkokkomu/short_news/service/S3Service.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
package com.kkokkomu.short_news.service;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import com.kkokkomu.short_news.exception.CommonException;
import com.kkokkomu.short_news.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Service
@RequiredArgsConstructor
public class S3Service {
private final AmazonS3Client s3Client;

@Value("${cloud.aws.s3.short-news}")
private String bucketShortNews;

public String uploadShortNews(MultipartFile file, Long newsId) {
log.info("upload ShortNews start");

try{
// 파일 이름에서 확장자 추출
String originalFileName = file.getOriginalFilename();
String fileExtension = "";
int dotIndex = originalFileName.lastIndexOf(".");
if (dotIndex != -1 && dotIndex < originalFileName.length() - 1) {
fileExtension = originalFileName.substring(dotIndex);
}

String fileName = newsId + fileExtension;
log.info("upload " + fileName);

// 파일 메타데이터 생성
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());

s3Client.putObject(new PutObjectRequest(bucketShortNews, fileName, file.getInputStream(), metadata)
.withCannedAcl(CannedAccessControlList.PublicRead));

return s3Client.getUrl(bucketShortNews, fileName).toString();
} catch (IOException e) {
log.error(e.getMessage());
throw new CommonException(ErrorCode.SHORTFORM_PROCESSING_ERROR);
}
}
}
//package com.kkokkomu.short_news.service;
//
//import com.amazonaws.services.s3.AmazonS3Client;
//import com.amazonaws.services.s3.model.*;
//import com.kkokkomu.short_news.exception.CommonException;
//import com.kkokkomu.short_news.exception.ErrorCode;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.IOException;
//
//@Slf4j
//@Service
//@RequiredArgsConstructor
//public class S3Service {
// private final AmazonS3Client s3Client;
//
// @Value("${cloud.aws.s3.short-news}")
// private String bucketShortNews;
//
// public String uploadShortNews(MultipartFile file, Long newsId) {
// log.info("upload ShortNews start");
//
// try{
// // 파일 이름에서 확장자 추출
// String originalFileName = file.getOriginalFilename();
// String fileExtension = "";
// int dotIndex = originalFileName.lastIndexOf(".");
// if (dotIndex != -1 && dotIndex < originalFileName.length() - 1) {
// fileExtension = originalFileName.substring(dotIndex);
// }
//
// String fileName = newsId + fileExtension;
// log.info("upload " + fileName);
//
// // 파일 메타데이터 생성
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setContentType(file.getContentType());
// metadata.setContentLength(file.getSize());
//
// s3Client.putObject(new PutObjectRequest(bucketShortNews, fileName, file.getInputStream(), metadata)
// .withCannedAcl(CannedAccessControlList.PublicRead));
//
// return s3Client.getUrl(bucketShortNews, fileName).toString();
// } catch (IOException e) {
// log.error(e.getMessage());
// throw new CommonException(ErrorCode.SHORTFORM_PROCESSING_ERROR);
// }
// }
//}

0 comments on commit 962c5c7

Please sign in to comment.