반응형
Google Drive API의 files().list() 메소드를 호출하여 파일 목록을 가져옵니다. 결과로 반환된 FileList 객체에서 파일 목록을 가져와서 각 파일의 이름과 ID를 출력합니다.
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();
getFileList(service);
}
public void getFileList(Drive driveService) throws IOException {
FileList result = driveService.files().list().setFields("nextPageToken, files(id, name, createdTime)").execute();
List<com.google.api.services.drive.model.File> files = result.getFiles();
for (com.google.api.services.drive.model.File file : files) {
System.out.println("File Name: " + file.getName() + " File Id: " + file.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");
}
}
반응형
'Spring Boot' 카테고리의 다른 글
[SpringBoot] iText7으로 PDF출력 (샘플 이미지, 표 추가 소스) (0) | 2023.02.10 |
---|---|
[SpringBoot] 구글드라이브 파일 다운로드 구현 (0) | 2023.02.09 |
[SpringBoot] Spring Boot에서 Google Drive에 파일을 업로드 (0) | 2023.02.09 |
[SpringBoot] Spring Boot에서 Dropbox에 파일을 업로드 (0) | 2023.02.09 |
[SpringBoot] Spring Boot에서 Naver Blog 글쓰기를 구현 (0) | 2023.02.09 |