Skip to main content

Use Object Storage with S3 API

This tutorial explains how to manage KakaoCloud Object Storage buckets and objects using the S3 API.

Basic information

About this scenario​

KakaoCloud Object Storage provides some APIs compatible with AWS S3, allowing you to integrate it into environments that already use S3. This tutorial explains how to manage buckets and objects using AWS CLI, Python Boto3, and the Java and Go SDKs with an S3 access key issued from the console.

The tutorial covers the following:

  • Issue an S3 access key in the console
  • Manage buckets and objects using AWS CLI
  • Call the S3 API using the Boto3, Java, and Go SDKs

If you have tools or applications built for AWS S3, you can change their configuration to migrate to KakaoCloud Object Storage. See the Migrate data from AWS S3 to KakaoCloud Object Storage tutorial.

Before you start​

Issue credentials for S3 API​

You can issue an S3 access key in the KakaoCloud console.

  1. Sign in to the KakaoCloud console and select Profile icon > Credentials in the upper-right corner.

  2. Re-enter your password and select Confirm Password.

  3. Go to the S3 Access Key tab and select [Create S3 Access Key].

  4. Enter the required information in the dialog and select [Create] to issue an access key and a secret access key.

    info
    • You can create up to two S3 access keys per project.
    • Issued keys automatically expire when the user is removed from the project.
  5. Store the issued keys securely and use them when configuring the CLI and SDKs.

Type 1. AWS CLI example​

This is an example of using KakaoCloud Object Storage with the AWS Command Line Interface (CLI).

AWS CLI configuration​

Set up the credentials and environment for running AWS CLI. If AWS CLI is not installed, install AWS CLI first.

  1. Install AWS CLI.

    info

    The latest S3 CLI version compatible with KakaoCloud is 2.22.0. S3 commands may not work as expected with later versions.

    curl
    curl "https://awscli.amazonaws.com/AWSCLIV2-2.22.0.pkg" -o "AWSCLIV2-2.22.0.pkg"
    sudo installer -pkg AWSCLIV2-2.22.0.pkg -target /
  2. Verify that the installation was successful by running the following command.

    Verify AWS CLI installation
    # Check the installation path
    which aws
    # Command output: /opt/homebrew/bin/aws

    # Check version
    aws --version
    # Command output: aws-cli/2.22.0 Python/3.12.6 Darwin/25.6.0 exe/x86_64

To proceed, check the S3 credentials.

  1. Use the configure command to set up the credentials.

    aws configure
  2. Enter your credentials based on the following:

    AWS Access key ID: {S3_ACCESS_KEY}
    AWS Secret access key: {S3_SECRET_ACCESS_KEY}
    Default region name: kr-central-2
    Default output format:
  3. Once the configuration is complete, you can use the s3 command as follows:

    aws --endpoint-url={endpoint} s3 {command} s3://{bucket}
    # Example
    # aws --endpoint-url=https://objectstorage.kr-central-2.kakaocloud.com s3 ls
    RegionEndpoint
    kr-central-2https://objectstorage.kr-central-2.kakaocloud.com

AWS CLI example​

Create a bucket
aws --endpoint-url={endpoint} s3 mb s3://{bucket_name}
List all buckets
aws --endpoint-url={endpoint} s3 ls
List the contents of a specific bucket
aws --endpoint-url={endpoint} s3 ls s3://{bucket_name}
Delete bucket
aws --endpoint-url={endpoint} s3 rb s3://{bucket_name}
Upload file
aws --endpoint-url={endpoint} s3 cp {local_path} s3://{bucket_name}/{upload_path}
Download file
aws --endpoint-url={endpoint} s3 cp s3://{bucket_name}/{file_path} {local_path}
Delete file
aws --endpoint-url={endpoint} s3 rm s3://{bucket_name}/{file_path}

Type 2. Python SDK(Boto3) example​

This is an example of using KakaoCloud Object Storage with the AWS Python SDK (Boto3).

