Skip to content

Commit

Permalink
# Fix: Gcp Credential 인식 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
gytjd committed Nov 25, 2024
1 parent b669de3 commit 2476c0d
Show file tree
Hide file tree
Showing 39 changed files with 29 additions and 366 deletions.
Binary file modified .gradle/8.10.2/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/8.10.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.10.2/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/8.10.2/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/8.10.2/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
3 changes: 0 additions & 3 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

182 changes: 0 additions & 182 deletions .idea/modules/AIWA-MCP-Backend-GcpService.McpBackend.test.iml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/modules/McpBackend.iml

This file was deleted.

155 changes: 0 additions & 155 deletions .idea/modules/McpBackend.main.iml

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
14 changes: 10 additions & 4 deletions src/main/java/AIWA/McpBackend/service/gcp/GcpResourceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ public class GcpResourceService {
private final RestTemplate restTemplate;

private GoogleCredentials getCredentials(String userId) throws IOException {
//credential파일 반환
String fileName = s3Service.downloadJsonFile(userId);
// JSON 키 파일 경로 설정
String credentialsPath = "tmp/"+fileName;
// credential 파일 다운로드 후 파일 경로 반환
String credentialsPath = s3Service.downloadJsonFile(userId);
if (credentialsPath == null) {
throw new IOException("Failed to download the credentials file.");
}

// 디버깅 코드로 경로 출력
System.out.println("Credential file path: " + credentialsPath);

// GoogleCredentials 반환
return GoogleCredentials.fromStream(new FileInputStream(credentialsPath))
.createScoped("https://www.googleapis.com/auth/cloud-platform");
}


/**
* 주소가 할당된 첫 번째 리소스의 이름을 추출하는 메서드
* @param address 고정 IP 주소 객체
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/AIWA/McpBackend/service/gcp/s3/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -56,6 +54,12 @@ public String downloadJsonFile(String userId) {
String prefix = "users/" + userId + "/GCP/";
String jsonFileName = null;

// tmp 디렉토리 존재 여부 확인 및 생성
File tmpDir = new File("tmp");
if (!tmpDir.exists()) {
tmpDir.mkdirs(); // 디렉토리 생성
}

ListObjectsV2Request request = ListObjectsV2Request.builder()
.bucket(bucketName)
.prefix(prefix)
Expand All @@ -76,27 +80,32 @@ public String downloadJsonFile(String userId) {
.build();
ResponseInputStream<GetObjectResponse> s3Object = s3Client.getObject(getObjectRequest);

// 파일 내용을 바이트 배열로 변환
try {
byte[] jsonFileData = s3Object.readAllBytes();
// 파일 저장 경로 지정
File localFile = new File("tmp/" + jsonFileName);
try (FileOutputStream outputStream = new FileOutputStream(localFile)) {
byte[] buffer = new byte[4096];
int length;
while ((length = s3Object.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
} catch (IOException e) {
// 예외 처리 코드
e.printStackTrace();
return null; // 오류 처리
}


break;
return localFile.getAbsolutePath(); // 파일 경로 반환
}
}
request = request.toBuilder()
.continuationToken(response.nextContinuationToken())
.build();
} while (response.isTruncated() && jsonFileName == null);

return jsonFileName; // 파일 이름 반환
return null; // 파일을 찾을 수 없을 때
}



/**
* S3에서 지정된 키에 해당하는 파일을 다운로드하여 문자열로 반환합니다.
*
Expand Down

0 comments on commit 2476c0d

Please sign in to comment.