本文介绍多语言(例如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”字段替换为宽表引擎的用户名和密码。建议将密码写入环境变量或配置文件中,避免采用硬编码的形式。
文档内容是否对您有帮助?