通过S3 非Java API连接并使用宽表引擎

本文介绍多语言(Go、Python)通过S3协议访问Lindorm的示例。

Go SDK 示例

  • 安装Go SDK

go get github.com/aws/aws-sdk-go
  • 使用Go SDK使用S3协议

package main

import (
 "log"
 "strings"

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

const (
    s3endpoint = "http://" + "ld-xxx:9053" // http加上集群的S3连接地址
)

func main() {
 conf := &aws.Config{
  Region:           aws.String("lindorm"),
  Endpoint:         aws.String(s3endpoint),
  S3ForcePathStyle: aws.Bool(true),
 }

 sess, err := session.NewSession(conf)

// 创建连接
 svc := s3.New(sess)

// 列举所有的bucket
 input := &s3.ListBucketsInput{}

 result, err := svc.ListBuckets(input)
 if err != nil {
  panic(err.Error())
 }
 println(result.String())

 bucket := "testbucketgo"
 key := "testObjectGo"

// 创建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
 }

// 写入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
 }
}

Python SDK 示例

  • 安装Python SDK

pip install boto3
  • 使用Python SDK使用S3协议

import boto3
 
s3endpoint = 'http://' + 'ld-xxx:9053' # http加上集群的S3连接地址
 
# 创建连接
s3client = boto3.client(
    's3',
    endpoint_url = s3endpoint
)

# 创建bucket 
# s3client.create_bucket(Bucket="testbucketpython")

# 列举bucket
buckets = s3client.list_buckets()

# 打印所有bucket
for bucket in buckets['Buckets']:
    print(bucket["Name"])

# 上传object
response = s3client.upload_file("demo.py", "testbucketpython", "demo.py")