Skip to content
Closed
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,21 @@ public void reader() throws IOException {
// [END reader]
}

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

/**
* 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,20 @@ public Bucket createBucket(String bucketName) {
return bucket;
}

/**
* Example of creating a bucket with storage class and location.
*/
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(subcontent, "ello, W".getBytes(UTF_8));

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