Accessing COS with the AWS S3 SDK

Last Updated At: 2025-11-12 14:28:14

Overview

COS provides APIs compatible with AWS S3. After your data is migrated from AWS S3 to COS, your client can easily be compatible with COS services through simple configuration modification. This section describes how to adapt to S3 SDKs of different development platforms. After adaptation, you can use S3 SDK APIs to access files in COS.

Preparations

  1. You have registered an account and obtained the SecretID and SecretKey from Cloud Access Management > Cloud API Key.
  2. You have a client that has been integrated with the S3 SDK and runs properly.

Java

The following example describes how to adapt to AWS Java SDK 2.20.0 to access COS services.

1. Modifying the AWS Configuration and Certificate Files

Note:

The following example describes how to modify AWS configuration and certificate files on Linux.

The default configuration file of the AWS SDK is typically located under the user directory. For details, see Configuration and Certificate Files.

  • Add the following configuration information to the configuration file (located in ~/.aws/config):

    [default]  
    s3 =  
    addressing_style = virtual 
    
  • Configure the key in the certificate file (located in ~/.aws/credentials).

    [default]  
    aws_access_key_id = [COS_SECRETID]  
    aws_secret_access_key = [COS_SECRETKEY] 
    

2. Setting the Endpoint in the Code

The bucket region ap-region-jcctest-ops is used as an example. The example code is as follows:

S3Client s3 = S3Client.builder()
              .endpointOverride(URI.create("http://cos.ap-region-jcctest-ops.cospub.yfm13.fsphere.cn"))
              .region(Region.of("ap-region-jcctest-ops"))
              .build();

Python

The following example describes how to adapt to AWS Python SDK 1.9.205 to access COS services.

1. Modifying the AWS Configuration and Certificate Files

Note:

The following example describes how to modify AWS configuration and certificate files on Linux.

The default configuration file of the AWS SDK is typically located under the user directory. For details, see Configuration and Certificate Files.

  • Configure the key in the certificate file (located in ~/.aws/credentials).

    [default]  
    aws_access_key_id = [COS_SECRETID]  
    aws_secret_access_key = [COS_SECRETKEY] 
    

2. Setting the Endpoint in the Code

The bucket region ap-region-jcctest-ops is used as an example.

s3 = boto3.client(
    's3',
    region_name='ap-region-jcctest-ops',
    endpoint_url='http://cos.ap-region-jcctest-ops.cospub.yfm13.fsphere.cn',
    verify=True,    # Whether to verify the certificate. True/False/'/path/to/ca-bundle.crt'
    use_ssl=False   # Whether to use SSL/TLS.
)

Go

The following example describes how to adapt to AWS Go SDK v1.55.6 to access COS services.

1. Modifying the AWS Certificate File

  • Configure the key in the certificate file (located in ~/.aws/credentials).

    [default]  
    aws_access_key_id = [COS_SECRETID]  
    aws_secret_access_key = [COS_SECRETKEY] 
    

2. Simple Examples

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

func main() {
    // 1. Create AWS session
    // Credentials are loaded from environment variables or ~/.aws/credentials
    region := "ap-region-jcctest-ops"
    endpoint := "http://cos.ap-region-jcctest-ops.cospub.yfm13.fsphere.cn"

    sess, err := session.NewSession(&aws.Config{
        Region:   aws.String(region),
        Endpoint: &endpoint,
    })
    if err != nil {
        log.Fatal("Failed to create session:", err)
    }

    // 2. Create S3 service client
    svc := s3.New(sess)

    // 3. List all buckets
    fmt.Println("Listing buckets:")
    result, err := svc.ListBuckets(nil)
    if err != nil {
        log.Fatal("Failed to list buckets:", err)
    }

    for _, b := range result.Buckets {
        fmt.Printf("* %s created at %s\n",
            aws.StringValue(b.Name), aws.TimeValue(b.CreationDate))
    }

    // 4. Upload file to S3
    bucketName := "test-1255000327"             // Replace with your bucket name
    fileName := "/tmp/test.txt"                    // Local file name
    objectKey := "uploaded_test_by_aws_go_sdk.txt" // Object key in S3

    file, err := os.Open(fileName)
    if err != nil {
        log.Fatal("Failed to open file:", err)
    }
    defer file.Close()

    _, err = svc.PutObject(&s3.PutObjectInput{
        Bucket: aws.String(bucketName),
        Key:    aws.String(objectKey),
        Body:   file,
    })
    if err != nil {
        log.Fatal("Failed to upload file:", err)
    }
    fmt.Printf("Successfully uploaded file %s to %s/%s\n", fileName, bucketName, objectKey)

    // 5. Download file from S3
    downloadedFile := "downloaded_test.txt"
    out, err := os.Create(downloadedFile)
    if err != nil {
        log.Fatal("Failed to create download file:", err)
    }
    defer out.Close()

    result2, err := svc.GetObject(&s3.GetObjectInput{
        Bucket: aws.String(bucketName),
        Key:    aws.String(objectKey),
    })
    if err != nil {
        log.Fatal("Failed to download file:", err)
    }
    defer result2.Body.Close()

    _, err = out.ReadFrom(result2.Body)
    if err != nil {
        log.Fatal("Failed to save downloaded file:", err)
    }
    fmt.Printf("Successfully downloaded file %s/%s to %s\n", bucketName, objectKey, downloadedFile)
}

C++

The following example describes how to adapt to AWS C++ SDK 1.7.68 to access COS services.

1. Modifying the AWS Configuration and Certificate Files

Note:

The following example describes how to modify AWS configuration and certificate files on Linux.

The default configuration file of the AWS SDK is typically located under the user directory. For details, see Configuration and Certificate Files.

  • Add the following configuration information to the configuration file (located in ~/.aws/config):

    [default]  
    s3 =  
    addressing_style = virtual 
    
  • Configure the key in the certificate file (located in ~/.aws/credentials).

    [default]  
    aws_access_key_id = [COS_SECRETID]  
    aws_secret_access_key = [COS_SECRETKEY] 
    

2. Setting the Endpoint in the Code

The bucket region ap-region-jcctest-ops is used as an example. The example code is as follows:

Aws::Client::ClientConfiguration awsCC;
awsCC.scheme = Aws::Http::Scheme::HTTP;
awsCC.region = "ap-region-jcctest-ops";
awsCC.endpointOverride = "cos.ap-region-jcctest-ops.cospub.yfm13.fsphere.cn"; 
Aws::S3::S3Client s3_client(awsCC);