Skip to content

Commit

Permalink
feat(storage): add set_metadata sample (#1858)
Browse files Browse the repository at this point in the history
* feat(storage): add set_metadata sample

* subtest in a test

* Update storage/objects/set_metadata.go

Co-authored-by: Chris Cotter <cjcotter@google.com>

Co-authored-by: Frank Natividad <frankyn@users.noreply.github.com>
Co-authored-by: Chris Cotter <cjcotter@google.com>
  • Loading branch information
3 people committed Feb 5, 2021
1 parent 771f422 commit 1a10191
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
15 changes: 15 additions & 0 deletions storage/objects/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ func TestObjects(t *testing.T) {
t.Errorf("contents = %q; want %q", got, want)
}

t.Run("setMetadata", func(t *testing.T) {
bkt := client.Bucket(bucket)
obj := bkt.Object(object1)
err = setMetadata(ioutil.Discard, bucket, object1)
if err != nil {
t.Errorf("setMetadata: %v", err)
}
attrs, err := obj.Attrs(ctx)
if err != nil {
t.Errorf("object.Attrs: %v", err)
}
if got, want := attrs.Metadata["keyToAddOrUpdate"], "value"; got != want {
t.Errorf("object content = %q; want %q", got, want)
}
})
_, err = getMetadata(ioutil.Discard, bucket, object1)
if err != nil {
t.Errorf("getMetadata: %v", err)
Expand Down
54 changes: 54 additions & 0 deletions storage/objects/set_metadata.go
Original file line number Diff line number Diff line change
@@ -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 objects

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

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

// setMetadata sets an object's metadata.
func setMetadata(w io.Writer, bucket, object string) error {
// bucket := "bucket-name"
// object := "object-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()

o := client.Bucket(bucket).Object(object)
objectAttrsToUpdate := storage.ObjectAttrsToUpdate{
Metadata: map[string]string{
"keyToAddOrUpdate": "value",
},
}
if _, err := o.Update(ctx, objectAttrsToUpdate); err != nil {
return fmt.Errorf("ObjectHandle(%q).Update: %v", object, err)
}
fmt.Fprintf(w, "Updated custom metadata for object %v in bucket %v.\n", object, bucket)
return nil
}

// [END storage_set_metadata]

0 comments on commit 1a10191

Please sign in to comment.