/** Example of getting some blobs in the bucket, using a batch request. */ // [TARGET get(String, String, String...)] // [VARIABLE "my_blob_name1"] // [VARIABLE "my_blob_name2"] public List<Blob> getBlobFromStrings(String blobName1, String blobName2) { // [START getBlobFromStrings] List<Blob> blobs = bucket.get(blobName1, blobName2); for (Blob blob : blobs) { if (blob == null) { // the blob was not found } } // [END getBlobFromStrings] return blobs; }
@Test public void testGet() throws Exception { initializeExpectedBucket(5); Blob expectedBlob = new Blob( serviceMockReturnsOptions, new BlobInfo.BuilderImpl(BlobInfo.newBuilder("b", "n").build())); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BlobId.of(expectedBucket.getName(), "n"), new Storage.BlobGetOption[0])) .andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.get("n"); assertEquals(expectedBlob, blob); }
/** Example of getting some blobs in the bucket, using a batch request. */ // [TARGET get(Iterable)] // [VARIABLE "my_blob_name1"] // [VARIABLE "my_blob_name2"] public List<Blob> getBlobFromStringIterable(String blobName1, String blobName2) { // [START getBlobFromStringIterable] List<String> blobNames = new LinkedList<>(); blobNames.add(blobName1); blobNames.add(blobName2); List<Blob> blobs = bucket.get(blobNames); for (Blob blob : blobs) { if (blob == null) { // the blob was not found } } // [END getBlobFromStringIterable] return blobs; }
/** * Example of getting a blob in the bucket, only if its metageneration matches a value, otherwise * a {@link StorageException} is thrown. */ // [TARGET get(String, BlobGetOption...)] // [VARIABLE "my_blob_name"] // [VARIABLE 42] public Blob getBlob(String blobName, long generation) { // [START getBlob] Blob blob = bucket.get(blobName, BlobGetOption.generationMatch(generation)); // [END getBlob] return blob; }
private byte[] getRawBytes(String blobName) { Bucket bucket = storage.get(bucketName); Preconditions.checkNotNull(bucket, "Bucket [%s] not found", bucketName); Blob blob = bucket.get(blobName); Preconditions.checkNotNull(blob, "blob [%s] not found", blobName); return blob.getContent(); }
InputStream getStream(UUID jobId, String keyName) { String blobName = getDataKeyName(jobId, keyName); Blob blob = bucket.get(blobName); ReadChannel channel = blob.reader(); return Channels.newInputStream(channel); }
@Test public void testGetAllIterable() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); List<BlobId> blobIds = Lists.transform( blobResults, new Function<Blob, BlobId>() { @Override public BlobId apply(Blob blob) { return blob.getBlobId(); } }); expect(storage.get(blobIds)).andReturn(blobResults); replay(storage); initializeBucket(); assertEquals(blobResults, bucket.get(ImmutableList.of("n1", "n2", "n3"))); }
@Test public void testGetAllArray() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); List<BlobId> blobIds = Lists.transform( blobResults, new Function<Blob, BlobId>() { @Override public BlobId apply(Blob blob) { return blob.getBlobId(); } }); expect(storage.get(blobIds)).andReturn(blobResults); replay(storage); initializeBucket(); assertEquals(blobResults, bucket.get("n1", "n2", "n3")); }