Skip to content

Commit 17e7ee1

Browse files
committed
feat: from s3 with s3 protocol OK
1 parent e870f65 commit 17e7ee1

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/GetStream/stream-chat-go/v5 v5.8.1
1111
github.com/MakeNowJust/heredoc v1.0.0
1212
github.com/aws/aws-sdk-go-v2/config v1.32.7
13+
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.21.0
1314
github.com/aws/aws-sdk-go-v2/service/s3 v1.95.1
1415
github.com/cheynewallace/tabby v1.1.1
1516
github.com/gizak/termui/v3 v3.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ github.com/aws/aws-sdk-go-v2/credentials v1.19.7 h1:tHK47VqqtJxOymRrNtUXN5SP/zUT
5858
github.com/aws/aws-sdk-go-v2/credentials v1.19.7/go.mod h1:qOZk8sPDrxhf+4Wf4oT2urYJrYt3RejHSzgAquYeppw=
5959
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17 h1:I0GyV8wiYrP8XpA70g1HBcQO1JlQxCMTW9npl5UbDHY=
6060
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17/go.mod h1:tyw7BOl5bBe/oqvoIeECFJjMdzXoa/dfVz3QQ5lgHGA=
61+
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.21.0 h1:pQZGI0qQXeCHZHMeWzhwPu+4jkWrdrIb2dgpG4OKmco=
62+
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.21.0/go.mod h1:XGq5kImVqQT4HUNbbG+0Y8O74URsPNH7CGPg1s1HW5E=
6163
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17 h1:xOLELNKGp2vsiteLsvLPwxC+mYmO6OZ8PYgiuPJzF8U=
6264
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17/go.mod h1:5M5CI3D12dNOtH3/mk6minaRwI2/37ifCURZISxA/IQ=
6365
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17 h1:WWLqlh79iO48yLkj1v3ISRNiv+3KdQoZ6JWyfcsyQik=

pkg/cmd/raw-recording-tool/s3_downloader.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"github.com/aws/aws-sdk-go-v2/config"
17+
s3manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
1718
"github.com/aws/aws-sdk-go-v2/service/s3"
1819
)
1920

@@ -149,6 +150,40 @@ func parseS3URL(inputURL string) (bucket, key string, err error) {
149150
return parts[0], parts[1], nil
150151
}
151152

153+
// getS3ClientForBucket creates an S3 client configured for the bucket's region
154+
func (d *S3Downloader) getS3ClientForBucket(ctx context.Context, bucket string) (*s3.Client, error) {
155+
// First, load the default config
156+
cfg, err := config.LoadDefaultConfig(ctx)
157+
if err != nil {
158+
return nil, fmt.Errorf("failed to load AWS config: %w", err)
159+
}
160+
161+
// Create a client to detect the bucket region
162+
client := s3.NewFromConfig(cfg)
163+
164+
// Get the actual bucket region
165+
region, err := s3manager.GetBucketRegion(ctx, client, bucket)
166+
if err != nil {
167+
// If we can't detect the region, return the default client
168+
if d.verbose {
169+
fmt.Printf("Warning: could not detect bucket region, using default: %v\n", err)
170+
}
171+
return client, nil
172+
}
173+
174+
if d.verbose {
175+
fmt.Printf("Detected bucket region: %s\n", region)
176+
}
177+
178+
// Reload config with the correct region
179+
cfg, err = config.LoadDefaultConfig(ctx, config.WithRegion(region))
180+
if err != nil {
181+
return nil, fmt.Errorf("failed to load AWS config with region %s: %w", region, err)
182+
}
183+
184+
return s3.NewFromConfig(cfg), nil
185+
}
186+
152187
// isCacheValid checks if the cached file is still valid
153188
func (d *S3Downloader) isCacheValid(ctx context.Context, inputURL, cachedFilePath, metadataPath string) bool {
154189
// Check if cached file exists
@@ -193,12 +228,11 @@ func (d *S3Downloader) getS3ETag(ctx context.Context, inputURL string) (string,
193228
return "", err
194229
}
195230

196-
cfg, err := config.LoadDefaultConfig(ctx)
231+
client, err := d.getS3ClientForBucket(ctx, bucket)
197232
if err != nil {
198-
return "", fmt.Errorf("failed to load AWS config: %w", err)
233+
return "", err
199234
}
200235

201-
client := s3.NewFromConfig(cfg)
202236
result, err := client.HeadObject(ctx, &s3.HeadObjectInput{
203237
Bucket: &bucket,
204238
Key: &key,
@@ -240,12 +274,11 @@ func (d *S3Downloader) downloadFromS3(ctx context.Context, inputURL, destPath st
240274
return "", err
241275
}
242276

243-
cfg, err := config.LoadDefaultConfig(ctx)
277+
client, err := d.getS3ClientForBucket(ctx, bucket)
244278
if err != nil {
245-
return "", fmt.Errorf("failed to load AWS config: %w", err)
279+
return "", err
246280
}
247281

248-
client := s3.NewFromConfig(cfg)
249282
result, err := client.GetObject(ctx, &s3.GetObjectInput{
250283
Bucket: &bucket,
251284
Key: &key,

0 commit comments

Comments
 (0)