Skip to content

Add more storage snippets #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ public void reader() throws IOException {
// [END reader]
}

/**
* Example of reading just a portion of the blob's content.
*/
// [TARGET reader(BlobSourceOption...)]
// [VARIABLE 1]
// [VARIABLE 8]
public byte[] readContentRange(int start, int end) throws IOException {
// [START readContentRange]
try (ReadChannel reader = blob.reader()) {
reader.seek(start);
ByteBuffer bytes = ByteBuffer.allocate(end - start);
reader.read(bytes);
return bytes.array();
}
// [END readContentRange]
}

/**
* Example of writing the blob's content through a writer.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ public Bucket createBucket(String bucketName) {
return bucket;
}

/**
* Example of creating a bucket with storage class and location.
*/
// [TARGET create(BucketInfo, BucketTargetOption...)]
// [VARIABLE "my_unique_bucket"]
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")
// Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr
.setLocation("asia")
.build());
// [END createBucketWithStorageClassAndLocation]
return bucket;
}

/**
* Example of creating a blob with no content.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public void testBlob() throws IOException {
}
assertFalse(blobSnippets.delete());
blobSnippets = new BlobSnippets(storage.get(blob.getBucket(), blob.getName()));

byte[] subcontent = blobSnippets.readContentRange(1, 8);
assertArrayEquals("ello, W".getBytes(UTF_8), subcontent);

assertNull(blobSnippets.getAcl());
assertNotNull(blobSnippets.createAcl());
Acl updatedAcl = blobSnippets.updateAcl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand All @@ -64,6 +65,7 @@ public class ITStorageSnippets {

private static Storage storage;
private static StorageSnippets storageSnippets;
private static List<String> bucketsToCleanUp;

@Rule
public ExpectedException thrown = ExpectedException.none();
Expand All @@ -75,20 +77,35 @@ public class ITStorageSnippets {
public static void beforeClass() {
RemoteStorageHelper helper = RemoteStorageHelper.create();
storage = helper.getOptions().getService();
bucketsToCleanUp = new ArrayList<String>();
storageSnippets = new StorageSnippets(storage);
storageSnippets.createBucket(BUCKET);
bucketsToCleanUp.add(BUCKET);
}

@AfterClass
public static void afterClass() throws ExecutionException, InterruptedException {
if (storage != null) {
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 5, TimeUnit.SECONDS);
if (!wasDeleted && log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET);
for (String bucket : bucketsToCleanUp) {
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
if (!wasDeleted && log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", bucket);
}
}
}
}

@Test
public void testCreateBucketWithStorageClassAndLocation()
throws ExecutionException, InterruptedException {
String tempBucket = RemoteStorageHelper.generateBucketName();
bucketsToCleanUp.add(tempBucket);

Bucket bucket = storageSnippets.createBucketWithStorageClassAndLocation(tempBucket);

assertNotNull(bucket);
}

@Test
public void testBlob() throws InterruptedException {
String blobName = "directory/test-blob";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,18 @@ public CopyWriter copyTo(String targetBucket, String targetBlob, BlobSourceOptio
* }
* }</pre>
*
* <p>Example of reading just a portion of the blob's content.
* <pre> {@code
* int start = 1;
* int end = 8;
* try (ReadChannel reader = blob.reader()) {
* reader.seek(start);
* ByteBuffer bytes = ByteBuffer.allocate(end - start);
* reader.read(bytes);
* return bytes.array();
* }
* }</pre>
*
* @param options blob read options
* @throws StorageException upon failure
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,17 @@ public static Builder newBuilder() {
* Bucket bucket = storage.create(BucketInfo.of(bucketName));
* }</pre>
*
* <p>Example of creating a bucket with storage class and location.
* <pre> {@code
* 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")
* // Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr
* .setLocation("asia")
* .build());
* }</pre>
*
* @return a complete bucket
* @throws StorageException upon failure
*/
Expand Down