Create and configure S3 client​

  1. Install Boto3 using pip.

    pip install boto3
    info

    Boto3 is currently supported only on Python 3.8 or higher.

  2. Configure the client with your credentials and environment information.

    import boto3

    client = boto3.client(
    region_name="kr-central-2",
    endpoint_url="{ENDPOINT}",
    aws_access_key_id="{S3_ACCESS_KEY}",
    aws_secret_access_key="{S3_SECRET_ACCESS_KEY}",
    service_name="s3"
    )

SDK usage example​

Create bucket
def create_bucket(bucket_name):
try:
return client.create_bucket(Bucket=bucket_name)
except Exception as e:
raise #...
List all buckets
def get_list_buckets() :
try:
response = client.list_buckets()
return [bucket.get('Name') for bucket in response.get('Buckets', [])]
except Exception as e:
raise #...
List the contents of a specific bucket
def get_list_objects(bucket_name):
try:
response = client.list_objects(Bucket=bucket_name)
return [obj.get('Key') for obj in response.get('Contents', [])]
except Exception as e:
raise #...
Delete bucket
def delete_bucket(bucket_name):
try:
return client.delete_bucket(Bucket=bucket_name)
except Exception as e:
raise #...
Upload file
# Upload a file to a specific bucket
# Upload file to the bucket
def upload_file(local_path, bucket_name, file_name) :
try :
# client.upload_file('/Documents/hello.jpeg', 'bucket', 'hello.jpeg')
return client.upload_file(local_path, bucket_name, file_name)
except Exception as e:
raise
Download file
# file_name : Name of the file to download
# local_path : Local path and filename to save the downloaded file
def download_file(bucket_name, file_name, local_path) :
try :
# client.download_file('bucket', 'hello.jpeg', '/Downloads/hello.jpeg')
return client.download_file(bucket_name, file_name, local_path)
except Exception as e:
raise
Delete file
def delete_object(bucket_name, file_name) :
try :
return client.delete_object(Bucket=bucket_name, Key=file_name)
except Exception as e :
raise

Type 3. Java SDK example​

This is an example of using KakaoCloud Object Storage with the AWS Java SDK. This document is based on aws-java-sdk-v2.

Create and configure S3 client​

  1. Add the following dependencies to your pom.xml.

    <dependencies>
    <dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.23.7</version>
    </dependency>

    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.32</version>
    </dependency>
    <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.4.12</version>
    </dependency>
    </dependencies>
  2. Rebuild Maven to apply the added dependencies.

  1. Rebuild Gradle to apply the added dependencies.

    String s3Endpoint = "https://objectstorage.kr-central-2.kakaocloud.com";
    String accessKey = "{S3_ACCESS_KEY}";
    String secretAccessKey = "{S3_SECRET_ACCESS_KEY}";
    String region = "kr-central-2";


    final S3Client client = S3Client.builder()
    .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretAccessKey)))
    .endpointOverride(URI.create(s3Endpoint))
    .forcePathStyle(true)
    .region(Region.of(region))
    .build();

SDK usage example​

