最近更新时间:2024-05-10 14:25:48
以下代码用于上传文件流:
package main
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"
"os"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 填写对象的Key
key := "<object_key>"
// 填写上传文件路径
filePath := "/Users/test/demo.txt"
// 打开文件
fd, err := os.Open(filePath)
if err != nil {
panic(err)
}
// 上传文件流
resp, err := client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 对象的Key,必填、
Body: fd, // 要上传的文件,必填
ACL: aws.String("public-read"), // 对象访问权限,非必填
ContentType: aws.String("application/octet-stream"), // 请求体资源类型,非必填
Metadata: map[string]*string{
"Key": aws.String("MetadataValue"), // 设置对象元数据,非必填
},
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于创建文件夹:
package main
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"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 填写文件夹的名称,文件夹名称要以/结尾
key := "folder/"
// 创建文件夹
resp, err := client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 文件夹名称,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于上传字符串:
package main
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"
"strings"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 填写对象的key
key := "<object_key>"
// 上传字符串
resp, err := client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 对象的key,必填、
Body: strings.NewReader("test content"), // 要上传的文件,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于上传Byte数组:
package main
import (
"bytes"
"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"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 填写对象的key
key := "<object_key>"
// 上传Byte数组
resp, err := client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 对象的key,必填、
Body: bytes.NewReader([]byte("test content")), // 要上传的文件,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于上传加密文件,加密方式为:KS3托管密钥的服务器端加密(SSE-S3)。
package main
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"
"os"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 填写对象的Key
key := "<object_key>"
// 填写上传文件路径
filePath := "/Users/test/demo.txt"
// 打开文件
fd, err := os.Open(filePath)
if err != nil {
panic(err)
}
// 上传文件,使用KS3托管密钥的服务器端加密 (SSE-S3)
resp, err := client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 对象的Key,必填
Body: fd, // 要上传的文件内容,必填
ServerSideEncryption: aws.String("AES256"), // 使用KS3托管密钥的服务器端加密,合法值:AES256
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
以下代码用于上传加密文件,加密方式为:客户提供加密密钥的服务器端加密(SSE-C)。
package main
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"
"github.com/ks3sdklib/aws-sdk-go/service/s3/s3util"
"os"
)
func main() {
// 创建访问凭证,请将<AccessKeyID>与<SecretAccessKey>替换成真正的值
cre := credentials.NewStaticCredentials("<AccessKeyID>", "<SecretAccessKey>", "")
// 创建S3Client,更多配置项请查看Go-SDK初始化文档
client := s3.New(&aws.Config{
Credentials: cre, // 访问凭证
Region: "BEIJING", // 填写您的Region
Endpoint: "ks3-cn-beijing.ksyuncs.com", // 填写您的Endpoint
})
// 填写存储空间名称
bucket := "<bucket_name>"
// 填写对象的Key
key := "<object_key>"
// 填写上传文件路径
filePath := "/Users/test/demo.txt"
// 打开文件
fd, err := os.Open(filePath)
if err != nil {
panic(err)
}
// 填写您用于加密的密钥,密钥长度支持16/24/32位
SSECustomerKey := "<encryption_key>"
// 上传文件,使用客户提供的加密密钥的服务器端加密(SSE-C)
resp, err := client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 对象的Key,必填
Body: fd, // 要上传的文件内容,必填
SSECustomerAlgorithm: aws.String("AES256"), // 客户端提供的加密算法,合法值:AES256
SSECustomerKey: aws.String(s3util.GetBase64Str(SSECustomerKey)), // 客户端提供的加密密钥进行Base64编码后的值
SSECustomerKeyMD5: aws.String(s3util.GetBase64MD5Str(SSECustomerKey)), // 客户端提供的通过BASE64编码的通过128位MD5加密的密钥的MD5值
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
纯净模式