Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(storage): copy metadata when using Copier with grpc
We need to not set `destination` on the request unless the
user specifies attrs. Since the struct is not a pointer,
we have to check if it is empty to determine if it has not been set.
  • Loading branch information
BrennaEpp committed Sep 19, 2025
commit 48dc7447cac04061d26ff0cb0c8299297b1083ea
6 changes: 6 additions & 0 deletions storage/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ func (c *Copier) Run(ctx context.Context) (attrs *ObjectAttrs, err error) {
token: c.RewriteToken,
maxBytesRewrittenPerCall: c.maxBytesRewrittenPerCall,
}
// If the destination object attributes are not set, do not include them
// in the request. This indicates that the object attributes should be
// copied from the source object.
if c.ObjectAttrs.isZero() {
req.dstObject.attrs = nil
}

isIdempotent := c.dst.conds != nil && (c.dst.conds.GenerationMatch != 0 || c.dst.conds.DoesNotExist)
var userProject string
Expand Down
20 changes: 19 additions & 1 deletion storage/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2407,7 +2407,7 @@ func TestIntegration_Copy(t *testing.T) {
})

// Create new bucket
if err := bucketInDifferentRegion.Create(ctx, testutil.ProjID(), &BucketAttrs{Location: "NORTHAMERICA-NORTHEAST2"}); err != nil {
if err := bucketInDifferentRegion.Create(ctx, testutil.ProjID(), &BucketAttrs{Location: "US-EAST1"}); err != nil {
t.Fatalf("bucket.Create: %v", err)
}
t.Cleanup(func() {
Expand Down Expand Up @@ -2435,6 +2435,14 @@ func TestIntegration_Copy(t *testing.T) {
h.mustDeleteObject(obj)
})

// Set metadata on the source object to check if it's copied.
if _, err := obj.Update(ctx, ObjectAttrsToUpdate{
ContentLanguage: "en",
ContentDisposition: "inline",
}); err != nil {
t.Fatalf("obj.Update: %v", err)
}

attrs, err := obj.Attrs(ctx)
if err != nil {
t.Fatalf("obj.Attrs: %v", err)
Expand Down Expand Up @@ -2521,6 +2529,16 @@ func TestIntegration_Copy(t *testing.T) {
}
}

// Check that metadata is copied when no destination attributes are provided.
if test.copierAttrs == nil {
Comment thread
BrennaEpp marked this conversation as resolved.
Resolved
if attrs.ContentLanguage != "en" {
t.Errorf("unexpected ContentLanguage; got: %s, want: en", attrs.ContentLanguage)
}
if attrs.ContentDisposition != "inline" {
t.Errorf("unexpected ContentDisposition; got: %s, want: inline", attrs.ContentDisposition)
}
}

// Check the copied contents
if attrs.CRC32C != crc32c {
t.Errorf("mismatching checksum: got %v, want %v", attrs.CRC32C, crc32c)
Expand Down
12 changes: 12 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,9 @@ func parseKey(key []byte) (*rsa.PrivateKey, error) {

// toRawObject copies the editable attributes from o to the raw library's Object type.
func (o *ObjectAttrs) toRawObject(bucket string) *raw.Object {
if o == nil {
return nil
}
var ret string
if !o.RetentionExpirationTime.IsZero() {
ret = o.RetentionExpirationTime.Format(time.RFC3339)
Expand Down Expand Up @@ -1417,6 +1420,9 @@ func (o *ObjectAttrs) toRawObject(bucket string) *raw.Object {

// toProtoObject copies the editable attributes from o to the proto library's Object type.
func (o *ObjectAttrs) toProtoObject(b string) *storagepb.Object {
if o == nil {
return nil
}
// For now, there are only globally unique buckets, and "_" is the alias
// project ID for such buckets. If the bucket is not provided, like in the
// destination ObjectAttrs of a Copy, do not attempt to format it.
Expand Down Expand Up @@ -1673,6 +1679,12 @@ type ObjectAttrs struct {
HardDeleteTime time.Time
}

// isZero reports whether the ObjectAttrs struct is empty (i.e. all the
// fields are their zero value).
func (o *ObjectAttrs) isZero() bool {
return reflect.DeepEqual(o, &ObjectAttrs{})
}

// ObjectRetention contains the retention configuration for this object.
type ObjectRetention struct {
// Mode is the retention policy's mode on this object. Valid values are
Expand Down
⌂ 换网址