|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.storage.object; |
| 18 | + |
| 19 | +// [START storage_move_object] |
| 20 | + |
| 21 | +import com.google.cloud.storage.BlobId; |
| 22 | +import com.google.cloud.storage.BlobInfo; |
| 23 | +import com.google.cloud.storage.Storage; |
| 24 | +import com.google.cloud.storage.Storage.MoveBlobRequest; |
| 25 | +import com.google.cloud.storage.StorageOptions; |
| 26 | + |
| 27 | +public final class AtomicMoveObject { |
| 28 | + |
| 29 | + public static void moveObject( |
| 30 | + String projectId, String bucketName, String sourceObjectName, String targetObjectName) { |
| 31 | + |
| 32 | + // The ID of your GCP project |
| 33 | + // String projectId = "your-project-id"; |
| 34 | + |
| 35 | + // The ID of your GCS bucket |
| 36 | + // String bucketName = "your-unique-bucket-name"; |
| 37 | + |
| 38 | + // The ID of your GCS object |
| 39 | + // String sourceObjectName = "your-object-name"; |
| 40 | + |
| 41 | + // The ID of your GCS object |
| 42 | + // String targetObjectName = "your-new-object-name"; |
| 43 | + |
| 44 | + Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); |
| 45 | + BlobId source = BlobId.of(bucketName, sourceObjectName); |
| 46 | + BlobId target = BlobId.of(bucketName, targetObjectName); |
| 47 | + |
| 48 | + // Optional: set a generation-match precondition to avoid potential race |
| 49 | + // conditions and data corruptions. The request returns a 412 error if the |
| 50 | + // preconditions are not met. |
| 51 | + Storage.BlobTargetOption precondition; |
| 52 | + BlobInfo existingTarget = storage.get(target); |
| 53 | + if (existingTarget == null) { |
| 54 | + // For a target object that does not yet exist, set the DoesNotExist precondition. |
| 55 | + // This will cause the request to fail if the object is created before the request runs. |
| 56 | + precondition = Storage.BlobTargetOption.doesNotExist(); |
| 57 | + } else { |
| 58 | + // If the destination already exists in your bucket, instead set a generation-match |
| 59 | + // precondition. This will cause the request to fail if the existing object's generation |
| 60 | + // changes before the request runs. |
| 61 | + precondition = Storage.BlobTargetOption.generationMatch(existingTarget.getGeneration()); |
| 62 | + } |
| 63 | + |
| 64 | + // Atomically move source object to target object within the bucket |
| 65 | + MoveBlobRequest moveBlobRequest = |
| 66 | + MoveBlobRequest.newBuilder() |
| 67 | + .setSource(source) |
| 68 | + .setTarget(target) |
| 69 | + .setTargetOptions(precondition) |
| 70 | + .build(); |
| 71 | + BlobInfo movedBlob = storage.moveBlob(moveBlobRequest); |
| 72 | + |
| 73 | + System.out.println( |
| 74 | + "Moved object " |
| 75 | + + source.toGsUtilUri() |
| 76 | + + " to " |
| 77 | + + movedBlob.getBlobId().toGsUtilUriWithGeneration()); |
| 78 | + } |
| 79 | +} |
| 80 | +// [END storage_move_object] |
0 commit comments