Skip to content

Commit

Permalink
POSC support for Storage (googleapis#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinfan authored and rybosome committed Mar 9, 2017
1 parent 4e15355 commit 339f172
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.google.cloud.storage.Storage.SignUrlOption;
import com.google.cloud.storage.StorageBatch;
import com.google.cloud.storage.StorageBatchResult;
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageException;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -100,7 +101,7 @@ public Bucket createBucketWithStorageClassAndLocation(String bucketName) {
// [START createBucketWithStorageClassAndLocation]
Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName)
// See here for possible values: http://g.co/cloud/storage/docs/storage-classes
.setStorageClass("COLDLINE")
.setStorageClass(StorageClass.COLDLINE)
// Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr
.setLocation("asia")
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ public Builder setMetadata(Map<String, String> metadata) {
return this;
}

@Override
public Builder setStorageClass(StorageClass 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 @@ -24,6 +24,7 @@
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.api.services.storage.model.StorageObject;
import com.google.api.services.storage.model.StorageObject.Owner;
import com.google.cloud.storage.Blob.Builder;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -78,6 +79,7 @@ public StorageObject apply(BlobInfo blobInfo) {
private final String contentEncoding;
private final String contentDisposition;
private final String contentLanguage;
private final StorageClass storageClass;
private final Integer componentCount;
private final boolean isDirectory;
private final CustomerEncryption customerEncryption;
Expand Down Expand Up @@ -242,6 +244,11 @@ public abstract static class Builder {

abstract Builder setMediaLink(String mediaLink);

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

/**
* Sets the blob's user provided metadata.
*/
Expand Down Expand Up @@ -290,6 +297,7 @@ static final class BuilderImpl extends Builder {
private Long createTime;
private Boolean isDirectory;
private CustomerEncryption customerEncryption;
private StorageClass storageClass;

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

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

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

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

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

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

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

Map<String, String> pbMetadata = metadata;
if (metadata != null && !Data.isNull(metadata)) {
pbMetadata = Maps.newHashMapWithExpectedSize(metadata.size());
Expand Down Expand Up @@ -909,6 +936,9 @@ public Acl apply(ObjectAccessControl objectAccessControl) {
builder.setCustomerEncryption(
CustomerEncryption.fromPb(storageObject.getCustomerEncryption()));
}
if (storageObject.getStorageClass() != null) {
builder.setStorageClass(StorageClass.valueOf(storageObject.getStorageClass()));
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public Builder setDeleteRules(Iterable<? extends DeleteRule> rules) {
}

@Override
public Builder setStorageClass(String storageClass) {
public Builder setStorageClass(StorageClass storageClass) {
infoBuilder.setStorageClass(storageClass);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public com.google.api.services.storage.model.Bucket apply(BucketInfo bucketInfo)
private final List<Acl> acl;
private final List<Acl> defaultAcl;
private final String location;
private final String storageClass;
private final StorageClass storageClass;

/**
* Base class for bucket's delete rules. Allows to configure automatic deletion of blobs and blobs
Expand Down Expand Up @@ -361,7 +361,7 @@ public abstract static class Builder {
* determines the SLA and the cost of storage. A list of supported values is available
* <a href="https://cloud.google.com/storage/docs/storage-classes">here</a>.
*/
public abstract Builder setStorageClass(String storageClass);
public abstract Builder setStorageClass(StorageClass storageClass);

/**
* Sets the bucket's location. Data for blobs in the bucket resides in physical storage within
Expand Down Expand Up @@ -419,7 +419,7 @@ static final class BuilderImpl extends Builder {
private String indexPage;
private String notFoundPage;
private List<DeleteRule> deleteRules;
private String storageClass;
private StorageClass storageClass;
private String location;
private String etag;
private Long createTime;
Expand Down Expand Up @@ -500,7 +500,7 @@ public Builder setDeleteRules(Iterable<? extends DeleteRule> rules) {
}

@Override
public Builder setStorageClass(String storageClass) {
public Builder setStorageClass(StorageClass storageClass) {
this.storageClass = storageClass;
return this;
}
Expand Down Expand Up @@ -671,7 +671,7 @@ public String getLocation() {
*
* @see <a href="https://cloud.google.com/storage/docs/storage-classes">Storage Classes</a>
*/
public String getStorageClass() {
public StorageClass getStorageClass() {
return storageClass;
}

Expand Down Expand Up @@ -748,7 +748,7 @@ com.google.api.services.storage.model.Bucket toPb() {
bucketPb.setLocation(location);
}
if (storageClass != null) {
bucketPb.setStorageClass(storageClass);
bucketPb.setStorageClass(storageClass.toString());
}
if (cors != null) {
bucketPb.setCors(transform(cors, Cors.TO_PB_FUNCTION));
Expand Down Expand Up @@ -830,7 +830,7 @@ static BucketInfo fromPb(com.google.api.services.storage.model.Bucket bucketPb)
builder.setLocation(bucketPb.getLocation());
}
if (bucketPb.getStorageClass() != null) {
builder.setStorageClass(bucketPb.getStorageClass());
builder.setStorageClass(StorageClass.valueOf(bucketPb.getStorageClass()));
}
if (bucketPb.getCors() != null) {
builder.setCors(transform(bucketPb.getCors(), Cors.FROM_PB_FUNCTION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ public static Builder newBuilder() {
* String bucketName = "my_unique_bucket";
* Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName)
* // See here for possible values: http://g.co/cloud/storage/docs/storage-classes
* .setStorageClass("COLDLINE")
* .setStorageClass(StorageClass.COLDLINE)
* // Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr
* .setLocation("asia")
* .build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.storage;

/**
* Enums for the storage classes.
* See https://cloud.google.com/storage/docs/storage-classes for details.
*/
public enum StorageClass {

/**
* Regional storage class.
*/
REGIONAL,

/**
* Multi-regional storage class.
*/
MULTI_REGIONAL,

/**
* Nearline storage class.
*/
NEARLINE,

/**
* Coldline storage class.
*/
COLDLINE,

/**
* Standard storage class.
*/
STANDARD
}
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 StorageClass STORAGE_CLASS = StorageClass.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 @@ -149,6 +152,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 @@ -203,6 +207,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 @@ -249,6 +254,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 @@ -57,7 +57,7 @@ public class BucketInfoTest {
private static final String INDEX_PAGE = "index.html";
private static final String NOT_FOUND_PAGE = "error.html";
private static final String LOCATION = "ASIA";
private static final String STORAGE_CLASS = "STANDARD";
private static final StorageClass STORAGE_CLASS = StorageClass.STANDARD;
private static final Boolean VERSIONING_ENABLED = true;
private static final BucketInfo BUCKET_INFO = BucketInfo.newBuilder("b")
.setAcl(ACL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class BucketTest {
private static final String INDEX_PAGE = "index.html";
private static final String NOT_FOUND_PAGE = "error.html";
private static final String LOCATION = "ASIA";
private static final String STORAGE_CLASS = "STANDARD";
private static final StorageClass STORAGE_CLASS = StorageClass.STANDARD;
private static final Boolean VERSIONING_ENABLED = true;
private static final BucketInfo FULL_BUCKET_INFO = BucketInfo.newBuilder("b")
.setAcl(ACLS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.cloud.storage.Storage.BucketField;
import com.google.cloud.storage.StorageBatch;
import com.google.cloud.storage.StorageBatchResult;
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -789,6 +790,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(StorageClass.STANDARD).build();
Blob remoteSourceBlob = storage.create(sourceInfo, BLOB_BYTE_CONTENT);
assertNotNull(remoteSourceBlob);
assertEquals(StorageClass.STANDARD, remoteSourceBlob.getStorageClass());

String targetBlobName = "test-copy-blob-update-storage-class-target";
BlobInfo targetInfo = BlobInfo
.newBuilder(BUCKET, targetBlobName).setStorageClass(StorageClass.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(StorageClass.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 339f172

Please sign in to comment.