diff --git a/storage/bucket.go b/storage/bucket.go index 19221168f90..5ada8cc81c8 100644 --- a/storage/bucket.go +++ b/storage/bucket.go @@ -661,6 +661,14 @@ type BucketAttrsToUpdate struct { // for more information. UniformBucketLevelAccess *UniformBucketLevelAccess + // StorageClass is the default storage class of the bucket. This defines + // how objects in the bucket are stored and determines the SLA + // and the cost of storage. Typical values are "STANDARD", "NEARLINE", + // "COLDLINE" and "ARCHIVE". Defaults to "STANDARD". + // See https://cloud.google.com/storage/docs/storage-classes for all + // valid values. + StorageClass string + // If set, updates the retention policy of the bucket. Using // RetentionPolicy.RetentionPeriod = 0 will delete the existing policy. // @@ -801,6 +809,7 @@ func (ua *BucketAttrsToUpdate) toRawBucket() *raw.Bucket { rb.DefaultObjectAcl = nil rb.ForceSendFields = append(rb.ForceSendFields, "DefaultObjectAcl") } + rb.StorageClass = ua.StorageClass if ua.setLabels != nil || ua.deleteLabels != nil { rb.Labels = map[string]string{} for k, v := range ua.setLabels { diff --git a/storage/bucket_test.go b/storage/bucket_test.go index ff92e85fade..123e319a84e 100644 --- a/storage/bucket_test.go +++ b/storage/bucket_test.go @@ -268,8 +268,9 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) { }, }, }, - Logging: &BucketLogging{LogBucket: "lb", LogObjectPrefix: "p"}, - Website: &BucketWebsite{MainPageSuffix: "mps", NotFoundPage: "404"}, + Logging: &BucketLogging{LogBucket: "lb", LogObjectPrefix: "p"}, + Website: &BucketWebsite{MainPageSuffix: "mps", NotFoundPage: "404"}, + StorageClass: "NEARLINE", } au.SetLabel("a", "foo") au.DeleteLabel("b") @@ -308,6 +309,7 @@ func TestBucketAttrsToUpdateToRawBucket(t *testing.T) { }, Logging: &raw.BucketLogging{LogBucket: "lb", LogObjectPrefix: "p"}, Website: &raw.BucketWebsite{MainPageSuffix: "mps", NotFoundPage: "404"}, + StorageClass: "NEARLINE", ForceSendFields: []string{"DefaultEventBasedHold", "Lifecycle"}, } if msg := testutil.Diff(got, want); msg != "" { diff --git a/storage/integration_test.go b/storage/integration_test.go index 1f331e74da2..c21ff8430a0 100644 --- a/storage/integration_test.go +++ b/storage/integration_test.go @@ -351,7 +351,7 @@ func TestIntegration_BucketUpdate(t *testing.T) { t.Fatalf("got %v, want %v", attrs.Labels, wantLabels) } - // Turn off versioning again; add and remove some more labels. + // Turn off versioning again; add and remove some more labels. ua = BucketAttrsToUpdate{VersioningEnabled: false} ua.SetLabel("l1", "v2") // update ua.SetLabel("new", "new") // create @@ -383,6 +383,18 @@ func TestIntegration_BucketUpdate(t *testing.T) { if !testutil.Equal(attrs.Lifecycle, wantLifecycle) { t.Fatalf("got %v, want %v", attrs.Lifecycle, wantLifecycle) } + // Check that StorageClass has "STANDARD" value for unset field by default + // before passing new value. + wantStorageClass := "STANDARD" + if !testutil.Equal(attrs.StorageClass, wantStorageClass) { + t.Fatalf("got %v, want %v", attrs.StorageClass, wantStorageClass) + } + wantStorageClass = "NEARLINE" + ua = BucketAttrsToUpdate{StorageClass: wantStorageClass} + attrs = h.mustUpdateBucket(b, ua) + if !testutil.Equal(attrs.StorageClass, wantStorageClass) { + t.Fatalf("got %v, want %v", attrs.StorageClass, wantStorageClass) + } } func TestIntegration_BucketPolicyOnly(t *testing.T) {