@@ -8,24 +8,25 @@ import (
88 "encoding/json"
99 "fmt"
1010 "io"
11- "log"
11+ "log/slog "
1212 "path"
1313 "path/filepath"
1414 "sync"
1515 "time"
1616
17- "github.com/aws/aws-sdk-go/aws"
18- "github.com/aws/aws-sdk-go/service/s3"
17+ "github.com/aws/aws-sdk-go-v2/aws"
18+ "github.com/aws/aws-sdk-go-v2/config"
19+ "github.com/aws/aws-sdk-go-v2/service/s3"
1920 "github.com/function61/edgerouter/pkg/erconfig"
2021 "github.com/function61/edgerouter/pkg/erdiscovery"
21- "github.com/function61/gokit/aws/s3facade"
2222 "github.com/function61/gokit/mime"
2323)
2424
2525type uploadJob struct {
2626 applicationID string
2727 deploymentSpec deploymentSpec
28- bucket * s3facade.BucketContext
28+ bucketName string
29+ s3Client * s3.Client
2930}
3031
3132// atomically deploys a new version of a site to a S3 bucket, then updates service
@@ -47,10 +48,7 @@ func Deploy(ctx context.Context, tarArchive io.Reader, applicationID string, dep
4748
4849 s3StaticWebsiteOpts := app .Backend .S3StaticWebsiteOpts
4950
50- bucket , err := s3facade .Bucket (
51- s3StaticWebsiteOpts .BucketName ,
52- s3facade .CredentialsFromEnv ,
53- s3StaticWebsiteOpts .RegionID )
51+ awsConfig , err := config .LoadDefaultConfig (ctx , config .WithRegion (s3StaticWebsiteOpts .RegionID ))
5452 if err != nil {
5553 return err
5654 }
@@ -61,7 +59,8 @@ func Deploy(ctx context.Context, tarArchive io.Reader, applicationID string, dep
6159 Version : deployVersion ,
6260 DeployedAt : time .Now (),
6361 },
64- bucket : bucket ,
62+ bucketName : s3StaticWebsiteOpts .BucketName ,
63+ s3Client : s3 .NewFromConfig (awsConfig ),
6564 }
6665
6766 if err := uploadAllFiles (ctx , tarArchive , upload ); err != nil {
@@ -95,7 +94,7 @@ func uploadAllFiles(ctx context.Context, tarArchive io.Reader, upload *uploadJob
9594 wg .Add (workerCount )
9695
9796 for i := 0 ; i < workerCount ; i ++ {
98- go uploadWorker (ctx , upload .bucket . S3 , workItems , workError , wg )
97+ go uploadWorker (ctx , upload .s3Client , workItems , workError , wg )
9998 }
10099
101100 // cancel and wait for workers exit in all exit paths
@@ -143,7 +142,7 @@ func uploadAllFiles(ctx context.Context, tarArchive io.Reader, upload *uploadJob
143142 }
144143
145144 workItems <- & s3.PutObjectInput {
146- Bucket : upload .bucket . Name ,
145+ Bucket : aws . String ( upload .bucketName ) ,
147146 Key : aws .String (bucketPrefix (upload .applicationID , upload .deploymentSpec .Version ) + ".deployment.json" ),
148147 ContentType : aws .String ("application/json" ),
149148 Body : bytes .NewReader (deploymentSpecJSON ),
@@ -177,24 +176,24 @@ func createUploadRequest(filePath string, content io.Reader, upload *uploadJob)
177176 fullPath := path .Clean (pathPrefix + filePath )
178177
179178 return & s3.PutObjectInput {
180- Bucket : upload .bucket . Name ,
179+ Bucket : aws . String ( upload .bucketName ) ,
181180 Key : aws .String (fullPath ),
182181 ContentType : aws .String (mime .TypeByExtension (ext , mime .OctetStream )),
183182 CacheControl : aws .String ("max-age=31536000" ), // 1 year (= "infinite caching"), because this versioned URL will never change
184183 Body : bytes .NewReader (wholeFileInMemory ),
185184 }, nil
186185}
187186
188- func uploadWorker (ctx context.Context , s3Client * s3.S3 , objects <- chan * s3.PutObjectInput , workError chan <- error , wg * sync.WaitGroup ) {
187+ func uploadWorker (ctx context.Context , s3Client * s3.Client , objects <- chan * s3.PutObjectInput , workError chan <- error , wg * sync.WaitGroup ) {
189188 defer wg .Done ()
190189
191190 uploadOneObject := func (object * s3.PutObjectInput ) error {
192191 ctx , cancel := context .WithTimeout (ctx , 120 * time .Second )
193192 defer cancel ()
194193
195- log . Printf ("uploading %s " , * object .Key )
194+ slog . Info ("uploading" , "key " , * object .Key )
196195
197- _ , err := s3Client .PutObjectWithContext (ctx , object )
196+ _ , err := s3Client .PutObject (ctx , object )
198197 return err
199198 }
200199
0 commit comments