sdk

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2021 License: Apache-2.0 Imports: 0 Imported by: 0

README

AWS S3 SDK For Go使用指南


[TOC]

1 概述

此SDK是基于aws-sdk-go改造,适用于golang开发环境。

2 环境准备

配置Go 开发环境

3 初始化

3.1 下载安装SDK

go get github.com/ks3sdklib/aws-sdk-go

3.2 获取秘钥

1、开通KS3服务,http://www.ksyun.com/user/register 注册账号
2、进入控制台, http://ks3.ksyun.com/console.html#/setting 获取AccessKeyID 、AccessKeySecret

3.3 初始化

1、引用相关包

import(
	"github.com/ks3sdklib/aws-sdk-go/aws"
	"github.com/ks3sdklib/aws-sdk-go/aws/credentials"
	"github.com/ks3sdklib/aws-sdk-go/service/s3"
)

2、初始化客户端

credentials := credentials.NewStaticCredentials("<AccessKeyID>","<AccessKeySecret>","")
client := s3.New(&aws.Config{
	Region: "HANGZHOU",
	Credentials: credentials,
	Endpoint:"kssws.ks-cdn.com",//s3地址
	DisableSSL:true,//是否禁用https
	LogLevel:1,//是否开启日志,0为关闭日志,1为开启日志
	S3ForcePathStyle:false,//是否强制使用path style方式访问
	LogHTTPBody:true,//是否把HTTP请求body打入日志
	Logger:os.Stdout,//打日志的位置
	})

4 使用示例

输入参数params和返回结果resp详细结构请参考github.com/ks3sdklib/aws-sdk-go/service/s3/api.go

4.1 上传文件
params := &s3.PutObjectInput{
	Bucket:             aws.String("BucketName"), // bucket名称
	Key:                aws.String("ObjectKey"),  // object key
	ACL:                aws.String("ObjectCannedACL"),//权限,支持private(私有),public-read(公开读)
	Body:               bytes.NewReader([]byte("PAYLOAD")),//要上传的内容
	ContentType:        aws.String("application/ocet-stream"),//设置content-type
	Metadata: map[string]*string{
		//"Key": aws.String("MetadataValue"), // 设置用户元数据
		// More values...
	},
}
resp, err := client.PutObject(params)
if err!= nil{
	panic(err)
}
fmt.Println(resp)
//获取新的文件名
fmt.Println(*resp.NewFileName)
4.2 下载文件
params := &s3.GetObjectInput{
	Bucket:             aws.String("BucketName"), // bucket名称
	Key:                aws.String("ObjectKey"),  // object key
}
resp, err := client.GetObject(params)
if err != nil{
	panic(err)
}
//读取返回结果中body的前20个字节
b := make([]byte, 20)
n, err := resp.Body.Read(b)
fmt.Printf("%-20s %-2v %v\n", b[:n], n, err)
4.3 生成文件下载外链
params := &s3.GetObjectInput{
	Bucket:             aws.String("BucketName"), // bucket名称
	Key:                aws.String("ObjectKey"),  // object key
	ResponseCacheControl:       aws.String("ResponseCacheControl"),//控制返回的Cache-Control header
	ResponseContentDisposition: aws.String("ResponseContentDisposition"),//控制返回的Content-Disposition header
	ResponseContentEncoding:    aws.String("ResponseContentEncoding"),//控制返回的Content-Encoding header
	ResponseContentLanguage:    aws.String("ResponseContentLanguage"),//控制返回的Content-Language header
	ResponseContentType:        aws.String("ResponseContentType"),//控制返回的Content-Type header
}
resp, err := client.GetObjectPresignedUrl(params,1444370289000000000)//第二个参数为外链过期时间,第二个参数为time.Duration类型
if err!=nil {
	panic(err)
}
fmt.Println(resp)//resp即生成的外链地址,类型为url.URL
4.4 生成文件上传外链
params := &s3.PutObjectInput{
	Bucket:             aws.String(bucket), // bucket名称
	Key:                aws.String(key),  // object key
	ACL:				aws.String("public-read"),//设置ACL
	ContentType:        aws.String("application/ocet-stream"),//设置文件的content-type
	ContentMaxLength:	aws.Long(20),//设置允许的最大长度,对应的header:x-amz-content-maxlength
}
resp, err := client.PutObjectPresignedUrl(params,1444370289000000000)//第二个参数为外链过期时间,第二个参数为time.Duration类型
if err!=nil {
	panic(err)
}

