Java용 AWS SDK
    • PDF

    Java용 AWS SDK

    • PDF

    기사 요약

    Java용 SDK for S3 API

    AWS S3에서 제공하는 Java용 SDK를 이용해 네이버 클라우드 플랫폼의 Object Storage 서비스를 사용하는 예제입니다. AWS Java SDK 1.11.238 버전을 기준으로 작성되었습니다.

    SDK 설치

    SDK 소스를 직접 다운로드하거나 Apache Maven을 이용해 설치할 수 있습니다.

    예제

    예제는 다음과 같습니다.

    참고

    예제에서 사용하는 accessKey, secretKey 값에는 등록한 API 인증 키 정보를 입력해야 합니다.

    버킷 생성

    버킷 생성의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    
    try {
        // create bucket if the bucket name does not exist
        if (s3.doesBucketExistV2(bucketName)) {
            System.out.format("Bucket %s already exists.\n", bucketName);
        } else {
            s3.createBucket(bucketName);
            System.out.format("Bucket %s has been created.\n", bucketName);
        }
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    버킷 목록 조회

    버킷 목록 조회의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    try {
        List<Bucket> buckets = s3.listBuckets();
        System.out.println("Bucket List: ");
        for (Bucket bucket : buckets) {
            System.out.println("    name=" + bucket.getName() + ", creation_date=" + bucket.getCreationDate() + ", owner=" + bucket.getOwner().getId());
        }
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    버킷 삭제

    버킷 삭제의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    
    try {
        // delete bucket if the bucket exists
        if (s3.doesBucketExistV2(bucketName)) {
            // delete all objects
            ObjectListing objectListing = s3.listObjects(bucketName);
            while (true) {
                for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext();) {
                    S3ObjectSummary summary = (S3ObjectSummary)iterator.next();
                    s3.deleteObject(bucketName, summary.getKey());
                }
    
                if (objectListing.isTruncated()) {
                    objectListing = s3.listNextBatchOfObjects(objectListing);
                } else {
                    break;
                }
            }
    
            // abort incomplete multipart uploads
            MultipartUploadListing multipartUploadListing = s3.listMultipartUploads(new ListMultipartUploadsRequest(bucketName));
            while (true) {
                for (Iterator<?> iterator = multipartUploadListing.getMultipartUploads().iterator(); iterator.hasNext();) {
                    MultipartUpload multipartUpload = (MultipartUpload)iterator.next();
                    s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, multipartUpload.getKey(), multipartUpload.getUploadId()));
                }
    
                if (multipartUploadListing.isTruncated()) {
                    ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
                    listMultipartUploadsRequest.withUploadIdMarker(multipartUploadListing.getNextUploadIdMarker());
                    multipartUploadListing = s3.listMultipartUploads(listMultipartUploadsRequest);
                } else {
                    break;
                }
            }
    
            s3.deleteBucket(bucketName);
            System.out.format("Bucket %s has been deleted.\n", bucketName);
        } else {
            System.out.format("Bucket %s does not exist.\n", bucketName);
        }
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    파일 업로드

    파일 업로드의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    
    // create folder
    String folderName = "sample-folder/";
    
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(0L);
    objectMetadata.setContentType("application/x-directory");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName, new ByteArrayInputStream(new byte[0]), objectMetadata);
    
    try {
        s3.putObject(putObjectRequest);
        System.out.format("Folder %s has been created.\n", folderName);
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    
    // upload local file
    String objectName = "sample-object";
    String filePath = "/tmp/sample.txt";
    
    try {
        s3.putObject(bucketName, objectName, new File(filePath));
        System.out.format("Object %s has been created.\n", objectName);
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    파일 목록 조회

    파일 목록 조회의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    
    // list all in the bucket
    try {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
            .withBucketName(bucketName)
            .withMaxKeys(300);
    
        ObjectListing objectListing = s3.listObjects(listObjectsRequest);
    
        System.out.println("Object List:");
        while (true) {
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                System.out.println("    name=" + objectSummary.getKey() + ", size=" + objectSummary.getSize() + ", owner=" + objectSummary.getOwner().getId());
            }
    
            if (objectListing.isTruncated()) {
                objectListing = s3.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }
    } catch (AmazonS3Exception e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    
    // top level folders and files in the bucket
    try {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
            .withBucketName(bucketName)
            .withDelimiter("/")
            .withMaxKeys(300);
    
        ObjectListing objectListing = s3.listObjects(listObjectsRequest);
    
        System.out.println("Folder List:");
        for (String commonPrefixes : objectListing.getCommonPrefixes()) {
            System.out.println("    name=" + commonPrefixes);
        }
    
        System.out.println("File List:");
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println("    name=" + objectSummary.getKey() + ", size=" + objectSummary.getSize() + ", owner=" + objectSummary.getOwner().getId());
        }
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    파일 다운로드

    파일 다운로드의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    String objectName = "sample-object.txt";
    String downloadFilePath = "/tmp/sample-object.txt";
    
    // download object
    try {
        S3Object s3Object = s3.getObject(bucketName, objectName);
        S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
    
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFilePath));
        byte[] bytesArray = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = s3ObjectInputStream.read(bytesArray)) != -1) {
            outputStream.write(bytesArray, 0, bytesRead);
        }
    
        outputStream.close();
        s3ObjectInputStream.close();
        System.out.format("Object %s has been downloaded.\n", objectName);
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    파일 삭제

    파일 삭제의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    String objectName = "sample-object";
    
    // delete object
    try {
        s3.deleteObject(bucketName, objectName);
        System.out.format("Object %s has been deleted.\n", objectName);
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    ACL 설정

    ACL 설정의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    
    // set bucket ACL
    try {
        // get the current ACL
        AccessControlList accessControlList = s3.getBucketAcl(bucketName);
    
        // add read permission to anonymous
        accessControlList.grantPermission(GroupGrantee.AllUsers, Permission.Read);
    
        s3.setBucketAcl(bucketName, accessControlList);
    } catch (AmazonS3Exception e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    
    String objectName = "sample-object";
    String userId = "test-user-02";
    
    // set object ACL
    try {
        // get the current ACL
        AccessControlList accessControlList = s3.getObjectAcl(bucketName, objectName);
    
        // add read permission to user by ID
        accessControlList.grantPermission(new CanonicalGrantee(userId), Permission.Read);
    
        s3.setObjectAcl(bucketName, objectName, accessControlList);
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    멀티파트 업로드

    멀티파트 업로드의 예제는 다음과 같습니다.

    주의

    멀티파트 업로드를 완료하지 않을 경우에는 잔여 파일이 버킷에 남게 되며, 잔여 파일은 버킷 용량에 포함되어 과금됩니다. 다음 방법을 통해 불완전한 멀티파트 객체를 삭제하여 불필요하게 과금되지 않도록 주의해 주십시오.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    String objectName = "sample-large-object";
    
    File file = new File("/tmp/sample.file");
    long contentLength = file.length();
    long partSize = 10 * 1024 * 1024;
    
    try {
        // initialize and get upload ID
        InitiateMultipartUploadResult initiateMultipartUploadResult = s3.initiateMultipartUpload(new InitiateMultipartUploadRequest(bucketName, objectName));
        String uploadId = initiateMultipartUploadResult.getUploadId();
    
        // upload parts
        List<PartETag> partETagList = new ArrayList<PartETag>();
    
        long fileOffset = 0;
        for (int i = 1; fileOffset < contentLength; i++) {
            partSize = Math.min(partSize, (contentLength - fileOffset));
    
            UploadPartRequest uploadPartRequest = new UploadPartRequest()
                .withBucketName(bucketName)
                .withKey(objectName)
                .withUploadId(uploadId)
                .withPartNumber(i)
                .withFile(file)
                .withFileOffset(fileOffset)
                .withPartSize(partSize);
    
            UploadPartResult uploadPartResult = s3.uploadPart(uploadPartRequest);
            partETagList.add(uploadPartResult.getPartETag());
    
            fileOffset += partSize;
        }
    
        // abort
        // s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, objectName, uploadId));
    
        // complete
        CompleteMultipartUploadResult completeMultipartUploadResult = s3.completeMultipartUpload(new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETagList));
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    고객 제공 키를 사용한 암호화(SSE-C) 요청

    고객 제공 키를 사용한 암호화(SSE-C) 요청의 예제는 다음과 같습니다.

    참고

    SSE-C 기반으로 객체를 암호화할 경우, 콘솔에서 일부 요청을 사용할 수 없습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
     
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    // create encryption key
    KeyGenerator KEY_GENERATOR = KeyGenerator.getInstance("AES");
    KEY_GENERATOR.init(256, new SecureRandom());
    final SSECustomerKey SSE_KEY = new SSECustomerKey(KEY_GENERATOR.generateKey());
    
    
    String bucketName = "sample-bucket";
    
    // upload local file
    String objectName = "sample-object";
    String filePath = "/tmp/sample.txt";
    
    try {
    	PutObjectRequest putRequest = new PutObjectRequest(bucketName, objectName, new File(filePath))
        		 .withSSECustomerKey(SSE_KEY);
    	s3.putObject(putRequest);
        System.out.format("Object %s has been created.\n", objectName);
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    
    // download object
    String downloadPath = "/tmp/sample-object";
    
    try {
    	GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName)
        		.withSSECustomerKey(SSE_KEY);
        S3Object s3Object = s3.getObject(getObjectRequest);
        S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
    
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadPath));
        byte[] bytesArray = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = s3ObjectInputStream.read(bytesArray)) != -1) {
            outputStream.write(bytesArray, 0, bytesRead);
        }
    
        outputStream.close();
        s3ObjectInputStream.close();
        System.out.format("Object %s has been downloaded.\n", objectName);
    } catch (AmazonS3Exception e) {
        e.printStackTrace();
    } catch(SdkClientException e) {
        e.printStackTrace();
    }
    

    CORS (Cross-Origin Resource Sharing) 설정

    CORS 설정의 예제는 다음과 같습니다.

    final String endPoint = "https://kr.object.fin-ncloudstorage.com";
    final String regionName = "fin-standard";
    final String accessKey = "ACCESS_KEY";
    final String secretKey = "SECRET_KEY";
    
    // S3 client
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
        .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
        .build();
    
    String bucketName = "sample-bucket";
    
    // Create CORS rules.
    List<CORSRule.AllowedMethods> methodRule = new ArrayList<CORSRule.AllowedMethods>();
    methodRule.add(CORSRule.AllowedMethods.PUT);
    methodRule.add(CORSRule.AllowedMethods.GET);
    methodRule.add(CORSRule.AllowedMethods.POST);
    CORSRule rule = new CORSRule().withId("CORSRule")
    		.withAllowedMethods(methodRule)
    		.withAllowedHeaders(Arrays.asList(new String[] { "*" }))
    		.withAllowedOrigins(Arrays.asList(new String[] { "*" }))
    		.withMaxAgeSeconds(3000);
    
    List<CORSRule> rules = new ArrayList<CORSRule>();
    rules.add(rule);
    
    // Add rules to new CORS configuration.
    BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();
    configuration.setRules(rules);
    
    // Set the rules to CORS configuration.
    s3.setBucketCrossOriginConfiguration(bucketName, configuration);
    
    // Get the rules for CORS configuration.
    configuration = s3.getBucketCrossOriginConfiguration(bucketName);
           
    if (configuration == null) {
        System.out.println("Configuration is null.");
    } else {
        System.out.println("Configuration has " + configuration.getRules().size() + " rules\n");
    
        for (CORSRule getRule : configuration.getRules()) {
            System.out.println("Rule ID: " + getRule.getId());
            System.out.println("MaxAgeSeconds: " + getRule.getMaxAgeSeconds());
            System.out.println("AllowedMethod: " + getRule.getAllowedMethods());
            System.out.println("AllowedOrigins: " + getRule.getAllowedOrigins());
            System.out.println("AllowedHeaders: " + getRule.getAllowedHeaders());
            System.out.println("ExposeHeader: " + getRule.getExposedHeaders());
            System.out.println();
        }
    }
    

    이 문서가 도움이 되었습니까?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.