Skip to content

Commit

Permalink
[#25] feat: Add GCP Storage method
Browse files Browse the repository at this point in the history
  • Loading branch information
hellouz818 committed Jun 4, 2022
1 parent 0823c76 commit cd9afc4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public NoticeTitleListDto saveNotice(
NoticeTitleListDto notice_title = noticeService.saveNotice(uploadfile, noticeRequestDto, request); //notice 저장
return notice_title;
}

@RequestMapping(value = "/notice/upload", method = RequestMethod.POST)
public String uploadObject(String objectName, String filePath) throws IOException{
return noticeService.uploadObjectimage(objectName, filePath);
}
/*
@PostMapping("/notice/test")
public List<NoticeSentenceDto> test(@RequestBody NoticeOCRDto dto, @RequestParam String lan) throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.translate.v3.*;
import com.google.cloud.translate.v3.LocationName;
import com.google.cloud.translate.v3.Translation;
Expand All @@ -31,6 +35,8 @@

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.*;

Expand Down Expand Up @@ -296,6 +302,30 @@ public Notice findNoticeById(Long id) {
);
}

public String uploadObjectimage(String objectName, String filePath) throws IOException{

String projectId = "solution-challenge-342914";
String bucketName = "notinote_bucket";

// The ID of your GCS object
//String objectName = "ex_notice.png";

// The path to your file to upload
//String filePath = "D:/ex_notice.png";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));

System.out.println(
"File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);

return "https://storage.googleapis.com/notinote_bucket/"+objectName;
}



/*public String dateDetect(Long nid) throws IOException {
Notice notice = noticeRepository.findByNid(nid);
String text = notice.getTrans_sum();
Expand Down

0 comments on commit cd9afc4

Please sign in to comment.