httpReq, _ := http.NewRequest("PUT", "", strings.NewReader("123"))
httpReq.URL = resp
httpReq.Header["x-amz-acl"] = []string{"public-read"}
httpReq.Header["x-amz-content-maxlength"] = []string{"20"}
httpReq.Header.Add("Content-Type","application/ocet-stream")
fmt.Println(httpReq)
httpRep,err := http.DefaultClient.Do(httpReq)
fmt.Println(httpRep)
if err != nil{
	panic(err)
}
4.5 生成设置文件ACL的外链
params := &s3.PutObjectACLInput{
	Bucket:             aws.String(bucket), // bucket名称
	Key:                aws.String(key),  // object key
	ACL:				aws.String("private"),//设置ACL
	ContentType:		aws.String("text/plain"),
}
resp, err := client.PutObjectACLPresignedUrl(params,1444370289000000000)//第二个参数为外链过期时间,第二个参数为time.Duration类型
if err!=nil {
	panic(err)
}
fmt.Println(resp)//resp即生成的外链地址,类型为url.URL

httpReq, _ := http.NewRequest("PUT", "", nil)
httpReq.URL = resp
httpReq.Header["x-amz-acl"] = []string{"private"}
httpReq.Header.Add("Content-Type","text/plain")
fmt.Println(httpReq)
httpRep,err := http.DefaultClient.Do(httpReq)
if err != nil{
	panic(err)
}
fmt.Println(httpRep)
4.6 获取OBJECTACL
func  GetObcjetAcl() {
	params := &s3.GetObjectACLInput{
		Bucket: aws.String("BucketName"),
		Key: aws.String("ObjectKey"),
	}
	resp, err := client.GetObjectACL(params)
	if err == nil {
	    //解析acl
		fmt.Println(s3.GetAcl(*resp))
	}
}
4.7 初始化分块上传
params := &s3.CreateMultipartUploadInput{
	Bucket:             aws.String("BucketName"), // bucket名称
	Key:                aws.String("ObjectKey"),  // object key
	ACL:                aws.String("ObjectCannedACL"),//权限,支持private(私有),public-read(公开读)
	ContentType:        aws.String("application/ocet-stream"),//设置content-type
	Metadata: map[string]*string{
		//"Key": aws.String("MetadataValue"), // 设置用户元数据
		// More values...
	},
}
resp, err := client.CreateMultipartUpload(params)
if err != nil{
	panic(err)
}
//获取这次初始化的uploadid
fmt.Println(*resp.UploadID)
4.8 分块上传-上传块
params := &s3.UploadPartInput{
	Bucket:aws.String(bucket),//bucket名称
	Key:aws.String(key),//文件名
	PartNumber:aws.Long(1),//当前块的序号
	UploadID:aws.String(uploadId),//由初始化获取到得uploadid
	Body:strings.NewReader(content),//当前块的内容
	ContentLength:aws.Long(int64(len(content))),//内容长度
}
resp,err := client.UploadPart(params)
if err != nil{
	panic(err)
}
fmt.Println(resp)
4.9 完成分块上传并合并块
params := &s3.CompleteMultipartUploadInput{
	Bucket:aws.String(bucket),//bucket名称
	Key:aws.String(key),//文件名
	UploadID:aws.String(uploadId),//由初始化获取到得uploadid
	MultipartUpload:&s3.CompletedMultipartUpload{
		Parts:<已经完成的块列表>,//类型为*s3.CompletedPart数组
	},
}
resp,err := client.CompleteMultipartUpload(params)
if err != nil{
	panic(err)
}
fmt.Println(resp)
4.10 取消分块上传
params := &s3.AbortMultipartUploadInput{
	Bucket:aws.String(bucket),//bucket名称
	Key:aws.String(key),//文件名
	UploadID:aws.String(uploadId),//由初始化获取到得uploadid
}
resp,err := client.AbortMultipartUpload(params)
if err != nil{
	panic(err)
}
fmt.Println(resp)
4.11 罗列分块上传已经上传的块
params := &s3.ListPartsInput{
	Bucket:aws.String(bucket),//bucket名称
	Key:aws.String(key),//文件名
	UploadID:aws.String(uploadId),//由初始化获取到得uploadid
}
resp,err := client.ListParts(params)
if err != nil{
	panic(err)
}
fmt.Println(resp)
4.12 计算token(移动端相关)
package main
import(
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"fmt"
)
func main(){
	AccessKeyId := "AccessKeyId"
	AccessKeySecret:= "AccessKeySecret"
	stringToSign := "stringToSign"

	signature := string(base64Encode(makeHmac([]byte(AccessKeySecret), []byte(stringToSign))))
token := "KSS "+AccessKeyId+":"+signature
fmt.Println(token)
}
func makeHmac(key []byte, data []byte) []byte {
	hash := hmac.New(sha1.New, key)
	hash.Write(data)
	return hash.Sum(nil)
}	
func base64Encode(src []byte) []byte {  
    return []byte(base64.StdEncoding.EncodeToString(src))  
} 

