afero-s3 icon indicating copy to clipboard operation
afero-s3 copied to clipboard

Subdir in S3?

Open flozano opened this issue 2 years ago • 2 comments

Is there any way to create a FS that accesses only a subdir in S3? (basically a prefix/ to everything)

flozano avatar Apr 12 '23 14:04 flozano

Small edit for clarification: I have seen afero.NewBasePathFs but I am not sure if it is a good direction

flozano avatar Apr 12 '23 15:04 flozano

diff --git a/fs/s3/s3.go b/fs/s3/s3.go
index b4756f8..ae2e413 100644
--- a/fs/s3/s3.go
+++ b/fs/s3/s3.go
@@ -18,6 +18,7 @@ func LoadFs(access *confpar.Access) (afero.Fs, error) {
        bucket := access.Params["bucket"]
        keyID := access.Params["access_key_id"]
        secretAccessKey := access.Params["secret_access_key"]
+       basePath := access.Params["base_path"]

        conf := aws.Config{
                Region:           aws.String(region),
@@ -42,6 +43,9 @@ func LoadFs(access *confpar.Access) (afero.Fs, error) {
        s3Fs := s3.NewFs(bucket, sess)

        // s3Fs = stripprefix.NewStripPrefixFs(s3Fs, 1)
-
-       return s3Fs, nil
+       if basePath != "" {
+               return afero.NewBasePathFs(s3Fs, basePath), nil
+       } else {
+               return s3Fs, nil
+       }
 }

flozano avatar Apr 12 '23 15:04 flozano