Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -44,6 +44,8 @@
import com.google.cloud.storage.Storage.BlobListOption;
import com.google.cloud.storage.Storage.BlobSourceOption;
import com.google.cloud.storage.Storage.BlobTargetOption;
import com.google.cloud.storage.Storage.BlobWriteOption;
import com.google.cloud.storage.Storage.BucketField;
import com.google.cloud.storage.Storage.BucketGetOption;
import com.google.cloud.storage.Storage.BucketListOption;
import com.google.cloud.storage.Storage.BucketSourceOption;
Expand All @@ -60,6 +62,8 @@
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
Expand Down Expand Up @@ -107,6 +111,66 @@ public Bucket createBucketWithStorageClassAndLocation(String bucketName) {
return bucket;
}

/**
* Example of changing a bucket's default storage class.
*/
// [TARGET update(BucketInfo, BucketTargetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "nearline"]
public Bucket changeBucketStorageClass(String bucketName, String storageClass) {
// [START storageChangeStorageClass]
Bucket bucket = storage.update(BucketInfo.newBuilder(bucketName)
// See here for possible values: http://g.co/cloud/storage/docs/storage-classes
.setStorageClass(storageClass)
.build());
// [END storageChangeStorageClass]
return bucket;
}

/**
* Example of enabling lifecycle management rules on a bucket.
*/
// [TARGET update(BucketInfo, BucketTargetOption...)]
// [VARIABLE "my_unique_bucket"]
public Bucket enableBucketLifecycleManagement(String bucketName) {
// [START storageEnableLifecycleManagement]
Bucket bucket = storage.update(BucketInfo.newBuilder(bucketName)
.setDeleteRules(Arrays.asList(
// Delete objects older than a week
new BucketInfo.AgeDeleteRule(7),
// Only keep up to 3 versions of a given object
new BucketInfo.NumNewerVersionsDeleteRule(3)))
.build());
// [END storageEnableLifecycleManagement]
return bucket;
}

/**
* Example of getting lifecycle management rules on a bucket.
*/
// [TARGET update(BucketInfo, BucketTargetOption...)]
// [VARIABLE "my_unique_bucket"]
public List<? extends BucketInfo.DeleteRule> getBucketLifecycleManagement(String bucketName) {
// [START storageGetLifecycleManagement]
Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.LIFECYCLE));
return bucket.getDeleteRules();
// [END storageGetLifecycleManagement]
}

/**
* Example of disabling lifecycle management rules on a bucket.
*/
// [TARGET update(BucketInfo, BucketTargetOption...)]
// [VARIABLE "my_unique_bucket"]
public Bucket disableBucketLifecycleManagement(String bucketName) {
// [START storageDisableLifecycleManagement]
Bucket bucket = storage.update(BucketInfo.newBuilder(bucketName)
.setDeleteRules(Collections.EMPTY_LIST)
.build());
// [END storageDisableLifecycleManagement]
return bucket;
}

/**
* Example of creating a blob with no content.
*/
Expand Down Expand Up @@ -153,6 +217,26 @@ public Blob createBlobFromInputStream(String bucketName, String blobName) {
return blob;
}

/**
* Example of uploading an encrypted blob.
*/
// [TARGET create(BlobInfo, InputStream, BlobWriteOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "my_encryption_key"]
public Blob createEncryptedBlob(String bucketName, String blobName, String encryptionKey) {
// [START storageUploadEncryptedFile]
InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));

BlobId blobId = BlobId.of(bucketName, blobName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
.setContentType("text/plain")
.build();
Blob blob = storage.create(blobInfo, content, BlobWriteOption.encryptionKey(encryptionKey));
// [END storageUploadEncryptedFile]
return blob;
}

/**
* Example of getting information on a bucket, only if its metageneration matches a value,
* otherwise a {@link StorageException} is thrown.
Expand All @@ -168,6 +252,20 @@ public Bucket getBucketWithMetageneration(String bucketName, long bucketMetagene
return bucket;
}

/**
* Example of getting storage class and location of a bucket.
*/
// [TARGET get(String, BucketGetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE 42]
public Bucket getBucketStorageClassAndLocation(String bucketName) {
// [START storageGetClassLocation]
Bucket bucket = storage.get(bucketName, BucketGetOption.fields(
BucketField.STORAGE_CLASS, BucketField.LOCATION));
// [END storageGetClassLocation]
return bucket;
}

/**
* Example of getting information on a blob, only if its metageneration matches a value,
* otherwise a {@link StorageException} is thrown.
Expand Down Expand Up @@ -446,6 +544,28 @@ public Blob copyBlobInChunks(String bucketName, String blobName, String copyBlob
return blob;
}

/**
* Example of rotating the encryption key of a blob.
*/
// [TARGET copy(CopyRequest)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "old_encryption_key"]
// [VARIABLE "new_encryption_key"]
public Blob rotateBlobEncryptionKey(
String bucketName, String blobName, String oldEncryptionKey, String newEncryptionKey) {
// [START storageRotateEncryptionKey]
BlobId blobId = BlobId.of(bucketName, blobName);
CopyRequest request = CopyRequest.newBuilder()
.setSource(blobId)
.setSourceOptions(BlobSourceOption.decryptionKey(oldEncryptionKey))
.setTarget(blobId, BlobTargetOption.encryptionKey(newEncryptionKey))
.build();
Blob blob = storage.copy(request).getResult();
// [END storageRotateEncryptionKey]
return blob;
}

