Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ Builder setCustomerEncryption(CustomerEncryption customerEncryption) {
return this;
}

@Override
Builder setKmsKey(String kmsKey) {
infoBuilder.setKmsKey(kmsKey);
return this;
}

@Override
public Blob build() {
return new Blob(storage, infoBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public StorageObject apply(BlobInfo blobInfo) {
private final Integer componentCount;
private final boolean isDirectory;
private final CustomerEncryption customerEncryption;
private final String kmsKey;

/**
* This class is meant for internal use only. Users are discouraged from using this class.
Expand Down Expand Up @@ -266,6 +267,8 @@ public abstract static class Builder {

abstract Builder setCustomerEncryption(CustomerEncryption customerEncryption);

abstract Builder setKmsKey(String kmsKey);

/**
* Creates a {@code BlobInfo} object.
*/
Expand Down Expand Up @@ -298,6 +301,7 @@ static final class BuilderImpl extends Builder {
private Boolean isDirectory;
private CustomerEncryption customerEncryption;
private StorageClass storageClass;
private String kmsKey;

BuilderImpl(BlobId blobId) {
this.blobId = blobId;
Expand Down Expand Up @@ -328,6 +332,7 @@ static final class BuilderImpl extends Builder {
createTime = blobInfo.createTime;
isDirectory = blobInfo.isDirectory;
storageClass = blobInfo.storageClass;
kmsKey = blobInfo.kmsKey;
}

@Override
Expand Down Expand Up @@ -475,6 +480,12 @@ Builder setCustomerEncryption(CustomerEncryption customerEncryption) {
return this;
}

@Override
Builder setKmsKey(String kmsKey) {
this.kmsKey = kmsKey;
return this;
}

@Override
public BlobInfo build() {
checkNotNull(blobId);
Expand Down Expand Up @@ -507,6 +518,7 @@ public BlobInfo build() {
createTime = builder.createTime;
isDirectory = firstNonNull(builder.isDirectory, Boolean.FALSE);
storageClass = builder.storageClass;
kmsKey = builder.kmsKey;
}

/**
Expand Down Expand Up @@ -737,6 +749,13 @@ public StorageClass getStorageClass() {
return storageClass;
}

/**
* Returns the Cloud KMS key used to encrypt the blob, if any.
*/
public String getKmsKey() {
return kmsKey;
}

/**
* Returns a builder for the current blob.
*/
Expand Down Expand Up @@ -809,6 +828,9 @@ public ObjectAccessControl apply(Acl acl) {
if (customerEncryption != null) {
storageObject.setCustomerEncryption(customerEncryption.toPb());
}
if (kmsKey != null) {
storageObject.setKmsKeyName(kmsKey);
}
storageObject.setMetadata(pbMetadata);
storageObject.setCacheControl(cacheControl);
storageObject.setContentEncoding(contentEncoding);
Expand Down Expand Up @@ -939,6 +961,9 @@ public Acl apply(ObjectAccessControl objectAccessControl) {
if (storageObject.getStorageClass() != null) {
builder.setStorageClass(StorageClass.valueOf(storageObject.getStorageClass()));
}
if (storageObject.getKmsKeyName() != null) {
builder.setKmsKey(storageObject.getKmsKeyName());
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,12 @@ public Builder setLabels(Map<String, String> labels) {
return this;
}

@Override
public Builder setDefaultKmsKeyName(String defaultKmsKeyName) {
infoBuilder.setDefaultKmsKeyName(defaultKmsKeyName);
return this;
}

@Override
public Bucket build() {
return new Bucket(storage, infoBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.client.util.DateTime;
import com.google.api.services.storage.model.*;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.Bucket.Encryption;
import com.google.api.services.storage.model.Bucket.Lifecycle;
import com.google.api.services.storage.model.Bucket.Lifecycle.Rule;
import com.google.api.services.storage.model.Bucket.Owner;
Expand Down Expand Up @@ -85,6 +86,7 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo)
private final String location;
private final StorageClass storageClass;
private final Map<String, String> labels;
private final String defaultKmsKeyName;

/**
* Base class for bucket's delete rules. Allows to configure automatic deletion of blobs and blobs
Expand Down Expand Up @@ -423,6 +425,11 @@ public abstract static class Builder {
*/
public abstract Builder setLabels(Map<String, String> labels);

/**
* Sets the default Cloud KMS key name for this bucket.
*/
public abstract Builder setDefaultKmsKeyName(String defaultKmsKeyName);

/**
* Creates a {@code BucketInfo} object.
*/
Expand All @@ -449,6 +456,7 @@ static final class BuilderImpl extends Builder {
private List<Acl> acl;
private List<Acl> defaultAcl;
private Map<String, String> labels;
private String defaultKmsKeyName;

BuilderImpl(String name) {
this.name = name;
Expand All @@ -473,6 +481,7 @@ static final class BuilderImpl extends Builder {
deleteRules = bucketInfo.deleteRules;
labels = bucketInfo.labels;
requesterPays = bucketInfo.requesterPays;
defaultKmsKeyName = bucketInfo.defaultKmsKeyName;
}

@Override
Expand Down Expand Up @@ -584,6 +593,12 @@ public Builder setLabels(Map<String, String> labels) {
return this;
}

@Override
public Builder setDefaultKmsKeyName(String defaultKmsKeyName) {
this.defaultKmsKeyName = defaultKmsKeyName;
return this;
}

@Override
public BucketInfo build() {
checkNotNull(name);
Expand All @@ -610,6 +625,7 @@ public BucketInfo build() {
deleteRules = builder.deleteRules;
labels = builder.labels;
requesterPays = builder.requesterPays;
defaultKmsKeyName = builder.defaultKmsKeyName;
}

/**
Expand Down Expand Up @@ -762,6 +778,13 @@ public Map<String, String> getLabels() {
return labels;
}

/**
* Returns the default Cloud KMS key to be applied to newly inserted objects in this bucket.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

*/
public String getDefaultKmsKeyName() {
return defaultKmsKeyName;
}

/**
* Returns a builder for the current bucket.
*/
Expand Down Expand Up @@ -857,7 +880,9 @@ public Rule apply(DeleteRule deleteRule) {
if (labels != null) {
bucketPb.setLabels(labels);
}

if (defaultKmsKeyName != null) {
bucketPb.setEncryption(new Encryption().setDefaultKmsKeyName(defaultKmsKeyName));
}
return bucketPb;
}

Expand Down Expand Up @@ -945,6 +970,10 @@ public DeleteRule apply(Rule rule) {
if (billing != null) {
builder.setRequesterPays(billing.getRequesterPays());
}
Encryption encryption = bucketPb.getEncryption();
if (encryption != null && encryption.getDefaultKmsKeyName() != null && !encryption.getDefaultKmsKeyName().isEmpty()) {
builder.setDefaultKmsKeyName(encryption.getDefaultKmsKeyName());

This comment was marked as spam.

}
return builder.build();
}
}