customizations.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package ec2
  2. import (
  3. "time"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/awsutil"
  6. "github.com/aws/aws-sdk-go/aws/request"
  7. "github.com/aws/aws-sdk-go/private/endpoints"
  8. )
  9. func init() {
  10. initRequest = func(r *request.Request) {
  11. if r.Operation.Name == opCopySnapshot { // fill the PresignedURL parameter
  12. r.Handlers.Build.PushFront(fillPresignedURL)
  13. }
  14. }
  15. }
  16. func fillPresignedURL(r *request.Request) {
  17. if !r.ParamsFilled() {
  18. return
  19. }
  20. origParams := r.Params.(*CopySnapshotInput)
  21. // Stop if PresignedURL/DestinationRegion is set
  22. if origParams.PresignedUrl != nil || origParams.DestinationRegion != nil {
  23. return
  24. }
  25. origParams.DestinationRegion = r.Config.Region
  26. newParams := awsutil.CopyOf(r.Params).(*CopySnapshotInput)
  27. // Create a new request based on the existing request. We will use this to
  28. // presign the CopySnapshot request against the source region.
  29. cfg := r.Config.Copy(aws.NewConfig().
  30. WithEndpoint("").
  31. WithRegion(aws.StringValue(origParams.SourceRegion)))
  32. clientInfo := r.ClientInfo
  33. clientInfo.Endpoint, clientInfo.SigningRegion = endpoints.EndpointForRegion(
  34. clientInfo.ServiceName,
  35. aws.StringValue(cfg.Region),
  36. aws.BoolValue(cfg.DisableSSL),
  37. aws.BoolValue(cfg.UseDualStack),
  38. )
  39. // Presign a CopySnapshot request with modified params
  40. req := request.New(*cfg, clientInfo, r.Handlers, r.Retryer, r.Operation, newParams, r.Data)
  41. url, err := req.Presign(5 * time.Minute) // 5 minutes should be enough.
  42. if err != nil { // bubble error back up to original request
  43. r.Error = err
  44. return
  45. }
  46. // We have our URL, set it on params
  47. origParams.PresignedUrl = &url
  48. }