Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
@@ -0,0 +1,59 @@
/*
* Copyright 2025 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
*
* http://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 com.example.storage.multipartupload;

// [START storage_abort_multipart_upload]

import com.google.cloud.storage.HttpStorageOptions;
import com.google.cloud.storage.MultipartUploadClient;
import com.google.cloud.storage.MultipartUploadSettings;
import com.google.cloud.storage.multipartupload.model.AbortMultipartUploadRequest;

public class AbortMultipartUpload {
public static void abortMultipartUpload(
String projectId, String bucketName, String objectName, String uploadId) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The ID of your GCS object
// String objectName = "your-object-name";

// The ID of the multipart upload
// String uploadId = "your-upload-id";

HttpStorageOptions storageOptions =
HttpStorageOptions.newBuilder().setProjectId(projectId).build();
MultipartUploadSettings mpuSettings = MultipartUploadSettings.of(storageOptions);
MultipartUploadClient mpuClient = MultipartUploadClient.create(mpuSettings);

System.out.println("Aborting multipart upload: " + uploadId);
AbortMultipartUploadRequest abortRequest =
AbortMultipartUploadRequest.builder()
.bucket(bucketName)
.key(objectName)
.uploadId(uploadId)
.build();

mpuClient.abortMultipartUpload(abortRequest);

System.out.println("Multipart upload with ID " + uploadId + " has been successfully aborted.");
}
}
// [END storage_abort_multipart_upload]
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2025 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
*
* http://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 com.example.storage.multipartupload;

// [START storage_complete_multipart_upload]

import com.google.cloud.storage.HttpStorageOptions;
import com.google.cloud.storage.MultipartUploadClient;
import com.google.cloud.storage.MultipartUploadSettings;
import com.google.cloud.storage.multipartupload.model.CompleteMultipartUploadRequest;
import com.google.cloud.storage.multipartupload.model.CompleteMultipartUploadResponse;
import com.google.cloud.storage.multipartupload.model.CompletedMultipartUpload;
import com.google.cloud.storage.multipartupload.model.CompletedPart;
import java.util.List;

public class CompleteMultipartUpload {
public static void completeMultipartUpload(
String projectId,
String bucketName,
String objectName,
String uploadId,
List<CompletedPart> completedParts) {

// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The ID of your GCS object
// String objectName = "your-object-name";

// The ID of the multipart upload
// String uploadId = "your-upload-id";

// The list of completed parts from the UploadPart responses.
// List<CompletedPart> completedParts = ...;

HttpStorageOptions storageOptions =
HttpStorageOptions.newBuilder().setProjectId(projectId).build();
MultipartUploadSettings mpuSettings = MultipartUploadSettings.of(storageOptions);
MultipartUploadClient mpuClient = MultipartUploadClient.create(mpuSettings);

System.out.println("Completing multipart upload for " + objectName);

CompletedMultipartUpload completedMultipartUpload =
CompletedMultipartUpload.builder().parts(completedParts).build();

CompleteMultipartUploadRequest completeRequest =
CompleteMultipartUploadRequest.builder()
.bucket(bucketName)
.key(objectName)
.uploadId(uploadId)
.multipartUpload(completedMultipartUpload)
.build();

CompleteMultipartUploadResponse completeResponse =
mpuClient.completeMultipartUpload(completeRequest);

System.out.println(
"Upload complete for "
+ completeResponse.key()
+ " in bucket "
+ completeResponse.bucket());
System.out.println("Final ETag: " + completeResponse.etag());
}
}
// [END storage_complete_multipart_upload]
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2025 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
*
* http://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 com.example.storage.multipartupload;

// [START storage_create_multipart_upload]

import com.google.cloud.storage.HttpStorageOptions;
import com.google.cloud.storage.MultipartUploadClient;
import com.google.cloud.storage.MultipartUploadSettings;
import com.google.cloud.storage.multipartupload.model.CreateMultipartUploadRequest;
import com.google.cloud.storage.multipartupload.model.CreateMultipartUploadResponse;

public class CreateMultipartUpload {
public static void createMultipartUpload(String projectId, String bucketName, String objectName) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String sourceBucketName = "your-unique-bucket-name";

// The ID of your GCS object
// String sourceObjectName = "your-object-name";

HttpStorageOptions storageOptions =
HttpStorageOptions.newBuilder().setProjectId(projectId).build();
MultipartUploadSettings mpuSettings = MultipartUploadSettings.of(storageOptions);
MultipartUploadClient mpuClient = MultipartUploadClient.create(mpuSettings);

System.out.println("Initiating multipart upload for " + objectName);
CreateMultipartUploadRequest createRequest =
CreateMultipartUploadRequest.builder().bucket(bucketName).key(objectName).build();
CreateMultipartUploadResponse createResponse = mpuClient.createMultipartUpload(createRequest);
String uploadId = createResponse.uploadId();
System.out.println("Upload ID: " + uploadId);
}
}
// [END storage_create_multipart_upload]
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2025 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
*
* http://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 com.example.storage.multipartupload;

// [START storage_list_parts]

import com.google.cloud.storage.HttpStorageOptions;
import com.google.cloud.storage.MultipartUploadClient;
import com.google.cloud.storage.MultipartUploadSettings;
import com.google.cloud.storage.multipartupload.model.ListPartsRequest;
import com.google.cloud.storage.multipartupload.model.ListPartsResponse;
import com.google.cloud.storage.multipartupload.model.Part;

public class ListParts {
public static void listParts(
String projectId, String bucketName, String objectName, String uploadId) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The ID of your GCS object
// String objectName = "your-object-name";

// The ID of the multipart upload
// String uploadId = "your-upload-id";

HttpStorageOptions storageOptions =
HttpStorageOptions.newBuilder().setProjectId(projectId).build();
MultipartUploadSettings mpuSettings = MultipartUploadSettings.of(storageOptions);
MultipartUploadClient mpuClient = MultipartUploadClient.create(mpuSettings);

System.out.println("Listing parts for upload ID: " + uploadId);

ListPartsRequest listPartsRequest =
ListPartsRequest.builder().bucket(bucketName).key(objectName).uploadId(uploadId).build();

ListPartsResponse listPartsResponse = mpuClient.listParts(listPartsRequest);

if (listPartsResponse.getParts() == null || listPartsResponse.getParts().isEmpty()) {
System.out.println("No parts have been uploaded yet.");
return;
}

System.out.println("Uploaded Parts:");
for (Part part : listPartsResponse.getParts()) {
System.out.println(" - Part Number: " + part.partNumber());
System.out.println(" ETag: " + part.eTag());
System.out.println(" Size: " + part.size() + " bytes");
System.out.println(" Last Modified: " + part.lastModified());
}
}
}
// [END storage_list_parts]
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2025 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
*
* http://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 com.example.storage.multipartupload;

// [START storage_upload_part]

import com.google.cloud.storage.HttpStorageOptions;
import com.google.cloud.storage.MultipartUploadClient;
import com.google.cloud.storage.MultipartUploadSettings;
import com.google.cloud.storage.RequestBody;
import com.google.cloud.storage.multipartupload.model.UploadPartRequest;
import com.google.cloud.storage.multipartupload.model.UploadPartResponse;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class UploadPartFromFile {
public static void uploadPartFromFile(
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets have a separate End-to-End sample, which reads a filepath, breaks it into parts and performs MPU. This sample will call 3 apis for MPU.

FIne to do this in a follow-up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay

String projectId, String bucketName, String objectName, String uploadId, String filePath)
throws IOException {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The ID of your GCS object
// String objectName = "your-object-name";

// The ID of the multipart upload
// String uploadId = "your-upload-id";

// The path to the file to upload
// String filePath = "/path/to/your/file.txt";

HttpStorageOptions storageOptions =
HttpStorageOptions.newBuilder().setProjectId(projectId).build();
MultipartUploadSettings mpuSettings = MultipartUploadSettings.of(storageOptions);
MultipartUploadClient mpuClient = MultipartUploadClient.create(mpuSettings);

// The minimum part size for a multipart upload is 5 MiB, except for the last part.
int partSize = 8 * 1024 * 1024;

Path path = Paths.get(filePath);
long fileSize = Files.size(path);
long partCount = (long) Math.ceil((double) fileSize / partSize);
System.out.println("File will be uploaded in " + partCount + " parts.");

try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ)) {
ByteBuffer buffer = ByteBuffer.allocate(partSize);
for (int partNumber = 1; partNumber <= partCount; partNumber++) {
buffer.clear();
int bytesRead = channel.read(buffer);
buffer.flip();

RequestBody requestBody = RequestBody.of(buffer.slice(0, bytesRead));

System.out.println("Uploading part " + partNumber);
UploadPartRequest uploadPartRequest =
UploadPartRequest.builder()
.bucket(bucketName)
.key(objectName)
.partNumber(partNumber)
.uploadId(uploadId)
.build();

UploadPartResponse uploadPartResponse =
mpuClient.uploadPart(uploadPartRequest, requestBody);

System.out.println(
"Part " + partNumber + " uploaded with ETag: " + uploadPartResponse.eTag());
}
}

System.out.println("All parts uploaded.");
}
}
// [END storage_upload_part]