最近更新时间:2023-11-03 16:26:45
以下代码用于设置文件访问权限:
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
key := "<object_key>"
// 设置文件访问权限
resp, err := client.PutObjectACL(&s3.PutObjectACLInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 对象的key,必填
ACL: aws.String("public-read"), // 对象访问权限,有效值private(私有),public-read(公开读)
})
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
key := "<object_key>"
// 获取文件访问权限
resp, err := client.GetObjectACL(&s3.GetObjectACLInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
Key: aws.String(key), // 对象的key,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
访问权限的更多信息,请参见 ACL 。
纯净模式