Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public static void composeObject(
String firstObjectName,
String secondObjectName,
String targetObjectName,
String projectId) {
String projectId,
boolean deleteSourceObjects) {
// The ID of your GCP project
// String projectId = "your-project-id";

Expand Down Expand Up @@ -70,17 +71,20 @@ public static void composeObject(
.addSource(firstObjectName, secondObjectName)
.setTarget(BlobInfo.newBuilder(bucketName, targetObjectName).build())
.setTargetOptions(precondition)
.setDeleteSourceObjects(deleteSourceObjects)
.build();

Blob compositeObject = storage.compose(composeRequest);

Comment on lines +74 to 78

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The ComposeRequest.Builder class in the Google Cloud Storage Java SDK does not have a setDeleteSourceObjects method, and the Cloud Storage Compose API does not support deleting source objects as part of the compose request. To achieve this behavior, you must explicitly delete the source objects using storage.delete(...) after the compose operation succeeds.

            .build();

    Blob compositeObject = storage.compose(composeRequest);

    if (deleteSourceObjects) {
      storage.delete(bucketName, firstObjectName);
      storage.delete(bucketName, secondObjectName);
    }

String deletionMessage = deleteSourceObjects ? " and the source objects were deleted." : ".";
System.out.println(
"New composite object "
+ compositeObject.getName()
+ " was created by combining "
+ firstObjectName
+ " and "
+ secondObjectName);
+ secondObjectName
+ deletionMessage);
}
}
// [END storage_compose_file]
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,39 @@ public void testComposeObject() {
BlobInfo.newBuilder(bucket.getName(), secondObject).build(), secondObject.getBytes(UTF_8));

ComposeObject.composeObject(
bucket.getName(), firstObject, secondObject, targetObject, GOOGLE_CLOUD_PROJECT);
bucket.getName(), firstObject, secondObject, targetObject, GOOGLE_CLOUD_PROJECT, false);

String got = stdOut.getCapturedOutputAsUtf8String();
assertThat(got).contains(firstObject);
assertThat(got).contains(secondObject);
assertThat(got).contains(targetObject);
// Verify source objects still exist
assertThat(storage.get(bucket.getName(), firstObject)).isNotNull();
assertThat(storage.get(bucket.getName(), secondObject)).isNotNull();
}

@Test
public void testComposeObjectWithDeleteSources() {
String firstObject = generator.randomObjectName();
String secondObject = generator.randomObjectName();
String targetObject = generator.randomObjectName();
storage.create(
BlobInfo.newBuilder(bucket.getName(), firstObject).build(), firstObject.getBytes(UTF_8));
storage.create(
BlobInfo.newBuilder(bucket.getName(), secondObject).build(), secondObject.getBytes(UTF_8));

ComposeObject.composeObject(
bucket.getName(), firstObject, secondObject, targetObject, GOOGLE_CLOUD_PROJECT, true);

String got = stdOut.getCapturedOutputAsUtf8String();
assertThat(got).contains(firstObject);
assertThat(got).contains(secondObject);
assertThat(got).contains(targetObject);
assertThat(got).contains("and the source objects were deleted");

// Verify source objects are deleted
assertThat(storage.get(bucket.getName(), firstObject)).isNull();
assertThat(storage.get(bucket.getName(), secondObject)).isNull();
}

@Test
Expand Down
Loading