Documentation

Overview

Package sdk is the official AWS SDK for the Go programming language.

See our Developer Guide for information for on getting started and using the SDK.

https://github.com/ks3sdklib/aws-sdk-go/wiki

Directories

Path Synopsis
aws
Package aws provides core functionality for making requests to AWS services.
Package aws provides core functionality for making requests to AWS services.
awserr
Package awserr represents API error interface accessors for the SDK.
Package awserr represents API error interface accessors for the SDK.
credentials
Package credentials provides credential retrieval and management The Credentials is the primary method of getting access to and managing credentials Values.
Package credentials provides credential retrieval and management The Credentials is the primary method of getting access to and managing credentials Values.
internal
apierr
Package apierr represents API error types.
Package apierr represents API error types.
endpoints
Package endpoints validates regional endpoints for services.
Package endpoints validates regional endpoints for services.
features/autoscaling
Package autoscaling provides gucumber integration tests suppport.
Package autoscaling provides gucumber integration tests suppport.
features/cloudformation
Package cloudformation provides gucumber integration tests suppport.
Package cloudformation provides gucumber integration tests suppport.
features/cloudfront
Package cloudfront provides gucumber integration tests suppport.
Package cloudfront provides gucumber integration tests suppport.
features/cloudhsm
Package cloudhsm provides gucumber integration tests suppport.
Package cloudhsm provides gucumber integration tests suppport.
features/cloudsearch
Package cloudsearch provides gucumber integration tests suppport.
Package cloudsearch provides gucumber integration tests suppport.
features/cloudtrail
Package cloudtrail provides gucumber integration tests suppport.
Package cloudtrail provides gucumber integration tests suppport.
features/cloudwatch
Package cloudwatch provides gucumber integration tests suppport.
Package cloudwatch provides gucumber integration tests suppport.
features/cloudwatchlogs
Package cloudwatchlogs provides gucumber integration tests suppport.
Package cloudwatchlogs provides gucumber integration tests suppport.
features/codedeploy
Package codedeploy provides gucumber integration tests suppport.
Package codedeploy provides gucumber integration tests suppport.
features/cognitoidentity
Package cognitoidentity provides gucumber integration tests suppport.
Package cognitoidentity provides gucumber integration tests suppport.
features/cognitosync
Package cognitosync provides gucumber integration tests suppport.
Package cognitosync provides gucumber integration tests suppport.
features/configservice
Package configservice provides gucumber integration tests suppport.
Package configservice provides gucumber integration tests suppport.
features/datapipeline
Package datapipeline provides gucumber integration tests suppport.
Package datapipeline provides gucumber integration tests suppport.
features/directconnect
Package directconnect provides gucumber integration tests suppport.
Package directconnect provides gucumber integration tests suppport.
features/directoryservice
Package directoryservice provides gucumber integration tests suppport.
Package directoryservice provides gucumber integration tests suppport.
features/dynamodb
Package dynamodb provides gucumber integration tests suppport.
Package dynamodb provides gucumber integration tests suppport.
features/ec2
Package ec2 provides gucumber integration tests suppport.
Package ec2 provides gucumber integration tests suppport.
features/efs
Package efs provides gucumber integration tests suppport.
Package efs provides gucumber integration tests suppport.
features/elasticache
Package elasticache provides gucumber integration tests suppport.
Package elasticache provides gucumber integration tests suppport.
features/elasticbeanstalk
Package elasticbeanstalk provides gucumber integration tests suppport.
Package elasticbeanstalk provides gucumber integration tests suppport.
features/elastictranscoder
Package elastictranscoder provides gucumber integration tests suppport.
Package elastictranscoder provides gucumber integration tests suppport.
features/elb
Package elb provides gucumber integration tests suppport.
Package elb provides gucumber integration tests suppport.
features/emr
Package emr provides gucumber integration tests suppport.
Package emr provides gucumber integration tests suppport.
features/glacier
Package glacier provides gucumber integration tests suppport.
Package glacier provides gucumber integration tests suppport.
features/iam
Package iam provides gucumber integration tests suppport.
Package iam provides gucumber integration tests suppport.
features/kinesis
Package kinesis provides gucumber integration tests suppport.
Package kinesis provides gucumber integration tests suppport.
features/kms
Package kms provides gucumber integration tests suppport.
Package kms provides gucumber integration tests suppport.
features/lambda
Package lambda provides gucumber integration tests suppport.
Package lambda provides gucumber integration tests suppport.
features/machinelearning
Package machinelearning provides gucumber integration tests suppport.
Package machinelearning provides gucumber integration tests suppport.
features/opsworks
Package opsworks provides gucumber integration tests suppport.
Package opsworks provides gucumber integration tests suppport.
features/rds
Package rds provides gucumber integration tests suppport.
Package rds provides gucumber integration tests suppport.
features/redshift
Package redshift provides gucumber integration tests suppport.
Package redshift provides gucumber integration tests suppport.
features/route53
Package route53 provides gucumber integration tests suppport.
Package route53 provides gucumber integration tests suppport.
features/route53domains
Package route53domains provides gucumber integration tests suppport.
Package route53domains provides gucumber integration tests suppport.
features/ses
Package ses provides gucumber integration tests suppport.
Package ses provides gucumber integration tests suppport.
features/shared
Package shared contains shared step definitions that are used across integration tests
Package shared contains shared step definitions that are used across integration tests
features/sns
Package sns provides gucumber integration tests suppport.
Package sns provides gucumber integration tests suppport.
features/sqs
Package sqs provides gucumber integration tests suppport.
Package sqs provides gucumber integration tests suppport.
features/ssm
Package ssm provides gucumber integration tests suppport.
Package ssm provides gucumber integration tests suppport.
features/storagegateway
Package storagegateway provides gucumber integration tests suppport.
Package storagegateway provides gucumber integration tests suppport.
features/sts
Package sts provides gucumber integration tests suppport.
Package sts provides gucumber integration tests suppport.
features/swf
Package swf provides gucumber integration tests suppport.
Package swf provides gucumber integration tests suppport.
features/workspaces
Package workspaces provides gucumber integration tests suppport.
Package workspaces provides gucumber integration tests suppport.
fixtures/helpers
Package helpers provides parameter filtering utilities.
Package helpers provides parameter filtering utilities.
model/api
Package api represents API abstractions for rendering service generated files.
Package api represents API abstractions for rendering service generated files.
model/cli/gen-api
Command aws-gen-gocli parses a JSON description of an AWS API and generates a Go file containing a client for the API.
Command aws-gen-gocli parses a JSON description of an AWS API and generates a Go file containing a client for the API.
model/cli/gen-endpoints
Command aws-gen-goendpoints parses a JSON description of the AWS endpoint discovery logic and generates a Go file which returns an endpoint.
Command aws-gen-goendpoints parses a JSON description of the AWS endpoint discovery logic and generates a Go file which returns an endpoint.
protocol/ec2query
Package ec2query provides serialisation of AWS EC2 requests and responses.
Package ec2query provides serialisation of AWS EC2 requests and responses.
protocol/json/jsonutil
Package jsonutil provides JSON serialisation of AWS requests and responses.
Package jsonutil provides JSON serialisation of AWS requests and responses.
protocol/jsonrpc
Package jsonrpc provides JSON RPC utilities for serialisation of AWS requests and responses.
Package jsonrpc provides JSON RPC utilities for serialisation of AWS requests and responses.
protocol/query
Package query provides serialisation of AWS query requests, and responses.
Package query provides serialisation of AWS query requests, and responses.
protocol/rest
Package rest provides RESTful serialisation of AWS requests and responses.
Package rest provides RESTful serialisation of AWS requests and responses.
protocol/restjson
Package restjson provides RESTful JSON serialisation of AWS requests and responses.
Package restjson provides RESTful JSON serialisation of AWS requests and responses.
protocol/restxml
Package restxml provides RESTful XML serialisation of AWS requests and responses.
Package restxml provides RESTful XML serialisation of AWS requests and responses.
protocol/xml/xmlutil
Package xmlutil provides XML serialisation of AWS requests and responses.
Package xmlutil provides XML serialisation of AWS requests and responses.
signer/v4
Package v4 implements signing for AWS V4 signer
Package v4 implements signing for AWS V4 signer
test/integration
Package integration performs initialization and validation for integration tests.
Package integration performs initialization and validation for integration tests.
test/unit
Package unit performs initialization and validation for unit tests
Package unit performs initialization and validation for unit tests
util/utilassert
Package utilassert provides testing assertion generation functions.
Package utilassert provides testing assertion generation functions.
util/utilsort
Package utilsort provides sorting utility methods.
Package utilsort provides sorting utility methods.
Package service contains automatically generated AWS clients.
Package service contains automatically generated AWS clients.
s3
Package s3 provides a client for Amazon Simple Storage Service.
Package s3 provides a client for Amazon Simple Storage Service.
s3/s3iface
Package s3iface provides an interface for the Amazon Simple Storage Service.
Package s3iface provides an interface for the Amazon Simple Storage Service.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL