get-encrypted-object.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // +build ignore
  2. /*
  3. * Minio Go Library for Amazon S3 Compatible Cloud Storage
  4. * Copyright 2015-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. "io"
  21. "log"
  22. "os"
  23. "github.com/minio/minio-go"
  24. "github.com/minio/minio-go/pkg/encrypt"
  25. )
  26. func main() {
  27. // Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname, my-objectname and
  28. // my-testfile are dummy values, please replace them with original values.
  29. // Requests are always secure (HTTPS) by default. Set secure=false to enable insecure (HTTP) access.
  30. // This boolean value is the last argument for New().
  31. // New returns an Amazon S3 compatible client object. API compatibility (v2 or v4) is automatically
  32. // determined based on the Endpoint value.
  33. s3Client, err := minio.New("s3.amazonaws.com", "YOUR-ACCESS-KEY-HERE", "YOUR-SECRET-KEY-HERE", true)
  34. if err != nil {
  35. log.Fatalln(err)
  36. }
  37. //// Build an asymmetric key from private and public files
  38. //
  39. // privateKey, err := ioutil.ReadFile("private.key")
  40. // if err != nil {
  41. // t.Fatal(err)
  42. // }
  43. //
  44. // publicKey, err := ioutil.ReadFile("public.key")
  45. // if err != nil {
  46. // t.Fatal(err)
  47. // }
  48. //
  49. // asymmetricKey, err := NewAsymmetricKey(privateKey, publicKey)
  50. // if err != nil {
  51. // t.Fatal(err)
  52. // }
  53. ////
  54. // Build a symmetric key
  55. symmetricKey := encrypt.NewSymmetricKey([]byte("my-secret-key-00"))
  56. // Build encryption materials which will encrypt uploaded data
  57. cbcMaterials, err := encrypt.NewCBCSecureMaterials(symmetricKey)
  58. if err != nil {
  59. log.Fatalln(err)
  60. }
  61. // Get a deciphered data from the server, deciphering is assured by cbcMaterials
  62. reader, err := s3Client.GetEncryptedObject("my-bucketname", "my-objectname", cbcMaterials)
  63. if err != nil {
  64. log.Fatalln(err)
  65. }
  66. defer reader.Close()
  67. // Local file which holds plain data
  68. localFile, err := os.Create("my-testfile")
  69. if err != nil {
  70. log.Fatalln(err)
  71. }
  72. defer localFile.Close()
  73. if _, err := io.Copy(localFile, reader); err != nil {
  74. log.Fatalln(err)
  75. }
  76. }