Use the Amazon S3 API for a non-Java language to develop applications

更新时间:
复制 MD 格式

This topic describes how to use Amazon S3 SDK for a non-Java language such as Go or Python to connect to and use LindormTable based on the Amazon S3 protocol and provides corresponding examples.

Prerequisites

  • You have obtained the S3 compatibility address of the Lindorm LindormTable engine. For more information, see View endpoints. The S3 compatibility address is the VPC endpoint of the Lindorm instance. You can find the endpoint on the Endpoints page, on the LindormTable tab, in the S3 Compatibility Address section. The endpoint is in the format ld-bp<instance ID>:9053. The default username and password are both root.

  • Add the client IP address to the whitelist of the Lindorm instance. For more information, see Configure a whitelist.

Procedure

Use Amazon S3 SDK for Go to connect to and use LindormTable

  1. Install Amazon S3 SDK for Go.

    go get github.com/aws/aws-sdk-go
  2. Use Amazon S3 SDK for Go to connect to and use LindormTable.

    package main
    import (
     "log"
     "strings"
     "github.com/aws/aws-sdk-go/aws"
     "github.com/aws/aws-sdk-go/aws/credentials"
     "github.com/aws/aws-sdk-go/aws/session"
     "github.com/aws/aws-sdk-go/service/s3"
    )
    const (
        s3endpoint = "http://ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com:9053" // The S3 compatibility address for the LindormTable engine.
    )
    func main() {
     conf := &aws.Config{
      Credentials:      credentials.NewStaticCredentials("AK", "SK", ""),
      Region:           aws.String("lindorm"),
      Endpoint:         aws.String(s3endpoint),
      S3ForcePathStyle: aws.Bool(true),
     }
     sess, err := session.NewSession(conf)
    // Create a connection.
     svc := s3.New(sess)
    // List all buckets.
     input := &s3.ListBucketsInput{}
     result, err := svc.ListBuckets(input)
     if err != nil {
      panic(err.Error())
     }
     println(result.String())
     bucket := "testbucketgo"
     key := "testObjectGo"
    // Create a bucket.
     out, err := svc.CreateBucket(&s3.CreateBucketInput{Bucket: &bucket})
     log.Println(out)
     if err != nil {
      log.Println("Failed to create bucket", err)
      return
     }
     if err = svc.WaitUntilBucketExists(&s3.HeadBucketInput{Bucket: &bucket}); err != nil {
      log.Printf("Failed to wait for bucket to exist %s, %s\n", bucket, err)
      return
     }
    // Upload an object.
     _, err = svc.PutObject(&s3.PutObjectInput{
      Body:   strings.NewReader("Hello World!"),
      Bucket: &bucket,
      Key:    &key,
     })
     if err != nil {
      log.Printf("Failed to upload data to %s/%s, %s\n", bucket, key, err)
      return
     }
    }
    Note

    Replace the AK and SK fields in the sample code with the AccessKey ID and AccessKey secret that you actually use to connect LindormTable. We recommend that you configure the AccessKey ID and AccessKey secret as environment variables or specify them in the configuration file rather than hardcoding them.

Use Amazon S3 SDK for Python to connect to and use LindormTable

  1. Install Amazon S3 SDK for Python.

    pip install boto3
  2. Use Amazon S3 SDK for Python to connect to and use LindormTable.

    import boto3
    from botocore.client import Config
    s3endpoint = 'http://ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com:9053' # The S3 compatibility address for the LindormTable engine.
    # Create a connection.
    s3client = boto3.client(
        's3',
        aws_access_key_id="AK", 
        aws_secret_access_key="SK",
        endpoint_url = s3endpoint,
        config=Config(s3={'addressing_style': 'path'})
    )
    # Create a bucket. 
    s3client.create_bucket(Bucket="testbucketpython")
    # List buckets.
    buckets = s3client.list_buckets()
    # Print all buckets.
    for bucket in buckets['Buckets']:
        print(bucket["Name"])
    # Upload an object.
    response = s3client.upload_file("demo.py", "testbucketpython", "demo.py")
    # Download an object.
    s3client.download_file('testbucketpython', 'demo.py', 'demo.py.download')
    Note

    Replace the AK and SK fields in the sample code with the AccessKey ID and AccessKey secret that you actually use to connect LindormTable. We recommend that you configure the AccessKey ID and AccessKey secret as environment variables or specify them in the configuration file rather than hardcoding them.