forked from gsn/predictor
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
)
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
|
|
// Create AWS config with anonymous credentials
|
|
cfg, err := config.LoadDefaultConfig(ctx,
|
|
config.WithRegion("us-east-1"),
|
|
config.WithCredentialsProvider(aws.AnonymousCredentials{}),
|
|
)
|
|
if err != nil {
|
|
log.Fatalf("Failed to load config: %v", err)
|
|
}
|
|
|
|
client := s3.NewFromConfig(cfg)
|
|
|
|
// Try to download a single file
|
|
bucket := "noaa-gfs-bdp-pds"
|
|
key := "gfs.20251020/00/atmos/gfs.t00z.pgrb2.0p50.f000"
|
|
|
|
fmt.Printf("Downloading: s3://%s/%s\n", bucket, key)
|
|
|
|
input := &s3.GetObjectInput{
|
|
Bucket: aws.String(bucket),
|
|
Key: aws.String(key),
|
|
}
|
|
|
|
result, err := client.GetObject(ctx, input)
|
|
if err != nil {
|
|
log.Fatalf("Failed to get object: %v", err)
|
|
}
|
|
defer result.Body.Close()
|
|
|
|
// Create output file
|
|
outFile := "/tmp/test_grib.part"
|
|
f, err := os.Create(outFile)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create file: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
// Copy data
|
|
written, err := io.Copy(f, result.Body)
|
|
if err != nil {
|
|
log.Fatalf("Failed to copy data: %v (wrote %d bytes)", err, written)
|
|
}
|
|
|
|
fmt.Printf("Successfully downloaded %d bytes\n", written)
|
|
|
|
// Rename
|
|
if err := os.Rename(outFile, "/tmp/test_grib"); err != nil {
|
|
log.Fatalf("Failed to rename: %v", err)
|
|
}
|
|
|
|
fmt.Println("Download complete!")
|
|
}
|