Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): add change default storage class sample #1791

24 changes: 24 additions & 0 deletions storage/buckets/buckets_test.go
Expand Up @@ -56,6 +56,30 @@ func TestCreateBucketClassLocation(t *testing.T) {
}
}

func TestStorageClass(t *testing.T) {
tc := testutil.SystemTest(t)
bucketName := tc.ProjectID + "-storage-buckets-tests"

ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
t.Fatalf("storage.NewClient: %v", err)
}
defer client.Close()

if err := changeDefaultStorageClass(ioutil.Discard, bucketName); err != nil {
t.Errorf("changeDefaultStorageClass: %v", err)
}
attrs, err := client.Bucket(bucketName).Attrs(ctx)
if err != nil {
t.Fatalf("Bucket(%q).Attrs: %v", bucketName, err)
}
got := attrs.StorageClass
if want := "COLDLINE"; !strings.Contains(got, want) {
t.Errorf("got %q, want %q", got, want)
}
}

func TestListBuckets(t *testing.T) {
tc := testutil.SystemTest(t)
bucketName := tc.ProjectID + "-storage-buckets-tests"
Expand Down
54 changes: 54 additions & 0 deletions storage/buckets/change_default_storage_class.go
@@ -0,0 +1,54 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buckets

// [START storage_change_default_storage_class]
import (
"context"
"fmt"
"io"
"time"

"cloud.google.com/go/storage"
)

// changeDefaultStorageClass changes the storage class on a bucket.
func changeDefaultStorageClass(w io.Writer, bucketName string) error {
// bucketName := "bucket-name"
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()

ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()

bucket := client.Bucket(bucketName)
// See the StorageClass documentation for other valid storage classes:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not necessary; it's embedded in a page that contains this link and other languages don't have it.

// https://cloud.google.com/storage/docs/storage-classes
newStorageClass := "COLDLINE"
bucketAttrsToUpdate := storage.BucketAttrsToUpdate{
StorageClass: newStorageClass,
}
if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
return fmt.Errorf("Bucket(%q).Update: %v", bucketName, err)
}
fmt.Fprintf(w, "Default storage class for bucket %v has been set to %v\n", bucketName, newStorageClass)
return nil
}

// [END storage_change_default_storage_class]