com.google.cloud.storage.StorageClass Java Examples

The following examples show how to use com.google.cloud.storage.StorageClass. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChangeDefaultStorageClass.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void changeDefaultStorageClass(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";

  // See the StorageClass documentation for other valid storage classes:
  // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
  StorageClass storageClass = StorageClass.COLDLINE;

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  Bucket bucket = storage.get(bucketName);
  bucket = bucket.toBuilder().setStorageClass(storageClass).build().update();

  System.out.println(
      "Default storage class for bucket "
          + bucketName
          + " has been set to "
          + bucket.getStorageClass());
}
 
Example #2
Source File: GoogleCloudBlobStore.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 5 votes vote down vote up
protected Bucket getOrCreateStorageBucket(final String location) {
  Bucket bucket = storage.get(getConfiguredBucketName());
  if (bucket == null) {
    bucket = storage.create(
        BucketInfo.newBuilder(getConfiguredBucketName())
            .setLocation(location)
            .setStorageClass(StorageClass.REGIONAL)
            .build());
  }

  return bucket;
}
 
Example #3
Source File: CreateBucketWithStorageClassAndLocation.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void createBucketWithStorageClassAndLocation(String projectId, String bucketName) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

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

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

  // See the StorageClass documentation for other valid storage classes:
  // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
  StorageClass storageClass = StorageClass.COLDLINE;

  // See this documentation for other valid locations:
  // http://g.co/cloud/storage/docs/bucket-locations#location-mr
  String location = "asia";

  Bucket bucket =
      storage.create(
          BucketInfo.newBuilder(bucketName)
              .setStorageClass(storageClass)
              .setLocation(location)
              .build());

  System.out.println(
      "Created bucket "
          + bucket.getName()
          + " in "
          + bucket.getLocation()
          + " with storage class "
          + bucket.getStorageClass());
}
 
Example #4
Source File: ChangeObjectStorageClass.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void changeObjectStorageClass(
    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 ID of your GCS object
  // String objectName = "your-object-name";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  BlobId blobId = BlobId.of(bucketName, objectName);

  // See the StorageClass documentation for other valid storage classes:
  // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
  StorageClass storageClass = StorageClass.COLDLINE;

  // You can't change an object's storage class directly, the only way is to rewrite the object
  // with the
  // desired storage class
  Storage.CopyRequest request =
      Storage.CopyRequest.newBuilder()
          .setSource(blobId)
          .setTarget(BlobInfo.newBuilder(blobId).setStorageClass(storageClass).build())
          .build();
  Blob updatedBlob = storage.copy(request).getResult();

  System.out.println(
      "Object "
          + objectName
          + " in bucket "
          + bucketName
          + " had its storage class set to "
          + updatedBlob.getStorageClass().name());
}
 
Example #5
Source File: ITBlobSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeObjectStorageClass() {
  Blob blob = storage.get(BUCKET, BLOB);
  Assert.assertNotEquals(StorageClass.COLDLINE, blob.getStorageClass());
  ChangeObjectStorageClass.changeObjectStorageClass(PROJECT_ID, BUCKET, BLOB);
  assertEquals(StorageClass.COLDLINE, storage.get(BUCKET, BLOB).getStorageClass());
  assertArrayEquals(CONTENT, storage.get(BUCKET, BLOB).getContent());
}