Skip to content

Commit 9ab4afb

Browse files
authored
Update signature of UpdateObjectEncryption (#2215)
1 parent 30849ad commit 9ab4afb

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

api-update-object-encryption.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ type UpdateObjectEncryptionOptions struct {
5252
VersionID string
5353
}
5454

55+
// UpdateObjectEncryptionResult holds the result of an UpdateObjectEncryption call.
56+
type UpdateObjectEncryptionResult struct {
57+
// VersionID is the version ID of the object that was updated, if versioning is enabled.
58+
VersionID string
59+
}
60+
5561
// UpdateObjectEncryption changes the encryption configuration of an existing object in-place.
5662
// The object must already be encrypted with SSE-S3 or SSE-KMS. SSE-C objects are not supported.
5763
// This operation rotates the data encryption key envelope without re-reading/re-writing object data.
@@ -62,19 +68,19 @@ type UpdateObjectEncryptionOptions struct {
6268
// - objectName: Name of the object
6369
// - opts: Options including KMSKeyArn (required), optional BucketKeyEnabled, and optional VersionID
6470
//
65-
// Returns an error if the operation fails.
66-
func (c *Client) UpdateObjectEncryption(ctx context.Context, bucketName, objectName string, opts UpdateObjectEncryptionOptions) error {
71+
// Returns the version ID of the updated object (if versioning is enabled) and an error if the operation fails.
72+
func (c *Client) UpdateObjectEncryption(ctx context.Context, bucketName, objectName string, opts UpdateObjectEncryptionOptions) (UpdateObjectEncryptionResult, error) {
6773
// Input validation.
6874
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
69-
return err
75+
return UpdateObjectEncryptionResult{}, err
7076
}
7177

7278
if err := s3utils.CheckValidObjectName(objectName); err != nil {
73-
return err
79+
return UpdateObjectEncryptionResult{}, err
7480
}
7581

7682
if opts.KMSKeyArn == "" {
77-
return errInvalidArgument("KMSKeyArn is required for UpdateObjectEncryption.")
83+
return UpdateObjectEncryptionResult{}, errInvalidArgument("KMSKeyArn is required for UpdateObjectEncryption.")
7884
}
7985

8086
// Get resources properly escaped and lined up before
@@ -96,7 +102,7 @@ func (c *Client) UpdateObjectEncryption(ctx context.Context, bucketName, objectN
96102

97103
bodyData, err := xml.Marshal(reqBody)
98104
if err != nil {
99-
return err
105+
return UpdateObjectEncryptionResult{}, err
100106
}
101107

102108
reqMetadata := requestMetadata{
@@ -113,10 +119,12 @@ func (c *Client) UpdateObjectEncryption(ctx context.Context, bucketName, objectN
113119
resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata)
114120
defer closeResponse(resp)
115121
if err != nil {
116-
return err
122+
return UpdateObjectEncryptionResult{}, err
117123
}
118124
if resp.StatusCode != http.StatusOK {
119-
return httpRespToErrorResponse(resp, bucketName, objectName)
125+
return UpdateObjectEncryptionResult{}, httpRespToErrorResponse(resp, bucketName, objectName)
120126
}
121-
return nil
127+
return UpdateObjectEncryptionResult{
128+
VersionID: resp.Header.Get(amzVersionID),
129+
}, nil
122130
}

api-update-object-encryption_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func TestUpdateObjectEncryptionSuccess(t *testing.T) {
9292
capturedPath = r.URL.Path
9393
capturedQuery = r.URL.Query()
9494
capturedBody, _ = io.ReadAll(r.Body)
95+
w.Header().Set("x-amz-version-id", "returned-version-id")
9596
w.WriteHeader(http.StatusOK)
9697
}))
9798
defer ts.Close()
@@ -116,11 +117,16 @@ func TestUpdateObjectEncryptionSuccess(t *testing.T) {
116117
VersionID: "test-version-id",
117118
}
118119

119-
err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", opts)
120+
result, err := client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", opts)
120121
if err != nil {
121122
t.Fatalf("UpdateObjectEncryption failed: %v", err)
122123
}
123124

125+
// Verify returned version ID from response header.
126+
if result.VersionID != "returned-version-id" {
127+
t.Fatalf("Expected VersionID 'returned-version-id', got %q", result.VersionID)
128+
}
129+
124130
// Verify request method.
125131
if capturedMethod != http.MethodPut {
126132
t.Fatalf("Expected PUT, got %s", capturedMethod)
@@ -178,7 +184,7 @@ func TestUpdateObjectEncryptionNoVersionID(t *testing.T) {
178184
t.Fatalf("Failed to create client: %v", err)
179185
}
180186

181-
err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
187+
_, err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
182188
KMSKeyArn: "my-key",
183189
})
184190
if err != nil {
@@ -212,7 +218,7 @@ func TestUpdateObjectEncryptionServerError(t *testing.T) {
212218
t.Fatalf("Failed to create client: %v", err)
213219
}
214220

215-
err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
221+
_, err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
216222
KMSKeyArn: "my-key",
217223
})
218224
if err == nil {
@@ -234,7 +240,7 @@ func TestUpdateObjectEncryptionInvalidBucket(t *testing.T) {
234240
t.Fatalf("Failed to create client: %v", err)
235241
}
236242

237-
err = client.UpdateObjectEncryption(context.Background(), "", "myobject", UpdateObjectEncryptionOptions{
243+
_, err = client.UpdateObjectEncryption(context.Background(), "", "myobject", UpdateObjectEncryptionOptions{
238244
KMSKeyArn: "my-key",
239245
})
240246
if err == nil {
@@ -251,7 +257,7 @@ func TestUpdateObjectEncryptionInvalidObject(t *testing.T) {
251257
t.Fatalf("Failed to create client: %v", err)
252258
}
253259

254-
err = client.UpdateObjectEncryption(context.Background(), "mybucket", "", UpdateObjectEncryptionOptions{
260+
_, err = client.UpdateObjectEncryption(context.Background(), "mybucket", "", UpdateObjectEncryptionOptions{
255261
KMSKeyArn: "my-key",
256262
})
257263
if err == nil {
@@ -268,7 +274,7 @@ func TestUpdateObjectEncryptionEmptyKMSKeyArn(t *testing.T) {
268274
t.Fatalf("Failed to create client: %v", err)
269275
}
270276

271-
err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
277+
_, err = client.UpdateObjectEncryption(context.Background(), "mybucket", "myobject", UpdateObjectEncryptionOptions{
272278
KMSKeyArn: "",
273279
})
274280
if err == nil {

0 commit comments

Comments
 (0)