Skip to content

Commit

Permalink
feat: add storage RPO samples (#7700)
Browse files Browse the repository at this point in the history
* feat: add storage RPO samples

* update dependnecy

* format
  • Loading branch information
JesseLovelace authored and sydney-munro committed Feb 7, 2022
1 parent eb84e2e commit 7f00aa8
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 1 deletion.
2 changes: 1 addition & 1 deletion google-cloud-examples/pom.xml
Expand Up @@ -76,7 +76,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>2.2.3</version>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
Expand Down
@@ -0,0 +1,40 @@
package com.google.cloud.examples.storage.buckets;

// [START storage_create_bucket_turbo_replication]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Rpo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class CreateBucketWithTurboReplication {
public static void createBucketWithTurboReplication(
String projectId, String bucketName, String location) {
// The ID of your GCP project
// String projectId = "your-project-id";

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

// The dual-region location to create your bucket in
// String location = "NAM4"

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

Bucket bucket =
storage.create(
BucketInfo.newBuilder(bucketName)
.setLocation(location)
.setRpo(Rpo.ASYNC_TURBO)
.build());

System.out.println(
"Created bucket "
+ bucket.getName()
+ " in "
+ bucket.getLocation()
+ " with RPO setting"
+ bucket.getRpo());
}
}
// [END storage_create_bucket_turbo_replication]
@@ -0,0 +1,23 @@
package com.google.cloud.examples.storage.buckets;

// [START storage_get_rpo]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class GetBucketRpo {
public static void getBucketRpo(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);
String rpo = bucket.getRpo().toString();

System.out.println("The RPO setting of bucket " + bucketName + " is " + rpo);
}
}
// [END storage_get_rpo]
@@ -0,0 +1,25 @@
package com.google.cloud.examples.storage.buckets;

// [START storage_set_rpo_async_turbo]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Rpo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class SetAsyncTurboRpo {
public static void setAsyncTurboRpo(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().setRpo(Rpo.ASYNC_TURBO).build().update();

System.out.println("Turbo replication was enabled for " + bucketName);
}
}
// [END storage_set_rpo_async_turbo]
@@ -0,0 +1,25 @@
package com.google.cloud.examples.storage.buckets;

// [START storage_set_rpo_default]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Rpo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class SetDefaultRpo {
public static void setDefaultRpo(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().setRpo(Rpo.DEFAULT).build().update();

System.out.println("Replication was set to default for " + bucketName);
}
}
// [END storage_set_rpo_default]
Expand Up @@ -28,6 +28,7 @@
import com.google.cloud.examples.storage.buckets.ConfigureBucketCors;
import com.google.cloud.examples.storage.buckets.CreateBucket;
import com.google.cloud.examples.storage.buckets.CreateBucketWithStorageClassAndLocation;
import com.google.cloud.examples.storage.buckets.CreateBucketWithTurboReplication;
import com.google.cloud.examples.storage.buckets.DeleteBucket;
import com.google.cloud.examples.storage.buckets.DisableBucketVersioning;
import com.google.cloud.examples.storage.buckets.DisableLifecycleManagement;
Expand All @@ -36,6 +37,7 @@
import com.google.cloud.examples.storage.buckets.EnableLifecycleManagement;
import com.google.cloud.examples.storage.buckets.EnableRequesterPays;
import com.google.cloud.examples.storage.buckets.GetBucketMetadata;
import com.google.cloud.examples.storage.buckets.GetBucketRpo;
import com.google.cloud.examples.storage.buckets.GetPublicAccessPrevention;
import com.google.cloud.examples.storage.buckets.ListBucketIamMembers;
import com.google.cloud.examples.storage.buckets.ListBuckets;
Expand All @@ -45,7 +47,9 @@
import com.google.cloud.examples.storage.buckets.RemoveBucketIamConditionalBinding;
import com.google.cloud.examples.storage.buckets.RemoveBucketIamMember;
import com.google.cloud.examples.storage.buckets.RemoveBucketLabel;
import com.google.cloud.examples.storage.buckets.SetAsyncTurboRpo;
import com.google.cloud.examples.storage.buckets.SetBucketWebsiteInfo;
import com.google.cloud.examples.storage.buckets.SetDefaultRpo;
import com.google.cloud.examples.storage.buckets.SetPublicAccessPreventionEnforced;
import com.google.cloud.examples.storage.buckets.SetPublicAccessPreventionInherited;
import com.google.cloud.examples.storage.objects.DownloadRequesterPaysObject;
Expand Down Expand Up @@ -520,4 +524,33 @@ public void testRequesterPays() throws Exception {
DisableRequesterPays.disableRequesterPays(PROJECT_ID, BUCKET);
assertFalse(storage.get(BUCKET).requesterPays());
}

@Test
public void testRpo() throws Exception {
String rpoBucket = RemoteStorageHelper.generateBucketName();
try {
CreateBucketWithTurboReplication.createBucketWithTurboReplication(
PROJECT_ID, rpoBucket, "NAM4");
Bucket bucket = storage.get(rpoBucket);
assertEquals("ASYNC_TURBO", bucket.getRpo().toString());

SetDefaultRpo.setDefaultRpo(PROJECT_ID, rpoBucket);
bucket = storage.get(rpoBucket);
assertEquals("DEFAULT", bucket.getRpo().toString());

SetAsyncTurboRpo.setAsyncTurboRpo(PROJECT_ID, rpoBucket);
bucket = storage.get(rpoBucket);
assertEquals("ASYNC_TURBO", bucket.getRpo().toString());

PrintStream standardOut = System.out;
final ByteArrayOutputStream snippetOutputCapture = new ByteArrayOutputStream();
System.setOut(new PrintStream(snippetOutputCapture));
GetBucketRpo.getBucketRpo(PROJECT_ID, rpoBucket);
String snippetOutput = snippetOutputCapture.toString();
System.setOut(standardOut);
assertTrue(snippetOutput.contains("ASYNC_TURBO"));
} finally {
storage.delete(rpoBucket);
}
}
}

0 comments on commit 7f00aa8

Please sign in to comment.