Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i can't upload image using google cloud bucket;; what's wrong? #17168

Open
semin98 opened this issue Sep 26, 2024 · 0 comments
Open

i can't upload image using google cloud bucket;; what's wrong? #17168

semin98 opened this issue Sep 26, 2024 · 0 comments
Labels
type:question This issue asks a question (how to...).

Comments

@semin98
Copy link

semin98 commented Sep 26, 2024

<title>Document</title>
<!--<script src="/static/js/ckeditor5.js"></script>-->
<script src="https://cdn.ckeditor.com/ckeditor5/35.0.1/classic/ckeditor.js"></script>
editor
<textarea name="content" id="content">{{#data}}{{data.content}}{{/data}}</textarea>
<script>
ClassicEditor.create( document.querySelector( '#content' ), {
    language: "ko",
    ckfinder : {

        uploadUrl : "/image/upload",
    }
} )
.catch(error => {
    console.error(error);
});
</script>

package com.grepp.ck.controller;

//import com.grepp.ck.service.GCSFileUploadService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.ResponseEntity;
//import org.springframework.stereotype.Controller;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.MultipartRequest;
//
//import java.io.IOException;
//import java.util.Map;
//
//@RestController
//public class ImageUploadController {
//
// @Autowired
// private GCSFileUploadService gcsFileUploadService;
//
// @value("${gcp.bucket.name}")
// private String bucketName;
//
// @PostMapping("/image/upload")
// public ResponseEntity<?> uploadImage(@RequestParam("upload") MultipartFile file) throws IOException {
// try {
// String imageUrl = gcsFileUploadService.uploadFile(file);
//
// return ResponseEntity.ok("{"url":"" + imageUrl + ""}");
// } catch (IOException e) {
// return ResponseEntity.status(500).body("{"error":"" + e.getMessage() + ""}");
// }
// }

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class ImageUploadController {

@Autowired
private Storage storage;

@Value("${gcp.bucket.name}")
private String bucketName;

@PostMapping("/image/upload")
public ResponseEntity<?> uploadImage(@RequestParam("upload") MultipartFile file) {
    try {
        // GCS 버킷 객체를 가져옵니다.
        Bucket bucket = storage.get(bucketName);
        String fileName = file.getOriginalFilename();

        // GCS에 파일을 직접 업로드합니다.
        Blob blob = bucket.create(fileName, file.getBytes(), file.getContentType());

        // 업로드된 파일의 URL을 반환합니다.
        String imageUrl = blob.getMediaLink();

        // CKEditor의 응답 형식에 맞게 JSON 응답을 반환합니다.
        return ResponseEntity.ok("{\"url\":\"" + imageUrl + "\"}");

    } catch (Exception e) {
        // 오류가 발생하면 500 상태 코드와 함께 에러 메시지를 반환합니다.
        return ResponseEntity.status(500).body("{\"error\":\"" + e.getMessage() + "\"}");
    }
}

}

help me plz

@semin98 semin98 added the type:question This issue asks a question (how to...). label Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question This issue asks a question (how to...).
Projects
None yet
Development

No branches or pull requests

1 participant