Skip to content

Commit 81278dc

Browse files
authored
Add more storage snippets (#1382)
1 parent c9e1630 commit 81278dc

File tree

6 files changed

+81
-3
lines changed

6 files changed

+81
-3
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ 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+
// [VARIABLE 1]
197+
// [VARIABLE 8]
198+
public byte[] readContentRange(int start, int end) throws IOException {
199+
// [START readContentRange]
200+
try (ReadChannel reader = blob.reader()) {
201+
reader.seek(start);
202+
ByteBuffer bytes = ByteBuffer.allocate(end - start);
203+
reader.read(bytes);
204+
return bytes.array();
205+
}
206+
// [END readContentRange]
207+
}
208+
192209
/**
193210
* Example of writing the blob's content through a writer.
194211
*/

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

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

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

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("ello, W".getBytes(UTF_8), subcontent);
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";

google-cloud-storage/src/main/java/com/google/cloud/storage/Blob.java

+12
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,18 @@ public CopyWriter copyTo(String targetBucket, String targetBlob, BlobSourceOptio
617617
* }
618618
* }</pre>
619619
*
620+
* <p>Example of reading just a portion of the blob's content.
621+
* <pre> {@code
622+
* int start = 1;
623+
* int end = 8;
624+
* try (ReadChannel reader = blob.reader()) {
625+
* reader.seek(start);
626+
* ByteBuffer bytes = ByteBuffer.allocate(end - start);
627+
* reader.read(bytes);
628+
* return bytes.array();
629+
* }
630+
* }</pre>
631+
*
620632
* @param options blob read options
621633
* @throws StorageException upon failure
622634
*/

google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java

+11
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,17 @@ public static Builder newBuilder() {
14961496
* Bucket bucket = storage.create(BucketInfo.of(bucketName));
14971497
* }</pre>
14981498
*
1499+
* <p>Example of creating a bucket with storage class and location.
1500+
* <pre> {@code
1501+
* String bucketName = "my_unique_bucket";
1502+
* Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName)
1503+
* // See here for possible values: http://g.co/cloud/storage/docs/storage-classes
1504+
* .setStorageClass("COLDLINE")
1505+
* // Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr
1506+
* .setLocation("asia")
1507+
* .build());
1508+
* }</pre>
1509+
*
14991510
* @return a complete bucket
15001511
* @throws StorageException upon failure
15011512
*/

0 commit comments

Comments
 (0)