bucket_context.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package s3
  2. import (
  3. "context"
  4. "github.com/aws/aws-sdk-go-v2/service/s3/internal/customizations"
  5. "github.com/aws/smithy-go/middleware"
  6. )
  7. // putBucketContextMiddleware stores the input bucket name within the request context (if
  8. // present) which is required for a variety of custom S3 behaviors
  9. type putBucketContextMiddleware struct{}
  10. func (*putBucketContextMiddleware) ID() string {
  11. return "putBucketContext"
  12. }
  13. func (m *putBucketContextMiddleware) HandleSerialize(
  14. ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
  15. ) (
  16. out middleware.SerializeOutput, metadata middleware.Metadata, err error,
  17. ) {
  18. if bucket, ok := m.bucketFromInput(in.Parameters); ok {
  19. ctx = customizations.SetBucket(ctx, bucket)
  20. }
  21. return next.HandleSerialize(ctx, in)
  22. }
  23. func (m *putBucketContextMiddleware) bucketFromInput(params interface{}) (string, bool) {
  24. v, ok := params.(bucketer)
  25. if !ok {
  26. return "", false
  27. }
  28. return v.bucket()
  29. }
  30. func addPutBucketContextMiddleware(stack *middleware.Stack) error {
  31. // This is essentially a post-Initialize task - only run it once the input
  32. // has received all modifications from that phase. Therefore we add it as
  33. // an early Serialize step.
  34. //
  35. // FUTURE: it would be nice to have explicit phases that only we as SDK
  36. // authors can hook into (such as between phases like this really should
  37. // be)
  38. return stack.Serialize.Add(&putBucketContextMiddleware{}, middleware.Before)
  39. }