最近更新时间:2023-11-03 16:34:22
以下代码用于设置存储空间CORS跨域规则:
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>"
// 构造存储空间的CORS规则
corsConfiguration := &s3.CORSConfiguration{
Rules: []*s3.CORSRule{
{
AllowedMethod: []string{"GET", "PUT"}, // 用户允许源所能执行的HTTP方法,类型: GET, PUT, HEAD, POST, DELETE
AllowedOrigin: []string{"*"}, // 用户允许跨域资源共享访问的源地址
AllowedHeader: []string{"*"}, // 指明在预检OPTION中通过Access-Control-Request-Headers哪些头部是可以使用的
},
},
}
// 设置存储空间的CORS
resp, err := client.PutBucketCORS(&s3.PutBucketCORSInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
CORSConfiguration: corsConfiguration, // 跨域规则配置,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关设置存储空间CORS跨域规则详情,请参见PUT Bucket CORS。
以下代码用于查看存储空间CORS跨域规则:
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>"
// 查看存储空间的CORS规则
resp, err := client.GetBucketCORS(&s3.GetBucketCORSInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关查看存储空间CORS跨域规则详情,请参见GET Bucket CORS。
以下代码用于删除存储空间CORS跨域规则:
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>"
// 删除存储空间的CORS规则
resp, err := client.DeleteBucketCORS(&s3.DeleteBucketCORSInput{
Bucket: aws.String(bucket), // 存储空间名称,必填
})
if err != nil {
panic(err)
}
fmt.Println("结果:\n", awsutil.StringValue(resp))
}
有关删除存储空间CORS跨域规则详情,请参见DELETE Bucket CORS。
纯净模式