参考源码
参考文本
GetEncryptedObject(bucketName, objectName string, encryptMaterials encrypt.Materials) (io.ReadCloser, error)
返回对象的解密流。读流时的常见错误。
参数
参数 | 类型 | 描述 |
---|---|---|
bucketName |
string | 存储桶名称 |
objectName |
string | 对象的名称 |
encryptMaterials |
encrypt.Materials | encrypt 包提供的对流加密的接口,(更多信息,请看https://godoc.org/github.com/minio/minio-go/v7) |
返回值
参数 | 类型 | 描述 |
---|---|---|
stream |
io.ReadCloser | 返回对象的reader,调用者需要在读取之后进行关闭。 |
err |
_error | 错误信息 |
参考代码
func main() {
InitClient()
CreateBucket(BucketName)
bucketname := BucketName // Specify a bucket name - the bucket must already exist
objectName := "minIO_set.txt" // Specify a object name - the object must already exist
password := "~!@#$%^&*()_++_)(*&^%$#@!~" // Specify your password.
// New SSE-C where the cryptographic key is derived from a password and the objectname + bucketname as salt
encryption := encrypt.DefaultPBKDF([]byte(password), []byte(bucketname+objectName))
// Get the encrypted object
reader, err := MinioClient.GetObject(context.Background(), bucketname, objectName, minio.GetObjectOptions{ServerSideEncryption: encryption})
if err != nil {
log.Fatalln(err)
}
defer reader.Close()
// Local file which holds plain data
localFile, err := os.Create("./"+objectName)
if err != nil {
log.Fatalln(err)
}
defer localFile.Close()
if _, err := io.Copy(localFile, reader); err != nil {
log.Fatalln(err)
}
log.Printf("success save %s\n",objectName)
}
效果图