putobject-getobject-sse.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // +build ignore
  2. /*
  3. * Minio Go Library for Amazon S3 Compatible Cloud Storage
  4. * Copyright 2017 Minio, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package main
  19. import (
  20. "bytes"
  21. "crypto/md5"
  22. "encoding/base64"
  23. "io/ioutil"
  24. "log"
  25. minio "github.com/minio/minio-go"
  26. )
  27. func main() {
  28. // Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-testfile, my-bucketname and
  29. // my-objectname are dummy values, please replace them with original values.
  30. // New returns an Amazon S3 compatible client object. API compatibility (v2 or v4) is automatically
  31. // determined based on the Endpoint value.
  32. minioClient, err := minio.New("s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
  33. if err != nil {
  34. log.Fatalln(err)
  35. }
  36. content := bytes.NewReader([]byte("Hello again"))
  37. key := []byte("32byteslongsecretkeymustprovided")
  38. h := md5.New()
  39. h.Write(key)
  40. encryptionKey := base64.StdEncoding.EncodeToString(key)
  41. encryptionKeyMD5 := base64.StdEncoding.EncodeToString(h.Sum(nil))
  42. // Amazon S3 does not store the encryption key you provide.
  43. // Instead S3 stores a randomly salted HMAC value of the
  44. // encryption key in order to validate future requests.
  45. // The salted HMAC value cannot be used to derive the value
  46. // of the encryption key or to decrypt the contents of the
  47. // encrypted object. That means, if you lose the encryption
  48. // key, you lose the object.
  49. var metadata = map[string]string{
  50. "x-amz-server-side-encryption-customer-algorithm": "AES256",
  51. "x-amz-server-side-encryption-customer-key": encryptionKey,
  52. "x-amz-server-side-encryption-customer-key-MD5": encryptionKeyMD5,
  53. }
  54. // minioClient.TraceOn(os.Stderr) // Enable to debug.
  55. _, err = minioClient.PutObject("mybucket", "my-encrypted-object.txt", content, 11, minio.PutObjectOptions{UserMetadata: metadata})
  56. if err != nil {
  57. log.Fatalln(err)
  58. }
  59. opts := minio.GetObjectOptions{}
  60. for k, v := range metadata {
  61. opts.Set(k, v)
  62. }
  63. coreClient := minio.Core{minioClient}
  64. reader, _, err := coreClient.GetObject("mybucket", "my-encrypted-object.txt", opts)
  65. if err != nil {
  66. log.Fatalln(err)
  67. }
  68. defer reader.Close()
  69. decBytes, err := ioutil.ReadAll(reader)
  70. if err != nil {
  71. log.Fatalln(err)
  72. }
  73. if !bytes.Equal(decBytes, []byte("Hello again")) {
  74. log.Fatalln("Expected \"Hello, world\", got %s", string(decBytes))
  75. }
  76. }