-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add samples for soft delete (objects) (#2754)
* feat: add samples for soft delete (objects) * seperate calls; run lint * style issues * one more lint issue * fix test issue * create test-specific bucket in restore test * format
- Loading branch information
1 parent
503e518
commit 41bc807
Showing
17 changed files
with
417 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
samples/snippets/src/main/java/com/example/storage/bucket/DisableSoftDelete.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2024 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.bucket; | ||
|
||
// [START storage_disable_soft_delete] | ||
import com.google.cloud.storage.Bucket; | ||
import com.google.cloud.storage.BucketInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import java.time.Duration; | ||
|
||
public class DisableSoftDelete { | ||
public static void disableSoftDelete(String projectId, String bucketName) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
// The ID of your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
Bucket bucket = storage.get(bucketName); | ||
bucket.toBuilder() | ||
.setSoftDeletePolicy( | ||
// Setting the retention duration to 0 disables Soft Delete. | ||
BucketInfo.SoftDeletePolicy.newBuilder() | ||
.setRetentionDuration(Duration.ofSeconds(0)) | ||
.build()) | ||
.build() | ||
.update(); | ||
|
||
System.out.println("Soft delete for " + bucketName + " was disabled"); | ||
} | ||
} | ||
// [END storage_disable_soft_delete] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
samples/snippets/src/main/java/com/example/storage/bucket/GetSoftDeletePolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2024 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.bucket; | ||
|
||
// [START storage_get_soft_delete_policy] | ||
import com.google.cloud.storage.BucketInfo.SoftDeletePolicy; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import java.time.Duration; | ||
|
||
public class GetSoftDeletePolicy { | ||
public static void getSoftDeletePolicy(String projectId, String bucketName) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
// The ID of your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
SoftDeletePolicy policy = storage.get(bucketName).getSoftDeletePolicy(); | ||
|
||
if (Duration.ofSeconds(0).equals(policy.getRetentionDuration())) { | ||
System.out.println("Soft delete is disabled for " + bucketName); | ||
} else { | ||
System.out.println("The soft delete policy for " + bucketName + " is:"); | ||
System.out.println(policy); | ||
} | ||
} | ||
} | ||
// [END storage_get_soft_delete_policy] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
samples/snippets/src/main/java/com/example/storage/bucket/SetSoftDeletePolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2024 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.bucket; | ||
|
||
// [START storage_set_soft_delete_policy] | ||
import com.google.cloud.storage.Bucket; | ||
import com.google.cloud.storage.BucketInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import java.time.Duration; | ||
|
||
public class SetSoftDeletePolicy { | ||
public static void setSoftDeletePolicy(String projectId, String bucketName) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
// The ID of your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
Bucket bucket = storage.get(bucketName); | ||
bucket.toBuilder() | ||
.setSoftDeletePolicy( | ||
BucketInfo.SoftDeletePolicy.newBuilder() | ||
.setRetentionDuration(Duration.ofDays(10)) | ||
.build()) | ||
.build() | ||
.update(); | ||
|
||
System.out.println( | ||
"Soft delete policy for " + bucketName + " was set to a 10-day retention period"); | ||
} | ||
} | ||
// [END storage_set_soft_delete_policy] |
41 changes: 41 additions & 0 deletions
41
samples/snippets/src/main/java/com/example/storage/object/ListSoftDeletedObjects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2024 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.object; | ||
|
||
// [START storage_list_soft_deleted_objects] | ||
import com.google.api.gax.paging.Page; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
|
||
public class ListSoftDeletedObjects { | ||
public static void listSoftDeletedObjects(String projectId, String bucketName) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
// The ID of your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
Page<Blob> blobs = storage.list(bucketName, Storage.BlobListOption.softDeleted(true)); | ||
|
||
for (Blob blob : blobs.iterateAll()) { | ||
System.out.println(blob.getName()); | ||
} | ||
} | ||
} | ||
// [END storage_list_soft_deleted_objects] |
51 changes: 51 additions & 0 deletions
51
...es/snippets/src/main/java/com/example/storage/object/ListSoftDeletedVersionsOfObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2024 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.object; | ||
|
||
// [START storage_list_soft_deleted_object_versions] | ||
import com.google.api.gax.paging.Page; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
|
||
public class ListSoftDeletedVersionsOfObject { | ||
|
||
public static void listSoftDeletedVersionOfObject( | ||
String projectId, String bucketName, String objectName) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
// The ID of your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
// The name of your GCS object | ||
// String objectName = "your-object-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
Page<Blob> blobs = | ||
storage.list( | ||
bucketName, | ||
Storage.BlobListOption.softDeleted(true), | ||
// See https://cloud.google.com/storage/docs/json_api/v1/objects/list#matchGlob | ||
Storage.BlobListOption.matchGlob(objectName)); | ||
|
||
for (Blob blob : blobs.iterateAll()) { | ||
System.out.println(blob.getName()); | ||
} | ||
} | ||
} | ||
// [END storage_list_soft_deleted_object_versions] |
43 changes: 43 additions & 0 deletions
43
samples/snippets/src/main/java/com/example/storage/object/RestoreSoftDeletedObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2024 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.object; | ||
|
||
// [START storage_restore_object] | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
|
||
public class RestoreSoftDeletedObject { | ||
public static void restoreSoftDeletedObject( | ||
String projectId, String bucketName, String objectName, long generation) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
// The ID of your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
// The name of your GCS object | ||
// String objectName = "your-object-name"; | ||
|
||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
Blob blob = storage.restore(BlobId.of(bucketName, objectName, generation)); | ||
|
||
System.out.println("Restored previously soft-deleted object " + blob.getName()); | ||
} | ||
} | ||
// [END storage_restore_object] |
Oops, something went wrong.