아무거나

[aws-java-sdk] S3 Bucket 조회 본문

Infra/AWS

[aws-java-sdk] S3 Bucket 조회

전봉근 2019. 6. 21. 10:27
반응형

// s3 bucket 가져오기

@Repository
public class S3BucketService {

    private static String bucketName = "deploy.versioning";
    private static String bucketFolderRoot = "project";
    private static String accessKey = {accesskey};
    private static String key = {key};

    @Autowired
    private ProjectDAOServiceImpl deployDAOService;

    // 전체 버킷 조회 서비스 모델
    public HashMap<String, ArrayList<String>> getS3Bucket() {
        ArrayList<String> projectArr = new ArrayList<String>();
        HashMap<String, ArrayList<String>> dataMap = new HashMap<String, ArrayList<String>>();

        AWSCredentials crd = new BasicAWSCredentials(accessKey, key);
        AmazonS3 s3Client = new AmazonS3Client(crd);

        try {
            ObjectListing bucketObject = s3Client.listObjects(bucketName, "project");

            // objectSummary 는 s3 버킷객체
            for (S3ObjectSummary objectSummary : bucketObject.getObjectSummaries()) {
                String[] keyArr = objectSummary.getKey().split("/");

                if(keyArr.length == 2) {
                    projectArr.add(keyArr[1]);
                }
        	}

        	dataMap.put("data", projectArr);
        } catch(AmazonServiceException ase) {
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        }

        return dataMap;
    } 
}

 

// 버킷 프로젝트별 서비스 모델 

public HashMap<String, HashMap<String, ArrayList<String>>> getS3BucketVersion(String projectName, String bucketType) throws ParseException {
    HashMap<String, HashMap<String, ArrayList<String>>> dataMap = new HashMap<String, HashMap<String, ArrayList<String>>>();
    HashMap<String, ArrayList<String>> buildMap = new HashMap<String, ArrayList<String>>();

    ArrayList<String> devBuildArr = new ArrayList<String>();
    ArrayList<String> qaBuildArr = new ArrayList<String>();
    ArrayList<String> liveBuildArr = new ArrayList<String>();

    AWSCredentials crd = new BasicAWSCredentials(accessKey, key);
    AmazonS3 s3Client = new AmazonS3Client(crd);

    String bucketFolderName = bucketFolderRoot + "/" + projectName;

    try {
        ObjectListing objects = s3Client.listObjects(bucketName, bucketFolderName);

        for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
            if(objectSummary.getSize() > 0) {

                String[] keyArr = objectSummary.getKey().split("/");

                // live current version date put
                if (keyArr[2].equals("develop")) { // chatbot
                    devBuildArr.add(keyArr[3]);
                    buildMap.put(keyArr[2], devBuildArr);
                } else if (keyArr[2].equals("dev")) {
                    devBuildArr.add(keyArr[3]);
                    buildMap.put(keyArr[2], devBuildArr);
                } else if (keyArr[2].equals("qa")) {
                    qaBuildArr.add(keyArr[3]);
                    buildMap.put(keyArr[2], qaBuildArr);
                } else {
                    liveBuildArr.add(keyArr[3]);
                    buildMap.put(keyArr[2], liveBuildArr);
                }
            } // size end if
        } // end for

        dataMap.put("data", buildMap);

    } catch(AmazonServiceException ase) {
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }

    return dataMap;
}  

 

반응형
Comments