Skip to content

Commit

Permalink
samples(storage): create HNS bucket sample (#4177)
Browse files Browse the repository at this point in the history
* samples(storage): create HNS bucket sample

Adds a sample to create a bucket with HierachicalNamespace
features enabled.

Updates googleapis/google-cloud-go#10294

* go mod tidy
  • Loading branch information
tritone authored Jun 11, 2024
1 parent d2e64af commit 0cbf1f0
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 80 deletions.
33 changes: 33 additions & 0 deletions storage/buckets/buckets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,36 @@ func TestAutoclass(t *testing.T) {
t.Errorf("got %q, want %q", got, want)
}
}

func TestCreateBucketHierarchicalNamespace(t *testing.T) {
tc := testutil.SystemTest(t)
ctx := context.Background()

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

bucketName := testutil.UniqueBucketName(testPrefix)
defer testutil.DeleteBucketIfExists(ctx, client, bucketName)

// Test creating new bucket with HNS enabled.
buf := new(bytes.Buffer)
if err := createBucketHierarchicalNamespace(buf, tc.ProjectID, bucketName); err != nil {
t.Fatalf("createBucketHierarchicalNamespace: %v", err)
}

if got, want := buf.String(), "Created bucket"; !strings.Contains(got, want) {
t.Errorf("got %q, want %q", got, want)
}

// Verify that HNS was set as expected.
attrs, err := client.Bucket(bucketName).Attrs(ctx)
if err != nil {
t.Fatalf("Bucket(%q).Attrs: %v", bucketName, err)
}
if got, want := (attrs.HierarchicalNamespace), (&storage.HierarchicalNamespace{Enabled: true}); got == nil || !got.Enabled {
t.Errorf("Attrs.HierarchicalNamespace: got %v, want %v", got, want)
}
}
60 changes: 60 additions & 0 deletions storage/buckets/create_bucket_hierarchical_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 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_create_bucket_hierarchical_namespace]
import (
"context"
"fmt"
"io"
"time"

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

// createBucketHierarchicalNamespace creates a new bucket with hierarchical
// namespace features enabled.
func createBucketHierarchicalNamespace(w io.Writer, projectID, bucketName string) error {
// projectID := "my-project-id"
// bucketName := "bucket-name"

ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %w", err)
}
defer client.Close()

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

attrs := &storage.BucketAttrs{
HierarchicalNamespace: &storage.HierarchicalNamespace{
Enabled: true,
},
// Hierarchical namespace buckets must use uniform bucket-level access.
UniformBucketLevelAccess: storage.UniformBucketLevelAccess{
Enabled: true,
},
}
bucket := client.Bucket(bucketName)
if err := bucket.Create(ctx, projectID, attrs); err != nil {
return fmt.Errorf("Bucket(%q).Create: %w", bucketName, err)
}
fmt.Fprintf(w, "Created bucket %v with hierarchical namespace enabled\n", bucketName)
return nil
}

// [END storage_create_bucket_hierarchical_namespace]
48 changes: 24 additions & 24 deletions storage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ module github.com/GoogleCloudPlatform/golang-samples/storage
go 1.21

require (
cloud.google.com/go/iam v1.1.7
cloud.google.com/go/pubsub v1.37.0
cloud.google.com/go/storage v1.40.0
cloud.google.com/go/iam v1.1.8
cloud.google.com/go/pubsub v1.38.0
cloud.google.com/go/storage v1.42.0
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20240514191801-f4bb2dc7b27f
github.com/aws/aws-sdk-go v1.53.15
github.com/googleapis/gax-go/v2 v2.12.3
google.golang.org/api v0.176.1
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda
github.com/googleapis/gax-go/v2 v2.12.4
google.golang.org/api v0.183.0
google.golang.org/genproto v0.0.0-20240610135401-a8a62080eff3
)

require (
cloud.google.com/go v0.112.2 // indirect
cloud.google.com/go/auth v0.3.0 // indirect
cloud.google.com/go v0.114.0 // indirect
cloud.google.com/go/auth v0.5.1 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/longrunning v0.5.6 // indirect
cloud.google.com/go/longrunning v0.5.7 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
Expand All @@ -29,20 +29,20 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.50.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0 // indirect
go.opentelemetry.io/otel v1.25.0 // indirect
go.opentelemetry.io/otel/metric v1.25.0 // indirect
go.opentelemetry.io/otel/trace v1.25.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
)
Loading

0 comments on commit 0cbf1f0

Please sign in to comment.