Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 12 additions & 2 deletions storage/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,14 +953,24 @@ func (c *grpcStorageClient) ComposeObject(ctx context.Context, req *composeObjec
}
func (c *grpcStorageClient) RewriteObject(ctx context.Context, req *rewriteObjectRequest, opts ...storageOption) (*rewriteObjectResponse, error) {
s := callSettings(c.settings, opts...)
obj := req.dstObject.attrs.toProtoObject("")

var dst *storagepb.Object
// 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 req.dstObject.attrs.isZero() {
dst = nil
} else {
dst = req.dstObject.attrs.toProtoObject("")
}

call := &storagepb.RewriteObjectRequest{
SourceBucket: bucketResourceName(globalProjectAlias, req.srcObject.bucket),
SourceObject: req.srcObject.name,
RewriteToken: req.token,
DestinationBucket: bucketResourceName(globalProjectAlias, req.dstObject.bucket),
DestinationName: req.dstObject.name,
Destination: obj,
Destination: dst,
DestinationKmsKey: req.dstObject.keyName,
DestinationPredefinedAcl: req.predefinedACL,
CommonObjectRequestParams: toProtoCommonObjectRequestParams(req.dstObject.encryptionKey),
Expand Down
18 changes: 17 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 @@ -2519,6 +2527,14 @@ func TestIntegration_Copy(t *testing.T) {
if attrs.ContentEncoding != test.copierAttrs.contentEncoding {
t.Errorf("unexpected ContentEncoding; got: %s, want: %s", attrs.ContentEncoding, test.copierAttrs.contentEncoding)
}
} else {
// Check that metadata is copied when no destination attributes are provided.
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
Expand Down
6 changes: 6 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,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
⌂ 换网址