Skip to content

Option to specify context when creating file and location objects#282

Open
NathanBaulch wants to merge 2 commits intoC2FO:mainfrom
NathanBaulch:context
Open

Option to specify context when creating file and location objects#282
NathanBaulch wants to merge 2 commits intoC2FO:mainfrom
NathanBaulch:context

Conversation

@NathanBaulch
Copy link
Contributor

@NathanBaulch NathanBaulch commented Sep 8, 2025

Something I find lacking in this library is the ability to trace outbound requests. It's possible to configure instrumented clients for most backends but common tracing tools like OpenTelemetry use context to pass state around.

Rather than breaking every API by adding context.Context arguments (as previously discussed), I propose the gs.WithContext pattern be applied more widely to NewLocation and NewFile. This would allow a web application to have a single shared FileSystem (or Location) and inject the current request context when working with specific files.

Here's a minimal example:

ctx := context.Background()
cfg, _ := config.LoadDefaultConfig(ctx)
cli := _s3.NewFromConfig(cfg, func(opts *_s3.Options) {
	// instrument S3 client
	opts.HTTPClient = &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
})
// pass default global context to fs
fs := s3.NewFileSystem(s3.WithClient(cli), s3.WithContext(ctx))
backend.Register(fs.Name(), fs)

http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
	// pass request context to file
	f, _ := fs.NewFile("", "version.txt", newfile.WithContext(r.Context()))
	c, _ := io.ReadAll(f)
	_, _ = fmt.Fprintf(w, "version: "+string(c))
})

_ = http.ListenAndServe(":8080", nil)

The other nice thing about this approach is it doesn't pollute the API for backends that don't support context, like mem, os and sftp.

This is a non-breaking additive change so in theory it could be included in v7, but I understand if it needs to wait for v8.

@c2fo-cibot c2fo-cibot bot added the size/XL Denotes a PR that changes 500-999 lines label Sep 8, 2025
Copy link
Member

@funkyshu funkyshu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still thinking this through but I think the s3 example suggestions make sense.

@c2fo-cibot c2fo-cibot bot added size/XXL Denotes a PR that changes 1000+ lines and removed size/XL Denotes a PR that changes 500-999 lines labels Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XXL Denotes a PR that changes 1000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants