SDK 가이드
    • PDF

    SDK 가이드

    • PDF

    기사 요약

    Java SDK for S3 API

    AWS S3에서 제공하는 Java SDK를 이용하여 네이버 클라우드 플랫폼 Object Storage를 사용하는 방법을 설명합니다.
    이 문서는 AWS Java SDK 1.11.238 버전을 기준으로 작성되었습니다.

    설치

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

    소스 다운로드

    AWS Java SDK GitHub : https://github.com/aws/aws-sdk-java/tree/master/aws-java-sdk-s3

    Apache Maven으로 이용하기

    pom.xml

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.11.238</version>
    </dependency>
    

    예제

    예제에서 사용하는 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";
    String downloadPath = "/tmp/sample-object";
    
    // download object
    try {
        S3Object s3Object = s3.getObject(bucketName, objectName);
        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();
    }
    

    오브젝트 삭제

    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 설정

    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 기반으로 오브젝트를 암호화 할 경우 콘솔에서 일부 요청을 사용할 수 없습니다.

    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) 설정

    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();
        }
    }
    


    Python SDK for S3 API

    AWS S3에서 제공하는 Python SDK를 이용하여 네이버 클라우드 플랫폼 Object Storage를 사용하는 방법을 설명합니다.

    이 문서는 AWS Python SDK 1.6.19 버전을 기준으로 작성되었습니다.

    설치

    pip install boto3==1.6.19
    
    참고

    예제

    예제에서 사용하는 access_key, secret_key 는 등록한 API 인증키 정보로 입력해야 합니다.

    버킷 생성

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
    
        bucket_name = 'sample-bucket'
    
        s3.create_bucket(Bucket=bucket_name)
    
    

    버킷 목록 조회

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
    
        response = s3.list_buckets()
        
        for bucket in response.get('Buckets', []):
            print (bucket.get('Name'))
    
    

    버킷 삭제

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
    
        bucket_name = 'sample-bucket'
    
        s3.delete_bucket(Bucket=bucket_name)
    
    

    오브젝트 업로드

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
    
        bucket_name = 'sample-bucket'
    
        # create folder
        object_name = 'sample-folder/'
    
        s3.put_object(Bucket=bucket_name, Key=object_name)
    
        # upload file
        object_name = 'sample-object'
        local_file_path = '/tmp/test.txt'
    
        s3.upload_file(local_file_path, bucket_name, object_name)
    
    

    오브젝트 목록 조회

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
    
        bucket_name = 'sample-bucket'
    
        # list all in the bucket
        max_keys = 300
        response = s3.list_objects(Bucket=bucket_name, MaxKeys=max_keys)
    
        print('list all in the bucket')
    
        while True:
            print('IsTruncated=%r' % response.get('IsTruncated'))
            print('Marker=%s' % response.get('Marker'))
            print('NextMarker=%s' % response.get('NextMarker'))
    
            print('Object List')
            for content in response.get('Contents'):
                print(' Name=%s, Size=%d, Owner=%s' % \
                      (content.get('Key'), content.get('Size'), content.get('Owner').get('ID')))
    
            if response.get('IsTruncated'):
                response = s3.list_objects(Bucket=bucket_name, MaxKeys=max_keys,
                                           Marker=response.get('NextMarker'))
            else:
                break
    
        # top level folders and files in the bucket
        delimiter = '/'
        max_keys = 300
    
        response = s3.list_objects(Bucket=bucket_name, Delimiter=delimiter, MaxKeys=max_keys)
    
        print('top level folders and files in the bucket')
    
        while True:
            print('IsTruncated=%r' % response.get('IsTruncated'))
            print('Marker=%s' % response.get('Marker'))
            print('NextMarker=%s' % response.get('NextMarker'))
    
            print('Folder List')
            for folder in response.get('CommonPrefixes'):
                print(' Name=%s' % folder.get('Prefix'))
    
            print('File List')
            for content in response.get('Contents'):
                print(' Name=%s, Size=%d, Owner=%s' % \
                      (content.get('Key'), content.get('Size'), content.get('Owner').get('ID')))
    
            if response.get('IsTruncated'):
                response = s3.list_objects(Bucket=bucket_name, Delimiter=delimiter, MaxKeys=max_keys,
                                           Marker=response.get('NextMarker'))
            else:
                break
    
    

    오브젝트 다운로드

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
        bucket_name = 'sample-bucket'
    
        object_name = 'sample-object'
        local_file_path = '/tmp/test.txt'
    
        s3.download_file(bucket_name, object_name, local_file_path)
    
    

    오브젝트 삭제

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
        
        bucket_name = 'sample-bucket'
        object_name = 'sample-object'
    
        s3.delete_object(Bucket=bucket_name, Key=object_name)
    
    

    ACL 설정

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
        bucket_name = 'sample-bucket'
    
        # set bucket ACL
        # add read permission to anonymous
        s3.put_bucket_acl(Bucket=bucket_name, ACL='public-read')
    
        response = s3.get_bucket_acl(Bucket=bucket_name)
    
        # set object ACL
        # add read permission to user by ID
        object_name = 'sample-object'
        owner_id = 'test-owner-id'
        target_id = 'test-user-id'
    
        s3.put_object_acl(Bucket=bucket_name, Key=object_name,
                          AccessControlPolicy={
                              'Grants': [
                                  {
                                      'Grantee': {
                                          'ID': owner_id,
                                          'Type': 'CanonicalUser'
                                      },
                                      'Permission': 'FULL_CONTROL'
                                  },
                                  {
                                      'Grantee': {
                                          'ID': target_id,
                                          'Type': 'CanonicalUser'
                                      },
                                      'Permission': 'READ'
                                  }
                              ],
                              'Owner': {
                                  'ID': owner_id
                              }
                          })
    
        response = s3.get_object_acl(Bucket=bucket_name, Key=object_name)
    
    

    Multipart Upload

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY_ID'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
        bucket_name = 'sample-bucket'
        object_name = 'sample-large-object'
        
        local_file = '/tmp/sample.file'
    
        # initialize and get upload ID
        create_multipart_upload_response = s3.create_multipart_upload(Bucket=bucket_name, Key=object_name)
        upload_id = create_multipart_upload_response['UploadId']
    
        part_size = 10 * 1024 * 1024
        parts = []
    
        # upload parts
        with open(local_file, 'rb') as f:
            part_number = 1
            while True:
                data = f.read(part_size)
                if not len(data):
                    break
                upload_part_response = s3.upload_part(Bucket=bucket_name, Key=object_name, PartNumber=part_number, UploadId=upload_id, Body=data)
                parts.append({
                    'PartNumber': part_number,
                    'ETag': upload_part_response['ETag']
                })
                part_number += 1
    
        multipart_upload = {'Parts': parts}
    
        # abort multipart upload
        # s3.abort_multipart_upload(Bucket=bucket_name, Key=object_name, UploadId=upload_id)
    
        # complete multipart upload
        s3.complete_multipart_upload(Bucket=bucket_name, Key=object_name, UploadId=upload_id, MultipartUpload=multipart_upload)
    
    

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

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

    import boto3
    import secrets
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY_ID'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        # S3 client
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
    
        # create encryption key
        sse_key = secrets.token_bytes(32)
        sse_conf = { "SSECustomerKey": sse_key, "SSECustomerAlgorithm": "AES256"}
    
        bucket_name = 'sample-bucket'
        object_name = 'sample-object'
        
        # upload object
        local_file_path = '/tmp/sample.txt'
        s3.upload_file(local_file_path, bucket_name, object_name, sse_conf)
    
        # download object
        download_file_path = '/tmp/sample-download.txt'
        s3.download_file(bucket_name, object_name, download_file_path, sse_conf)
    
    

    CORS(Cross-Origin Resource Sharing) 설정

    import boto3
    
    service_name = 's3'
    endpoint_url = 'https://kr.object.fin-ncloudstorage.com'
    region_name = 'fin-standard'
    access_key = 'ACCESS_KEY_ID'
    secret_key = 'SECRET_KEY'
    
    if __name__ == "__main__":
        s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                          aws_secret_access_key=secret_key)
                          
        bucket_name = 'sample-bucket'
    
    # Define the configuration rules
        cors_configuration = {
            'CORSRules': [{
                'AllowedHeaders': ['*'],
                'AllowedMethods': ['GET', 'PUT'],
                'AllowedOrigins': ['*'],
                'MaxAgeSeconds': 3000
            }]
        }
    
        # Set CORS configuration
        s3.put_bucket_cors(Bucket=bucket_name,
                        CORSConfiguration=cors_configuration)
    
        # Get CORS configuration
        response = s3.get_bucket_cors(Bucket=bucket_name)
        print(response['CORSRules'])
    


    Javascript SDK for S3 API

    AWS S3에서 제공하는 Javascript SDK를 이용하여 네이버 클라우드 플랫폼 Object Storage를 사용하는 방법을 설명합니다.

    이 문서는 AWS Javascript SDK 2.348.0 버전을 기준으로 작성되었습니다.

    설치

    npm install --save aws-sdk@2.348.0
    

    AWS Javascript SDK

    예제

    예제에서 사용하는 access_key, secret_key는 등록한 API 인증키 정보로 입력해야 합니다.

    버킷 생성

    const AWS = require('aws-sdk');
    
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    
    const bucket_name = 'sample-bucket';
    
    
    (async () => {
    
        await S3.createBucket({
            Bucket: bucket_name,
            CreateBucketConfiguration: {}
        }).promise()
    
    })();
    

    버킷 목록 조회

    const AWS = require('aws-sdk');
    
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    (async () => {
    
        let { Buckets } = await S3.listBuckets().promise();
    
        for(let bucket of Buckets) {
            console.log(bucket.Name);
        }
    
    })();
    

    버킷 삭제

    const AWS = require('aws-sdk');
    
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    
    
    const bucket_name = 'sample-bucket';
    
    (async () => {
    
        await S3.deleteBucket({
            Bucket: bucket_name
        }).promise();
    
    })();
    

    오브젝트 업로드

    const AWS = require('aws-sdk');
    const fs = require('fs');
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    
    const bucket_name = 'sample-bucket';
    const local_file_path = '/tmp/test.txt';
    
    
    
    (async () => {
    
        let object_name = 'sample-folder/';
        // create folder
        await S3.putObject({
            Bucket: bucket_name,
            Key: object_name
        }).promise();
    
        object_name = 'sample-object';
    
        // upload file
        await S3.putObject({
            Bucket: bucket_name,
            Key: object_name,
            ACL: 'public-read',
            // ACL을 지우면 전체공개가 되지 않습니다.
            Body: fs.createReadStream(local_file_path)
        }).promise();
    
    })();
    
    

    오브젝트 목록 조회

    const AWS = require('aws-sdk');
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    const bucket_name = 'sample-bucket';
    const MAX_KEYS = 300;
    
    var params = {
        Bucket: bucket_name,
        MaxKeys: MAX_KEYS,
        FetchOwner: true
    };
    
    (async () => {
    
        // List All Objects
        console.log("List All In The Bucket");
        console.log("==========================");
    
        while (true) {
        
            let response = await S3.listObjectsV2(params).promise();
    
            console.log(`IsTruncated = ${response.IsTruncated}`);
            console.log(
                `ContinuationToken = ${response.ContinuationToken ? response.ContinuationToken : null}`
            );
            console.log(
                `NextContinuationToken = ${
                    response.NextContinuationToken ? response.NextContinuationToken : null
                }`
            );
            console.log(`  Object Lists`);
            for (let content of response.Contents) {
                console.log(
                    `    Name = ${content.Key}, Size = ${content.Size}, Owner = ${content.Owner.ID}`
                );
            }
    
            if (response.IsTruncated) {
                params.ContinuationToken = response.NextContinuationToken;
            } else {
                break;
            }
            
        }
    
        // List Top Level Folder And Files
        params.Delimiter = "/";
        console.log("Top Level Folders And Files In The Bucket");
        console.log("==========================");
    
        while (true) {
        
            let response = await S3.listObjectsV2(params).promise();
    
            console.log(`IsTruncated = ${response.IsTruncated}`);
            console.log(
                `ContinuationToken = ${response.ContinuationToken ? response.ContinuationToken : null}`
            );
            console.log(
                `NextContinuationToken = ${
                    response.NextContinuationToken ? response.NextContinuationToken : null
                }`
            );
    
            console.log(`  Folder Lists`);
            for (let folder of response.CommonPrefixes) {
                console.log(`    Name = ${folder.Prefix}`);
            }
    
            console.log(`  File Lists`);
            for (let content of response.Contents) {
                console.log(
                    `    Name = ${content.Key}, Size = ${content.Size}, Owner = ${content.Owner.ID}`
                );
            }
    
            if (response.IsTruncated) {
                params.ContinuationToken = response.NextContinuationToken;
            } else {
                break;
            }
        }
        
    })();
    

    오브젝트 다운로드

    const AWS = require('aws-sdk');
    const fs = require('fs');
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    const bucket_name = 'sample-bucket';
    const object_name = 'sample-object';
    const local_file_path = '/tmp/test.txt';
    
    
    (() => {
    
        let outStream = fs.createWriteStream(local_file_path);
        let inStream = S3.getObject({
            Bucket: bucket_name,
            Key: object_name
        }).createReadStream();
    
        inStream.pipe(outStream);
        inStream.on('end', () => {
            console.log("Download Done");
        });
    
    })();
    

    오브젝트 삭제

    const AWS = require('aws-sdk');
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    const bucket_name = 'sample-bucket';
    
    (async () => {
    
        // Delete Folder
        let object_name = 'sample-folder/';
    
        await S3.deleteObject({
            Bucket: bucket_name,
            Key: object_name
        }).promise();
    
        // Delete File
        object_name = 'sample-object';
    
        await S3.deleteObject({
            Bucket: bucket_name,
            Key: object_name
        }).promise();
    
    })();
    

    ACL 설정

    const AWS = require('aws-sdk');
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    const bucket_name = 'sample-bucket'
    const object_name = 'sample-object'
    const owner_id = 'test-owner-id'
    const target_id = 'test-user-id'
    
    (async () => {
    
        await S3.putObjectAcl({
            Bucket: bucket_name,
            Key: object_name,
            AccessControlPolicy: {
                'Grants': [
                    {
                        'Grantee': {
                            'ID': owner_id,
                            'Type': 'CanonicalUser'
                        },
                        'Permission': 'FULL_CONTROL'
                    },
                    {
                        'Grantee': {
                            'ID': target_id,
                            'Type': 'CanonicalUser'
                        },
                        'Permission': 'READ'
                    }
                ],
                'Owner': {
                    'ID': owner_id
                }
            }
        }).promise();
    
    })();
    

    Multipart Upload

    const AWS = require('aws-sdk');
    const fs = require('fs');
    const endpoint = new AWS.Endpoint('https://kr.object.fin-ncloudstorage.com');
    const region = 'fin-standard';
    const access_key = 'ACCESS_KEY';
    const secret_key = 'SECRET_KEY';
    
    const S3 = new AWS.S3({
        endpoint,
        region,
        credentials: {
            accessKeyId : access_key,
            secretAccessKey: secret_key
        }
    });
    
    const bucket_name = 'sample-bucket';
    const local_file_name = '/tmp/sample.file';
    const object_name = 'sample-large-object';
    
    let options = {
        partSize: 5 * 1024 * 1024
    };
    
    
    (async () => {
    
        await S3.upload({
            Bucket: bucket_name,
            Key: object_name,
            Body: fs.createReadStream(local_file_name)
        }, options).promise();
    
    })();
    

    CORS(Cross-Origin Resource Sharing) 설정

    const AWS = require("aws-sdk");
    
    const endpoint = new AWS.Endpoint("https://kr.object.fin-ncloudstorage.com");
    const region = 'fin-standard';
    const access_key = "<ACCESS_KEY>";
    const secret_key = "<SECRET_KEY>";
    
    const S3 = new AWS.S3({
      endpoint,
      region,
      credentials: {
        accessKeyId: access_key,
        secretAccessKey: secret_key,
      },
    });
    
    const bucket_name = "sample-bucket";
    
    const params = {
      Bucket: bucket_name,
      CORSConfiguration: {
        CORSRules: [
          {
            AllowedHeaders: ["*"],
            AllowedMethods: ["GET", "PUT"],
            AllowedOrigins: ["*"],
            MaxAgeSeconds: 3000,
          },
        ],
      },
    };
    
    (async () => {
    // Set CORS
      await S3.putBucketCors(params).promise();
      
    // Get CORS
      const response = await S3.getBucketCors({ Bucket: bucket_name }).promise();
      console.log(JSON.stringify(response, null, 2));
    })();
    


    CLI

    AWS S3에서 제공하는 CLI를 이용하여 네이버 클라우드 플랫폼 Object Storage를 사용하는 방법을 설명합니다.
    자세한 내용은 Object Storage CLI 사용 가이드를 참고해 주십시오.


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

    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.