From 2850e83dc8d3a2a587e67c9b03f4f847b3631f42 Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Thu, 2 May 2024 17:08:54 +0800 Subject: [PATCH 1/4] add io.SeekEnd Signed-off-by: yxxhero <11087727+yxxhero@users.noreply.github.com> --- aws_s3_reader_seeker.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aws_s3_reader_seeker.go b/aws_s3_reader_seeker.go index 1de5cbe..b7d3292 100644 --- a/aws_s3_reader_seeker.go +++ b/aws_s3_reader_seeker.go @@ -71,6 +71,8 @@ func (s *S3ReadSeeker) Seek(offset int64, whence int) (int64, error) { } discardBytes = int(offset - s.offset) s.offset = offset + case io.SeekEnd: + return int64(s.getSize()), nil default: return 0, errors.New("unsupported whence") } From 477404697bad8fd125fb70644bf231adc6871954 Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Thu, 2 May 2024 17:10:46 +0800 Subject: [PATCH 2/4] fix lint --- aws_s3_reader_seeker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws_s3_reader_seeker.go b/aws_s3_reader_seeker.go index b7d3292..ceb5211 100644 --- a/aws_s3_reader_seeker.go +++ b/aws_s3_reader_seeker.go @@ -71,7 +71,7 @@ func (s *S3ReadSeeker) Seek(offset int64, whence int) (int64, error) { } discardBytes = int(offset - s.offset) s.offset = offset - case io.SeekEnd: + case io.SeekEnd: return int64(s.getSize()), nil default: return 0, errors.New("unsupported whence") From 8693189c63cd9102cf21f8afc8d9ee822f82f156 Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Fri, 3 May 2024 07:32:04 +0800 Subject: [PATCH 3/4] fix more issue --- aws_s3_reader_seeker.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/aws_s3_reader_seeker.go b/aws_s3_reader_seeker.go index ceb5211..7ab2e2a 100644 --- a/aws_s3_reader_seeker.go +++ b/aws_s3_reader_seeker.go @@ -72,7 +72,13 @@ func (s *S3ReadSeeker) Seek(offset int64, whence int) (int64, error) { discardBytes = int(offset - s.offset) s.offset = offset case io.SeekEnd: - return int64(s.getSize()), nil + if offset > 0 { + return 0, errors.New("cannot seek beyond end") + } + size := s.getSize() + noffset := int64(size) + offset + discardBytes = int(noffset - s.offset) + s.offset = noffset default: return 0, errors.New("unsupported whence") } From 2baba61fcfafcf00607f1b216e84dbf98bbe40eb Mon Sep 17 00:00:00 2001 From: yxxhero Date: Fri, 3 May 2024 21:22:19 +0800 Subject: [PATCH 4/4] add tests Signed-off-by: yxxhero --- aws_s3_reader_seeker_test.go | 71 ++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/aws_s3_reader_seeker_test.go b/aws_s3_reader_seeker_test.go index f2f4e32..1e8e9b8 100644 --- a/aws_s3_reader_seeker_test.go +++ b/aws_s3_reader_seeker_test.go @@ -238,3 +238,74 @@ func ExampleS3ReadSeeker() { panic(err) } } +func TestS3ReadSeeker_Seek(t *testing.T) { + mySession := session.Must(session.NewSession( + aws.NewConfig(). + WithRegion("ap-southeast-1"). + WithCredentials(credentials.AnonymousCredentials), + )) + s3client := s3.New(mySession) + + bucket := "nikolaydubina-blog-public" + key := "photos/2021-12-20-4.jpeg" + + r := awss3reader.NewS3ReadSeeker( + s3client, + bucket, + key, + awss3reader.FixedChunkSizePolicy{Size: 1 << 10 * 100}, // 100 KB + ) + defer r.Close() + + // Seek to offset 100 from current position + offset, err := r.Seek(100, io.SeekCurrent) + if err != nil { + t.Fatal(err) + } + if offset != 100 { + t.Errorf("expected offset 100, got %d", offset) + } + + // Seek to offset 200 from start + offset, err = r.Seek(200, io.SeekStart) + if err != nil { + t.Fatal(err) + } + if offset != 200 { + t.Errorf("expected offset 200, got %d", offset) + } + + // Seek to current position (0 offset) + offset, err = r.Seek(0, io.SeekCurrent) + if err != nil { + t.Fatal(err) + } + if offset != 200 { + t.Errorf("expected offset 200, got %d", offset) + } + + // Seek beyond end of file + _, err = r.Seek(1000, io.SeekEnd) + if err == nil { + t.Errorf("expected error, got nil when seeking beyond end of file") + } + + // seek 0 from end + offset, err = r.Seek(0, io.SeekEnd) + if err != nil { + t.Errorf("expected nil, got %v when seeking 0 from end", err) + } + if offset != 11356322 { + t.Errorf("expected offset 11356322, got %d", offset) + } + + // seek -100 from end + offset, err = r.Seek(-100, io.SeekEnd) + if err != nil { + t.Errorf("expected nil, got %v when seeking -100 from end", err) + } + + if offset != 11356222 { + t.Errorf("expected offset 11356222, got %d", offset) + } +}