全部产品
Search
文档中心

Object Storage Service:Enkripsi sisi server (Go SDK V2)

更新时间:Dec 06, 2025

OSS mendukung Enkripsi sisi server (SSE). Saat Anda mengunggah data, OSS akan mengenkripsi dan menyimpannya. Saat Anda mengunduh data tersebut, OSS secara otomatis mendekripsinya dan mengembalikan data mentah. Header respons HTTP menunjukkan bahwa data telah dienkripsi di sisi server.

Catatan

  • Sebelum mengonfigurasi Enkripsi sisi server, pastikan Anda memahami fitur ini. Untuk informasi selengkapnya, lihat Server-side encryption.

  • Kode contoh dalam topik ini menggunakan ID wilayah China (Hangzhou) cn-hangzhou dan titik akhir publik. Jika Anda mengakses OSS dari layanan Alibaba Cloud lainnya dalam wilayah yang sama, gunakan titik akhir internal. Untuk informasi selengkapnya tentang wilayah dan titik akhir OSS, lihat Regions and endpoints.

  • Topik ini menggunakan variabel lingkungan untuk membaca kredensial akses. Untuk informasi selengkapnya tentang cara mengonfigurasi kredensial akses, lihat Configure access credentials.

  • Untuk mengonfigurasi enkripsi bucket, Anda harus memiliki izin oss:PutBucketEncryption. Untuk mengambil konfigurasi enkripsi bucket, Anda harus memiliki izin oss:GetBucketEncryption. Untuk menghapus konfigurasi enkripsi bucket, Anda harus memiliki izin oss:DeleteBucketEncryption. Untuk informasi selengkapnya, lihat Grant custom access policies to RAM users.

Kode contoh

Konfigurasikan enkripsi bucket

Anda dapat menggunakan kode berikut untuk mengatur metode enkripsi default untuk sebuah bucket. Setelah konfigurasi ini diterapkan, semua objek yang diunggah ke bucket tanpa metode enkripsi yang ditentukan akan dienkripsi menggunakan metode default tersebut.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The storage region.
	bucketName string // The bucket name.
)

// The init function is used to initialize command-line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line parameters.
	flag.Parse()

	// Check whether the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to set the encryption rule for the bucket.
	request := &oss.PutBucketEncryptionRequest{
		Bucket: oss.Ptr(bucketName), // The bucket name.
		ServerSideEncryptionRule: &oss.ServerSideEncryptionRule{
			ApplyServerSideEncryptionByDefault: &oss.ApplyServerSideEncryptionByDefault{
				SSEAlgorithm:      oss.Ptr("KMS"), // Use the KMS encryption algorithm.
				KMSDataEncryption: oss.Ptr("SM4"), // Use the SM4 data encryption algorithm.
			},
		},
	}

	// Send the request to set the encryption rule for the bucket.
	result, err := client.PutBucketEncryption(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket encryption %v", err)
	}

	// Print the result of setting the encryption rule for the bucket.
	log.Printf("put bucket encryption result:%#v\n", result)
}

Ambil konfigurasi enkripsi bucket

Anda dapat menggunakan kode berikut untuk mengambil konfigurasi enkripsi bucket.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The storage region.
	bucketName string // The bucket name.
)

// The init function is used to initialize command-line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line parameters.
	flag.Parse()

	// Check whether the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to get the bucket encryption configuration.
	request := &oss.GetBucketEncryptionRequest{
		Bucket: oss.Ptr(bucketName), // The bucket name.
	}

	// Get the bucket encryption configuration and process the result.
	result, err := client.GetBucketEncryption(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket encryption %v", err)
	}

	// Print the result of getting the bucket encryption configuration.
	log.Printf("get bucket encryption result:%#v\n", result)
}

Hapus konfigurasi enkripsi bucket

Anda dapat menggunakan kode berikut untuk menghapus konfigurasi enkripsi bucket.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // The storage region.
	bucketName string // The bucket name.
)

// The init function is used to initialize command-line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	// Parse command-line parameters.
	flag.Parse()

	// Check whether the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to delete the bucket encryption configuration.
	request := &oss.DeleteBucketEncryptionRequest{
		Bucket: oss.Ptr(bucketName), // The bucket name.
	}

	// Delete the bucket encryption configuration and process the result.
	result, err := client.DeleteBucketEncryption(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket encryption %v", err)
	}

	// Print the result of deleting the bucket encryption configuration.
	log.Printf("delete bucket encryption result:%#v\n", result)
}

Referensi