Skip to content

Commit

Permalink
POSC support for Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
shinfan committed Feb 28, 2017
1 parent ba10fac commit 37f08e9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ public Builder setMetadata(Map<String, String> metadata) {
return this;
}

@Override
public Builder setStorageClass(String storageClass) {
infoBuilder.setStorageClass(storageClass);
return this;
}

@Override
Builder setMetageneration(Long metageneration) {
infoBuilder.setMetageneration(metageneration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public StorageObject apply(BlobInfo blobInfo) {
private final String contentEncoding;
private final String contentDisposition;
private final String contentLanguage;
private final String storageClass;
private final Integer componentCount;
private final boolean isDirectory;
private final CustomerEncryption customerEncryption;
Expand Down Expand Up @@ -340,6 +341,11 @@ public abstract static class Builder {
@Deprecated
public abstract Builder metadata(Map<String, String> metadata);

/**
* Sets the blob's storage class.
*/
public abstract Builder setStorageClass(String storageClass);

/**
* Sets the blob's user provided metadata.
*/
Expand All @@ -357,6 +363,7 @@ public abstract static class Builder {

abstract Builder setCustomerEncryption(CustomerEncryption customerEncryption);


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

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

@Override
Expand Down Expand Up @@ -582,6 +591,12 @@ public Builder setMetadata(Map<String, String> metadata) {
return this;
}

@Override
public Builder setStorageClass(String storageClass) {
this.storageClass = storageClass;
return this;
}

@Override
Builder setMetageneration(Long metageneration) {
this.metageneration = metageneration;
Expand Down Expand Up @@ -649,6 +664,7 @@ public BlobInfo build() {
updateTime = builder.updateTime;
createTime = builder.createTime;
isDirectory = firstNonNull(builder.isDirectory, Boolean.FALSE);
storageClass = builder.storageClass;
}

/**
Expand Down Expand Up @@ -1106,6 +1122,13 @@ public CustomerEncryption getCustomerEncryption() {
return customerEncryption;
}

/**
* Returns the storage class of the blob.
*/
public String getStorageClass() {
return storageClass;
}

/**
* Returns a builder for the current blob.
*/
Expand Down Expand Up @@ -1163,6 +1186,10 @@ public ObjectAccessControl apply(Acl acl) {
if (owner != null) {
storageObject.setOwner(new Owner().setEntity(owner.toPb()));
}
if (storageClass != null) {
storageObject.setStorageClass(storageClass);
}

Map<String, String> pbMetadata = metadata;
if (metadata != null && !Data.isNull(metadata)) {
pbMetadata = Maps.newHashMapWithExpectedSize(metadata.size());
Expand Down Expand Up @@ -1341,6 +1368,9 @@ public Acl apply(ObjectAccessControl objectAccessControl) {
builder.setCustomerEncryption(
CustomerEncryption.fromPb(storageObject.getCustomerEncryption()));
}
if (storageObject.getStorageClass() != null) {
builder.setStorageClass(storageObject.getStorageClass());
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class BlobInfoTest {
private static final String KEY_SHA256 = "keySha";
private static final CustomerEncryption CUSTOMER_ENCRYPTION =
new CustomerEncryption(ENCRYPTION_ALGORITHM, KEY_SHA256);
private static final String STORAGE_CLASS = "COLDLINE";

private static final BlobInfo BLOB_INFO = BlobInfo.newBuilder("b", "n", GENERATION)
.setAcl(ACL)
.setComponentCount(COMPONENT_COUNT)
Expand All @@ -88,6 +90,7 @@ public class BlobInfoTest {
.setSize(SIZE)
.setUpdateTime(UPDATE_TIME)
.setCreateTime(CREATE_TIME)
.setStorageClass(STORAGE_CLASS)
.build();
private static final BlobInfo DIRECTORY_INFO = BlobInfo.newBuilder("b", "n/")
.setSize(0L)
Expand Down Expand Up @@ -188,6 +191,7 @@ public void testBuilder() {
assertEquals(SIZE, BLOB_INFO.getSize());
assertEquals(UPDATE_TIME, BLOB_INFO.getUpdateTime());
assertEquals(CREATE_TIME, BLOB_INFO.getCreateTime());
assertEquals(STORAGE_CLASS, BLOB_INFO.getStorageClass());
assertFalse(BLOB_INFO.isDirectory());
assertEquals("b", DIRECTORY_INFO.getBucket());
assertEquals("n/", DIRECTORY_INFO.getName());
Expand Down Expand Up @@ -296,6 +300,7 @@ private void compareBlobs(BlobInfo expected, BlobInfo value) {
assertEquals(expected.getSelfLink(), value.getSelfLink());
assertEquals(expected.getSize(), value.getSize());
assertEquals(expected.getUpdateTime(), value.getUpdateTime());
assertEquals(expected.getStorageClass(), value.getStorageClass());
}

private void compareCustomerEncryptions(CustomerEncryption expected, CustomerEncryption value) {
Expand Down Expand Up @@ -342,6 +347,7 @@ public void testToPbAndFromPb() {
assertNull(blobInfo.getSelfLink());
assertEquals(0L, (long) blobInfo.getSize());
assertNull(blobInfo.getUpdateTime());
assertNull(blobInfo.getStorageClass());
assertTrue(blobInfo.isDirectory());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public class ITStorageTest {
new SecretKeySpec(BaseEncoding.base64().decode(BASE64_KEY), "AES256");
private static final byte[] COMPRESSED_CONTENT = BaseEncoding.base64()
.decode("H4sIAAAAAAAAAPNIzcnJV3DPz0/PSVVwzskvTVEILskvSkxPVQQA/LySchsAAAA=");
private static final String STORAGE_CLASS_COLDLINE = "COLDLINE";
private static final String STORAGE_CLASS_STANDARD = "STANDARD";

@BeforeClass
public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException {
Expand Down Expand Up @@ -789,6 +791,29 @@ public void testCopyBlobUpdateMetadata() {
assertTrue(storage.delete(BUCKET, targetBlobName));
}

@Test
public void testCopyBlobUpdateStorageClass() {
String sourceBlobName = "test-copy-blob-update-storage-class-source";
BlobId source = BlobId.of(BUCKET, sourceBlobName);
BlobInfo sourceInfo =
BlobInfo.newBuilder(source).setStorageClass(STORAGE_CLASS_STANDARD).build();
Blob remoteSourceBlob = storage.create(sourceInfo, BLOB_BYTE_CONTENT);
assertNotNull(remoteSourceBlob);
assertEquals(STORAGE_CLASS_STANDARD, remoteSourceBlob.getStorageClass());

String targetBlobName = "test-copy-blob-update-storage-class-target";
BlobInfo targetInfo = BlobInfo
.newBuilder(BUCKET, targetBlobName).setStorageClass(STORAGE_CLASS_COLDLINE).build();
Storage.CopyRequest req = Storage.CopyRequest.of(source, targetInfo);
CopyWriter copyWriter = storage.copy(req);
assertEquals(BUCKET, copyWriter.getResult().getBucket());
assertEquals(targetBlobName, copyWriter.getResult().getName());
assertEquals(STORAGE_CLASS_COLDLINE, copyWriter.getResult().getStorageClass());
assertTrue(copyWriter.isDone());
assertTrue(remoteSourceBlob.delete());
assertTrue(storage.delete(BUCKET, targetBlobName));
}

@Test
public void testCopyBlobNoContentType() {
String sourceBlobName = "test-copy-blob-no-content-type-source";
Expand Down

0 comments on commit 37f08e9

Please sign in to comment.