Skip to content

Commit 41a71b3

Browse files
author
Jerjou Cheng
committed
Add storage examples.
1 parent e4fb76b commit 41a71b3

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java

+15
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,21 @@ public void reader() throws IOException {
189189
// [END reader]
190190
}
191191

192+
/**
193+
* Example of reading just a portion of the blob's content.
194+
*/
195+
// [TARGET reader(BlobSourceOption...)]
196+
public byte[] readContentRange(int start, int end) throws IOException {
197+
// [START storageDownloadByteRange]
198+
try (ReadChannel reader = blob.reader()) {
199+
reader.seek(start);
200+
ByteBuffer bytes = ByteBuffer.allocate(end - start);
201+
reader.read(bytes);
202+
return bytes.array();
203+
}
204+
// [END storageDownloadByteRange]
205+
}
206+
192207
/**
193208
* Example of writing the blob's content through a writer.
194209
*/

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java

+14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ public Bucket createBucket(String bucketName) {
9090
return bucket;
9191
}
9292

93+
/**
94+
* Example of creating a bucket with storage class and location.
95+
*/
96+
public Bucket createBucketWithStorageClassAndLocation(String bucketName) {
97+
// [START createBucketWithStorageClassAndLocation]
98+
Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName)
99+
// See here for possible values: http://g.co/cloud/storage/docs/storage-classes
100+
.setStorageClass("COLDLINE")
101+
// Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr
102+
.setLocation("asia").build());
103+
// [END createBucketWithStorageClassAndLocation]
104+
return bucket;
105+
}
106+
93107
/**
94108
* Example of creating a blob with no content.
95109
*/

google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBlobSnippets.java

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public void testBlob() throws IOException {
123123
}
124124
assertFalse(blobSnippets.delete());
125125
blobSnippets = new BlobSnippets(storage.get(blob.getBucket(), blob.getName()));
126+
127+
byte[] subcontent = blobSnippets.readContentRange(1, 8);
128+
assertArrayEquals(subcontent, "ello, W".getBytes(UTF_8));
129+
126130
assertNull(blobSnippets.getAcl());
127131
assertNotNull(blobSnippets.createAcl());
128132
Acl updatedAcl = blobSnippets.updateAcl();

google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.io.InputStream;
4949
import java.net.URL;
5050
import java.net.URLConnection;
51+
import java.util.ArrayList;
5152
import java.util.HashSet;
5253
import java.util.Iterator;
5354
import java.util.List;
@@ -64,6 +65,7 @@ public class ITStorageSnippets {
6465

6566
private static Storage storage;
6667
private static StorageSnippets storageSnippets;
68+
private static List<String> bucketsToCleanUp;
6769

6870
@Rule
6971
public ExpectedException thrown = ExpectedException.none();
@@ -75,20 +77,35 @@ public class ITStorageSnippets {
7577
public static void beforeClass() {
7678
RemoteStorageHelper helper = RemoteStorageHelper.create();
7779
storage = helper.getOptions().getService();
80+
bucketsToCleanUp = new ArrayList<String>();
7881
storageSnippets = new StorageSnippets(storage);
7982
storageSnippets.createBucket(BUCKET);
83+
bucketsToCleanUp.add(BUCKET);
8084
}
8185

8286
@AfterClass
8387
public static void afterClass() throws ExecutionException, InterruptedException {
8488
if (storage != null) {
85-
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 5, TimeUnit.SECONDS);
86-
if (!wasDeleted && log.isLoggable(Level.WARNING)) {
87-
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET);
89+
for (String bucket : bucketsToCleanUp) {
90+
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
91+
if (!wasDeleted && log.isLoggable(Level.WARNING)) {
92+
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", bucket);
93+
}
8894
}
8995
}
9096
}
9197

98+
@Test
99+
public void testCreateBucketWithStorageClassAndLocation()
100+
throws ExecutionException, InterruptedException {
101+
String tempBucket = RemoteStorageHelper.generateBucketName();
102+
bucketsToCleanUp.add(tempBucket);
103+
104+
Bucket bucket = storageSnippets.createBucketWithStorageClassAndLocation(tempBucket);
105+
106+
assertNotNull(bucket);
107+
}
108+
92109
@Test
93110
public void testBlob() throws InterruptedException {
94111
String blobName = "directory/test-blob";

0 commit comments

Comments
 (0)