本文介紹多語言(例如Go、Python)通過S3協議訪問Lindorm寬表引擎的具體操作和使用樣本。
前提條件
操作步驟
Go SDK訪問Lindorm寬表引擎樣本
安裝Go SDK。
go get github.com/aws/aws-sdk-go
使用Go SDK串連並訪問Lindorm寬表引擎。
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" // Lindorm寬表引擎的S3相容串連地址 ) 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) // 建立串連 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 } }
說明建立串連時,請將樣本中的“AK”和“SK”欄位替換為寬表引擎的使用者名稱和密碼。建議將密碼寫入環境變數或設定檔中,避免採用硬式編碼形式。
Python SDK訪問Lindorm寬表引擎樣本
安裝Python SDK。
pip install boto3
使用Python SDK串連並訪問Lindorm寬表引擎。
import boto3 from botocore.client import Config s3endpoint = 'http://ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com:9053' # Lindorm寬表引擎的S3相容串連地址 # 建立串連 s3client = boto3.client( 's3', aws_access_key_id="AK", aws_secret_access_key="SK", endpoint_url = s3endpoint, config=Config(s3={'addressing_style': 'path'}) ) # 建立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") # 下載object s3client.download_file('testbucketpython', 'demo.py', 'demo.py.download')
說明建立串連時,請將樣本中的“AK”和“SK”欄位替換為寬表引擎的使用者名稱和密碼。建議將密碼寫入環境變數或設定檔中,避免採用硬式編碼形式。