context.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package presignedurl
  2. import (
  3. "context"
  4. "github.com/aws/smithy-go/middleware"
  5. )
  6. // WithIsPresigning adds the isPresigning sentinel value to a context to signal
  7. // that the middleware stack is using the presign flow.
  8. //
  9. // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
  10. // to clear all stack values.
  11. func WithIsPresigning(ctx context.Context) context.Context {
  12. return middleware.WithStackValue(ctx, isPresigningKey{}, true)
  13. }
  14. // GetIsPresigning returns if the context contains the isPresigning sentinel
  15. // value for presigning flows.
  16. //
  17. // Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
  18. // to clear all stack values.
  19. func GetIsPresigning(ctx context.Context) bool {
  20. v, _ := middleware.GetStackValue(ctx, isPresigningKey{}).(bool)
  21. return v
  22. }
  23. type isPresigningKey struct{}
  24. // AddAsIsPresigingMiddleware adds a middleware to the head of the stack that
  25. // will update the stack's context to be flagged as being invoked for the
  26. // purpose of presigning.
  27. func AddAsIsPresigingMiddleware(stack *middleware.Stack) error {
  28. return stack.Initialize.Add(asIsPresigningMiddleware{}, middleware.Before)
  29. }
  30. type asIsPresigningMiddleware struct{}
  31. func (asIsPresigningMiddleware) ID() string { return "AsIsPresigningMiddleware" }
  32. func (asIsPresigningMiddleware) HandleInitialize(
  33. ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler,
  34. ) (
  35. out middleware.InitializeOutput, metadata middleware.Metadata, err error,
  36. ) {
  37. ctx = WithIsPresigning(ctx)
  38. return next.HandleInitialize(ctx, in)
  39. }