customizations.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package ec2
  2. import (
  3. "time"
  4. "github.com/aws/aws-sdk-go/aws/awsutil"
  5. "github.com/aws/aws-sdk-go/aws/request"
  6. )
  7. func init() {
  8. initRequest = func(r *request.Request) {
  9. if r.Operation.Name == opCopySnapshot { // fill the PresignedURL parameter
  10. r.Handlers.Build.PushFront(fillPresignedURL)
  11. }
  12. }
  13. }
  14. func fillPresignedURL(r *request.Request) {
  15. if !r.ParamsFilled() {
  16. return
  17. }
  18. params := r.Params.(*CopySnapshotInput)
  19. // Stop if PresignedURL/DestinationRegion is set
  20. if params.PresignedUrl != nil || params.DestinationRegion != nil {
  21. return
  22. }
  23. // First generate a copy of parameters
  24. r.Params = awsutil.CopyOf(r.Params)
  25. params = r.Params.(*CopySnapshotInput)
  26. // Set destination region. Avoids infinite handler loop.
  27. // Also needed to sign sub-request.
  28. params.DestinationRegion = r.Service.Config.Region
  29. // Create a new client pointing at source region.
  30. // We will use this to presign the CopySnapshot request against
  31. // the source region
  32. config := r.Service.Config.Copy().
  33. WithEndpoint("").
  34. WithRegion(*params.SourceRegion)
  35. client := New(config)
  36. // Presign a CopySnapshot request with modified params
  37. req, _ := client.CopySnapshotRequest(params)
  38. url, err := req.Presign(300 * time.Second) // 5 minutes should be enough.
  39. if err != nil { // bubble error back up to original request
  40. r.Error = err
  41. }
  42. // We have our URL, set it on params
  43. params.PresignedUrl = &url
  44. }