From fb4a6a7ab21b1ac97fdf73917f99ae8966ebce1c Mon Sep 17 00:00:00 2001 From: Mickael Kerjean Date: Thu, 8 Dec 2022 16:05:40 +1100 Subject: [PATCH] fix (s3): stop condition for recursive ls in s3 --- server/plugin/plg_backend_s3/index.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/server/plugin/plg_backend_s3/index.go b/server/plugin/plg_backend_s3/index.go index 6d4c1619..04460a7c 100644 --- a/server/plugin/plg_backend_s3/index.go +++ b/server/plugin/plg_backend_s3/index.go @@ -1,6 +1,7 @@ package plg_backend_s3 import ( + "context" "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" @@ -16,14 +17,16 @@ import ( "os" "path/filepath" "strings" + "time" ) var S3Cache AppCache type S3Backend struct { - client *s3.S3 - config *aws.Config - params map[string]string + client *s3.S3 + config *aws.Config + params map[string]string + context context.Context } func init() { @@ -63,9 +66,10 @@ func (s S3Backend) Init(params map[string]string, app *App) (IBackend, error) { } backend := &S3Backend{ - config: config, - params: params, - client: s3.New(session.New(config)), + config: config, + params: params, + client: s3.New(session.New(config)), + context: app.Context, } return backend, nil } @@ -161,7 +165,9 @@ func (s S3Backend) Ls(path string) (files []os.FileInfo, err error) { } client := s3.New(s.createSession(p.bucket)) - err = client.ListObjectsV2Pages( + startTime := time.Now() + err = client.ListObjectsV2PagesWithContext( + s.context, &s3.ListObjectsV2Input{ Bucket: aws.String(p.bucket), Prefix: aws.String(p.path), @@ -185,7 +191,7 @@ func (s S3Backend) Ls(path string) (files []os.FileInfo, err error) { FType: "directory", }) } - return true + return time.Since(startTime) < 5*time.Second }) return files, err }