getobject-context.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "io"
  21. "log"
  22. "os"
  23. "time"
  24. "context"
  25. "github.com/minio/minio-go"
  26. )
  27. func main() {
  28. // Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname, my-objectname and
  29. // my-testfile are dummy values, please replace them with original values.
  30. // Requests are always secure (HTTPS) by default. Set secure=false to enable insecure (HTTP) access.
  31. // This boolean value is the last argument for New().
  32. // New returns an Amazon S3 compatible client object. API compatibility (v2 or v4) is automatically
  33. // determined based on the Endpoint value.
  34. s3Client, err := minio.New("s3.amazonaws.com", "YOUR-ACCESS-KEY-HERE", "YOUR-SECRET-KEY-HERE", true)
  35. if err != nil {
  36. log.Fatalln(err)
  37. }
  38. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
  39. defer cancel()
  40. opts := minio.GetObjectOptions{}
  41. opts.SetModified(time.Now().Round(10 * time.Minute)) // get object if was modified within the last 10 minutes
  42. reader, err := s3Client.GetObjectWithContext(ctx, "my-bucketname", "my-objectname", opts)
  43. if err != nil {
  44. log.Fatalln(err)
  45. }
  46. defer reader.Close()
  47. localFile, err := os.Create("my-testfile")
  48. if err != nil {
  49. log.Fatalln(err)
  50. }
  51. defer localFile.Close()
  52. stat, err := reader.Stat()
  53. if err != nil {
  54. log.Fatalln(err)
  55. }
  56. if _, err := io.CopyN(localFile, reader, stat.Size); err != nil {
  57. log.Fatalln(err)
  58. }
  59. }