/**
* Example of reading all bytes of a blob, if generation matches a value, otherwise a
* {@link StorageException} is thrown.
Expand All @@ -470,7 +590,7 @@ public byte[] readBlobFromStringsWithGeneration(String bucketName, String blobNa
// [TARGET readAllBytes(BlobId, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE 42"]
// [VARIABLE 42]
public byte[] readBlobFromId(String bucketName, String blobName, long blobGeneration) {
// [START readBlobFromId]
BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
Expand All @@ -479,6 +599,21 @@ public byte[] readBlobFromId(String bucketName, String blobName, long blobGenera
return content;
}

/**
* Example of reading all bytes of an encrypted blob.
*/
// [TARGET readAllBytes(BlobId, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "my_encryption_key"]
public byte[] readEncryptedBlob(String bucketName, String blobName, String decryptionKey) {
// [START readEncryptedBlob]
byte[] content = storage.readAllBytes(
bucketName, blobName, BlobSourceOption.decryptionKey(decryptionKey));
// [END readEncryptedBlob]
return content;
}

/**
* Example of using a batch request to delete, update and get a blob.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.testing.RemoteStorageHelper;
Expand Down Expand Up @@ -110,6 +111,36 @@ public void testCreateBucketWithStorageClassAndLocation()
assertNotNull(bucket);
}

@Test
public void testChangeBucketStorageClass()
throws ExecutionException, InterruptedException {
Bucket bucket = storageSnippets.changeBucketStorageClass(BUCKET, "nearline");
assertEquals("NEARLINE", bucket.getStorageClass());

// Restore it to regional for the sake of the rest of the tests
bucket = storageSnippets.changeBucketStorageClass(BUCKET, "standard");
assertEquals("STANDARD", bucket.getStorageClass());
}

@Test
public void testEnableDisableBucketLifecycleManagement() {
Bucket bucket = storageSnippets.enableBucketLifecycleManagement(BUCKET);
List<? extends BucketInfo.DeleteRule> deleteRules = bucket.getDeleteRules();
assertEquals(2, deleteRules.size());
assertEquals(BucketInfo.DeleteRule.Type.AGE, deleteRules.get(0).getType());
assertEquals(BucketInfo.DeleteRule.Type.NUM_NEWER_VERSIONS, deleteRules.get(1).getType());

deleteRules = storageSnippets.getBucketLifecycleManagement(BUCKET);
assertEquals(2, deleteRules.size());

bucket = storageSnippets.disableBucketLifecycleManagement(BUCKET);
deleteRules = bucket.getDeleteRules();
assertNull(deleteRules);

deleteRules = storageSnippets.getBucketLifecycleManagement(BUCKET);
assertNull(deleteRules);
}

@Test
public void testBlob() throws InterruptedException {
String blobName = "directory/test-blob";
Expand Down Expand Up @@ -145,6 +176,34 @@ public void testBlob() throws InterruptedException {
copiedBlob.delete();
}

@Test
public void testCreateUpdateEncryptedBlob() throws InterruptedException {
// Note: DO NOT put your encryption key in your code, like it is here. Store it somewhere safe,
// and read it in when you need it. This key is just here to make the code easier to read.
String encryptionKey1 = "0mMWhFvQOdS4AmxRpo8SJxXn5MjFhbz7DkKBUdUIef8=";
String blobName = "encrypted-blob";

Blob blob = storageSnippets.createEncryptedBlob(BUCKET, blobName, encryptionKey1);

assertNotNull(blob);
assertEquals("text/plain", blob.getContentType());
byte[] encryptedContent = storageSnippets.readEncryptedBlob(BUCKET, blobName, encryptionKey1);
assertEquals("Hello, World!", new String(encryptedContent));
blob = storageSnippets.getBlobFromId(BUCKET, blobName);
assertEquals("text/plain", blob.getContentType());

String encryptionKey2 = "wnxMO0w+dmxribu7rICJ+Q2ES9TLpFRIDy3/L7HN5ZA=";

blob = storageSnippets.rotateBlobEncryptionKey(
BUCKET, blobName, encryptionKey1, encryptionKey2);

assertNotNull(blob);
encryptedContent = storageSnippets.readEncryptedBlob(BUCKET, blobName, encryptionKey2);
assertEquals("Hello, World!", new String(encryptedContent));
blob = storageSnippets.getBlobFromId(BUCKET, blobName);
assertEquals("text/plain", blob.getContentType());
}

@Test
public void testCreateCopyAndGetBlob() {
String blobName = "test-create-copy-get-blob";
Expand Down Expand Up @@ -178,6 +237,13 @@ public void testGetBucketWithMetageneration() {
storageSnippets.getBucketWithMetageneration(BUCKET, -1);
}

@Test
public void testGetBucketStorageClassAndLocation() {
Bucket bucket = storageSnippets.getBucketStorageClassAndLocation(BUCKET);
assertEquals("STANDARD", bucket.getStorageClass());
assertEquals("US", bucket.getLocation());
}

@Test
public void testListBucketsWithSizeAndPrefix() throws InterruptedException {
Page<Bucket> buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET);
Expand Down
Loading