Create a bucket
private void createBucket(S3Client client, String bucketName) {
try {
CreateBucketResponse res = client.createBucket(
CreateBucketRequest.builder()
.bucket(bucketName)
.build());
}
catch (Exception e){
e.printStackTrace();
}
}
List all buckets
private void listBuckets(S3Client client) {
try {
ListBucketsResponse res = client.listBuckets();
System.out.println(res);
}
catch (Exception e){
e.printStackTrace();
}
}
List the contents of a specific bucket
private void listObjects(S3Client client, String bucketName) {
try {
ListObjectsResponse res = client.listObjects(
ListObjectsRequest.builder()
.bucket(bucketName)
.build());
System.out.println(res);
}
catch (Exception e){
e.printStackTrace();
}
}
Delete bucket
private void deleteBucket(S3Client client, String bucketName) {
try {
DeleteBucketResponse res = client.deleteBucket(
DeleteBucketRequest.builder()
.bucket(bucketName)
.build());
System.out.println(res);
}
catch (Exception e){
e.printStackTrace();
}
}
Upload file
// objectKey : Name of the file to upload
// filePath : Local path where the file to upload is located
private void uploadObject(S3Client client, String bucketName, String objectKey, String filePath){
try {
Path path = Paths.get(filePath);
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build();

client.putObject(putObjectRequest, path);
} catch (Exception e){
e.printStackTrace();
}
}
Download file
// objectKey : Name of the file to download
// filePath : Local path and filename to save the downloaded file
private void downloadObject(S3Client client, String bucketName, String objectKey, String filePath){
try {
Path path = Paths.get(filePath);
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build();

ResponseBytes<GetObjectResponse> responseBytes = client.getObjectAsBytes(getObjectRequest);

byte[] data = responseBytes.asByteArray();

File myFile = new File(filePath);
OutputStream os = new FileOutputStream(myFile);
os.write(data);
os.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (S3Exception e){
e.printStackTrace();
}
}
Delete file
// objectKey : File name to delete
private void deleteObject(S3Client client, String bucketName, String objectKey){
try {
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build();

client.deleteObject(deleteObjectRequest);

} catch (Exception e){
e.printStackTrace();
}
}

Type 4. Go SDK example​

This is an example of using KakaoCloud Object Storage with the AWS Go SDK.

Create and configure S3 client​

  1. Initialize the local project using the following command.

    go mod init {project_name}
  2. Fetch AWS SDK packages for Go V2 using Go modules.

    go get github.com/aws/aws-sdk-go-v2/config
    go get github.com/aws/aws-sdk-go-v2/credentials
    go get github.com/aws/aws-sdk-go-v2/service/s3
  3. Configure the client with your credentials and environment information.

    var accessKeyId = "{S3_ACCESS_KEY}"
    var accessKeySecret = "{S3_SECRET_ACCESS_KEY}"
    var endpoint = "{ENDPOINT}"
    var region = "kr-central-2"
    resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
    return aws.Endpoint{
    URL: endpoint,
    }, nil
    })

    cfg, err := config.LoadDefaultConfig(context.TODO(),
    config.WithRegion(region),
    config.WithEndpointResolverWithOptions(resolver),
    config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")),
    )
    if err != nil {
    log.Fatal(err)
    }

    client := s3.NewFromConfig(cfg, func(options *s3.Options) {
    options.UsePathStyle = true
    })

SDK usage example​

Create a bucket
bucketname := "{BUCKET_NAME}"
_, err = client.CreateBucket(context.TODO(), &s3.CreateBucketInput{
Bucket: &bucketname,
})
if err != nil {
log.Fatal(err)
}
List all buckets
_, err = client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
if err != nil {
log.Fatal(err)
}
List the contents of a specific bucket
bucketname := "{BUCKET_NAME}"
_, err = client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: &bucketname,
})
if err != nil {
log.Fatal(err)
}
Delete bucket
bucketname := "{BUCKET_NAME}"
_, err = client.DeleteBucket(context.TODO(), &s3.DeleteBucketInput{
Bucket: &bucketname,
})
if err != nil {
log.Fatal(err)
}
Upload file
bucketname := "{BUCKET_NAME}"
filePath := "{LOCAL_PATH}" // Local path where the file to upload is located
objectKey := "{FILE_NAME}" // File name to upload

file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()

_, err = client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: &bucketname,
Key: &objectKey,
Body: file,
})
if err != nil {
log.Fatal(err)
}
Download file
bucketname := "{BUCKET_NAME}"
filePath := "{LOCAL_PATH}" // Local path and filename to save the downloaded file
objectKey := "{FILE_NAME}" // Name of the file to download

file, err := os.Create(filePath)
if err != nil {
panic(err)
}
defer file.Close()

result, err := client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: &bucketname,
Key: &objectKey,
})
if err != nil {
log.Fatal(err)
}

_, err = io.Copy(file, result.Body)
if err != nil {
panic(err)
}
Delete file
bucketname := "{BUCKET_NAME}"
objectKey := "{FILE_NAME}" // File name to delete

_, err = client.DeleteObject(context.TODO(), &s3.DeleteObjectInput{
Bucket: &bucketname,
Key: &objectKey,
})
if err != nil {
log.Fatal(err)
}