@@ -53,9 +53,9 @@ const (
5353)
5454
5555// NewAWSClient returns a new instance of awsClient configured to work in the default region (us-east-2).
56- func NewClient (ctx context.Context ) (* Client , error ) {
56+ func NewClient (ctx context.Context , region string ) (* Client , error ) {
5757 cfg , err := awsconfig .LoadDefaultConfig (ctx ,
58- awsconfig .WithRegion (defaultRegion ))
58+ awsconfig .WithRegion (region ))
5959 if err != nil {
6060 return nil , fmt .Errorf ("loading AWS config: %w" , err )
6161 }
@@ -71,8 +71,8 @@ func (c Client) BucketName(ctx context.Context, bucketType BucketType) (string,
7171 // Construct the bucket name based on the ProwJob ID (if running in Prow) or AWS account ID (if running outside
7272 // Prow) and the current timestamp
7373 var identifier string
74- if jobID := os .Getenv ("PROW_JOB_ID " ); len (jobID ) >= 4 {
75- identifier = jobID [: 4 ]
74+ if jobID := os .Getenv ("BUILD_ID " ); len (jobID ) >= 4 {
75+ identifier = jobID [len ( jobID ) - 4 : ]
7676 } else {
7777 callerIdentity , err := c .stsClient .GetCallerIdentity (ctx , & sts.GetCallerIdentityInput {})
7878 if err != nil {
@@ -95,15 +95,18 @@ func (c Client) BucketName(ctx context.Context, bucketType BucketType) (string,
9595}
9696
9797// EnsureS3Bucket creates a new S3 bucket with the given name and public read permissions.
98- func (c Client ) EnsureS3Bucket (ctx context.Context , bucketName string , publicRead bool ) error {
98+ func (c Client ) EnsureS3Bucket (ctx context.Context , region , bucketName string , publicRead bool ) error {
9999 bucketName = strings .TrimPrefix (bucketName , "s3://" )
100- klog .Infof ("Creating bucket %s in region %s" , bucketName , defaultRegion )
100+ klog .Infof ("Creating bucket %s in region %s" , bucketName , region )
101+ bucketConfig := & types.CreateBucketConfiguration {}
102+ if region != "us-east-1" {
103+ bucketConfig .LocationConstraint = types .BucketLocationConstraint (region )
104+ }
101105 _ , err := c .s3Client .CreateBucket (ctx , & s3.CreateBucketInput {
102- Bucket : aws .String (bucketName ),
103- CreateBucketConfiguration : & types.CreateBucketConfiguration {
104- LocationConstraint : defaultRegion ,
105- },
106- })
106+ Bucket : aws .String (bucketName ),
107+ CreateBucketConfiguration : bucketConfig ,
108+ },
109+ )
107110 if err != nil {
108111 var exists * types.BucketAlreadyExists
109112 if errors .As (err , & exists ) {
@@ -130,15 +133,16 @@ func (c Client) EnsureS3Bucket(ctx context.Context, bucketName string, publicRea
130133 klog .Infof ("Bucket %s created successfully" , bucketName )
131134
132135 if publicRead {
133- // We assume it will take 5-10 seconds for the bucket to be created and wait for it.
134- time .Sleep (10 * time .Second )
135136 err = c .setPublicAccessBlock (ctx , bucketName )
136137 if err != nil {
137138 klog .Errorf ("Failed to disable public access block policies on bucket %s, err: %v" , bucketName , err )
138139
139140 return fmt .Errorf ("disabling public access block policies for bucket %s: %w" , bucketName , err )
140141 }
141142
143+ // Wait for public access block settings to propagate before setting the policy
144+ time .Sleep (10 * time .Second )
145+
142146 err = c .setPublicReadPolicy (ctx , bucketName )
143147 if err != nil {
144148 klog .Errorf ("Failed to set public read policy on bucket %s, err: %v" , bucketName , err )
0 commit comments