最近更新时间:2023-01-04 10:04:41
本文介绍如何快速使用GO SDK完成常见操作,如创建存储空间(Bucket)、上传文件(Object)、下载文件等。
已安装GO SDK。详情请参见安装。
Bucket是存放Object的容器,所有的Object都必须存放在特定的Bucket中。
以下代码用于创建一个Bucket
package bucket_sample
import (
"fmt"
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
)
var bucket = string("yourbucket")
var key = string("yourkey")
var key_encode = string("yourkey")
var key_copy = string("yourkey")
var content = string("content")
// 金山云主账号 AccessKey 拥有所有API的访问权限,风险很高。
// 强烈建议您创建并使用子账号账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
// 通过指定 host(Endpoint),您可以在指定的地域创建新的存储空间。
var cre = credentials.NewStaticCredentials("ak", "sk", "") //online
var svc = s3.New(&aws.Config{
Region: "BEIJING",
Credentials: cre,
Endpoint: "ks3-cn-beijing.ksyuncs.com",
DisableSSL: true,
LogLevel: 1,
S3ForcePathStyle: false,
LogHTTPBody: true,
})
//创建bucket并关联项目
func TestCreateBucket(svc *s3.S3) {
resp, _ := svc.CreateBucket(&s3.CreateBucketInput{
ACL: aws.String("public-read"),
Bucket: aws.String(bucket),
ProjectId: aws.String("1232"), //项目ID
})
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于上传文件到KS3。
package ks3test
import (
"bufio"
"bytes"
"github.com/ks3sdklib/aws-sdk-go/aws/awserr"
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
"net/url"
"strconv"
"strings"
"testing"
"time"
// "io"
"fmt"
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
"net/http"
"os"
)
var bucket = string("yourbucket")
var key = string("yourkey")
var key_encode = string("yourkey")
var key_copy = string("yourkey")
var content = string("content")
var prefix = "test/" //目录名称
// 金山云主账号 AccessKey 拥有所有API的访问权限,风险很高。
// 强烈建议您创建并使用子账号账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
// 通过指定 host(Endpoint),您可以在指定的地域创建新的存储空间。
var cre = credentials.NewStaticCredentials("ak", "sk", "") //online
var svc = s3.New(&aws.Config{
//Region 可参考 [https://docs.ksyun.com/documents/6761](https://docs.ksyun.com/documents/6761)
Region: "BEIJING",
Credentials: cre,
//Endpoint 可参考 [https://docs.ksyun.com/documents/6761](https://docs.ksyun.com/documents/6761)
Endpoint: "ks3-cn-beijing.ksyuncs.com",
DisableSSL: true, //是否禁用https
LogLevel: 1, //是否开启日志,0为关闭日志,1为开启日志
LogHTTPBody: true, //是否把HTTP请求body打入日志
S3ForcePathStyle: true,
Logger: nil, //打日志的位置
})
//上传文件
func putObj(svc *s3.S3) {
//获取本地文件FD
fd, _ := os.Open("D:\\suiyi.jpg")
//指定目标Object对象标签,可同时设置多个标签,如:TagA=A&TagB=B。
//说明 Key和Value需要先进行URL编码,如果某项没有“=”,则看作Value为空字符串。详情请见对象标签([https://docs.ksyun.com/documents/39576](https://docs.ksyun.com/documents/39576))。
v := url.Values{}
v.Add("name", "yz")
v.Add("age", "11")
XAmzTagging := v.Encode()
input := s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
ACL: aws.String("public-read"),
Body: fd,
XAmzTagging: aws.String(XAmzTagging),
}
resp, _ := svc.PutObject(&input)
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于下载文件。
package ks3test
import (
"bufio"
"bytes"
"github.com/ks3sdklib/aws-sdk-go/aws/awserr"
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
"net/url"
"strconv"
"strings"
"testing"
"time"
// "io"
"fmt"
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
"net/http"
"os"
)
var bucket = string("yourbucket")
var key = string("yourkey")
var key_encode = string("yourkey")
var key_copy = string("yourkey")
var content = string("content")
var prefix = "test/" //目录名称
// 金山云主账号 AccessKey 拥有所有API的访问权限,风险很高。
// 强烈建议您创建并使用子账号账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
// 通过指定 host(Endpoint),您可以在指定的地域创建新的存储空间。
var cre = credentials.NewStaticCredentials("ak", "sk", "") //online
var svc = s3.New(&aws.Config{
//Region 可参考 [https://docs.ksyun.com/documents/6761](https://docs.ksyun.com/documents/6761)
Region: "BEIJING",
Credentials: cre,
//Endpoint 可参考 [https://docs.ksyun.com/documents/6761](https://docs.ksyun.com/documents/6761)
Endpoint: "ks3-cn-beijing.ksyuncs.com",
DisableSSL: true, //是否禁用https
LogLevel: 1, //是否开启日志,0为关闭日志,1为开启日志
LogHTTPBody: true, //是否把HTTP请求body打入日志
S3ForcePathStyle: false,
Logger: nil, //打日志的位置
})
//下载文件
func TestGetObject(svc *s3.S3) {
resp, _ := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
ResponseContentType: aws.String("application/pdf"), //响应类型
Range: aws.String("bytes=0-1"), //rang分块下载
})
//读取数据
br := bufio.NewReader(resp.Body)
//转成字符串
_, _ = br.ReadString('\n')
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于删除文件。
package ks3test
import (
"bufio"
"bytes"
"github.com/ks3sdklib/aws-sdk-go/aws/awserr"
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
"net/url"
"strconv"
"strings"
"testing"
"time"
// "io"
"fmt"
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
"net/http"
"os"
)
var bucket = string("yourbucket")
var key = string("yourkey")
var key_encode = string("yourkey")
var key_copy = string("yourkey")
var content = string("content")
var prefix = "test/" //目录名称
// 金山云主账号 AccessKey 拥有所有API的访问权限,风险很高。
// 强烈建议您创建并使用子账号账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
// 通过指定 host(Endpoint),您可以在指定的地域创建新的存储空间。
var cre = credentials.NewStaticCredentials("ak", "sk", "") //online
var svc = s3.New(&aws.Config{
//Region 可参考 [https://docs.ksyun.com/documents/6761](https://docs.ksyun.com/documents/6761)
Region: "BEIJING",
Credentials: cre,
//Endpoint 可参考 [https://docs.ksyun.com/documents/6761](https://docs.ksyun.com/documents/6761)
Endpoint: "ks3-cn-beijing.ksyuncs.com",
DisableSSL: true, //是否禁用https
LogLevel: 1, //是否开启日志,0为关闭日志,1为开启日志
LogHTTPBody: true, //是否把HTTP请求body打入日志
S3ForcePathStyle: false,
Logger: nil, //打日志的位置
})
//删除对象
func TestDelObject(svc *s3.S3) {
resp, _ := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于删除存储空间。
package bucket_sample
import (
"fmt"
"github.com/ks3sdklib/aws-sdk-go/aws"
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
"github.com/ks3sdklib/aws-sdk-go/service/s3"
)
var bucket = string("yourbucket")
var key = string("yourkey")
var key_encode = string("yourkey")
var key_copy = string("yourkey")
var content = string("content")
// 金山云主账号 AccessKey 拥有所有API的访问权限,风险很高。
// 强烈建议您创建并使用子账号账号进行 API 访问或日常运维,请登录 https://uc.console.ksyun.com/pro/iam/#/user/list 创建子账号。
// 通过指定 host(Endpoint),您可以在指定的地域创建新的存储空间。
var cre = credentials.NewStaticCredentials("ak", "sk", "") //online
var svc = s3.New(&aws.Config{
Region: "BEIJING",
Credentials: cre,
//Endpoint:"ks3-sgp.ksyun.com",
Endpoint: "ks3-cn-beijing.ksyuncs.com",
DisableSSL: true,
LogLevel: 1,
LogHTTPBody: true,
})
//删除bucket
func TestDeleteBucket(svc *s3.S3) {
resp, _ := svc.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucket),
})
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
纯净模式