반응형
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
public class GoogleDriveFileUploadExample {
private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_FILE);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
public static void uploadFile(MultipartFile file) throws IOException, GeneralSecurityException {
NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
File fileMetadata = new File();
fileMetadata.setName(file.getOriginalFilename());
InputStream inputStream = file.getInputStream();
File uploadedFile = service.files().create(fileMetadata, inputStream, file.getContentType()).execute();
System.out.println("File ID: " + uploadedFile.getId());
}
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
InputStream in = GoogleDriveFileUploadExample.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
}
Google Drive API에 연결하는 데 필요한 인증 정보를 가져옵니다. CREDENTIALS_FILE_PATH 변수에서 구글 개발자 콘솔에서 다운로드한 credentials.json 파일을 읽어들입니다.
반응형
'Spring Boot' 카테고리의 다른 글
[SpringBoot] 구글드라이브 파일 다운로드 구현 (0) | 2023.02.09 |
---|---|
[SpringBoot] 구글드라이브 파일리스트 취득 (0) | 2023.02.09 |
[SpringBoot] Spring Boot에서 Dropbox에 파일을 업로드 (0) | 2023.02.09 |
[SpringBoot] Spring Boot에서 Naver Blog 글쓰기를 구현 (0) | 2023.02.09 |
[JAVA] TCP/IP 데이터통신 파일암호화 전송 서버,클라이언트 (0) | 2023.02.09 |