本文整理汇总了Java中com.google.cloud.storage.Blob类的典型用法代码示例。如果您正苦于以下问题:Java Blob类的具体用法?Java Blob怎么用?Java Blob使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Blob类属于com.google.cloud.storage包,在下文中一共展示了Blob类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: load
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Override
public boolean load(BuildCacheKey buildCacheKey, BuildCacheEntryReader buildCacheEntryReader)
throws BuildCacheException {
try {
Blob blob = cloudStorage.get(cacheKeyToBlobId(buildCacheKey));
if (blob == null || !blob.exists()) {
return false;
}
try (InputStream is = Channels.newInputStream(blob.reader())) {
buildCacheEntryReader.readFrom(is);
}
} catch (Exception e) {
logger.warn(
"Exception when trying to read from cloud storage, this usually means gcloud "
+ "auth application-default login has not been called. Builds may be slower.",
e);
return false;
}
return true;
}
示例2: testWritableOutputStreamWithAutoCreateOnNullBlob
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Test
public void testWritableOutputStreamWithAutoCreateOnNullBlob() throws Exception {
String location = "gs://test-spring/test";
Storage storage = mock(Storage.class);
BlobId blobId = BlobId.of("test-spring", "test");
when(storage.get(blobId)).thenReturn(null);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
WriteChannel writeChannel = mock(WriteChannel.class);
Blob blob = mock(Blob.class);
when(blob.writer()).thenReturn(writeChannel);
when(storage.create(blobInfo)).thenReturn(blob);
GoogleStorageResourceObject resource = new GoogleStorageResourceObject(storage, location);
GoogleStorageResourceObject spyResource = spy(resource);
doReturn(this.bucketResource).when(spyResource).getBucket();
OutputStream os = spyResource.getOutputStream();
Assert.assertNotNull(os);
}
示例3: get
import com.google.cloud.storage.Blob; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
public void get(String s, File file)
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
{
if (LOG.isTraceEnabled()) {
LOG.trace(String.format("Fetching [%s] to [%s]", s, file));
}
try {
final Blob blob = getStorage().get(toBlobID(s));
if (blob == null) {
throw new ResourceDoesNotExistException(String.format("Not found [%s]", s));
}
get(blob, file);
}
catch (StorageException se) {
throw translate(s, se);
}
catch (IOException e) {
throw new TransferFailedException(String.format("Failed to write to file [%s]", file), e);
}
}
示例4: getIfNewer
import com.google.cloud.storage.Blob; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
public boolean getIfNewer(String s, File file, long ts)
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
{
try {
final Blob blob = getStorage().get(toBlobID(s));
if (blob.getCreateTime() <= ts) {
if (LOG.isTraceEnabled()) {
LOG.trace(String.format("Not fetching, [%s] at [%s] is older than [%s]", s, blob.getCreateTime(), ts));
}
return false;
}
get(blob, file);
return true;
}
catch (StorageException se) {
throw translate(s, se);
}
catch (IOException ioe) {
throw new TransferFailedException(String.format("Unable to write [%s]", file), ioe);
}
}
示例5: testGetBlob
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Test
public void testGetBlob() throws Exception
{
final long size = 100;
final Blob blob = createStrictMock(Blob.class);
final ReadChannel readChannel = createStrictMock(ReadChannel.class);
expect(blob.getName()).andReturn(connectionPOJO.baseId.getName() + "/foo").once();
expect(blob.getSize()).andReturn(size).once();
expect(blob.reader()).andReturn(readChannel).once();
expect(readChannel.read(anyObject()))
.andReturn(0).times(10)
.andReturn(1).times((int) size);
readChannel.close();
expectLastCall().once();
replay(readChannel, blob);
final File outFile = temporaryFolder.newFile();
outFile.delete();
assertFalse(outFile.exists());
gsWagon.swapAndCloseConnection(connectionPOJO);
gsWagon.get(blob, outFile);
assertTrue(outFile.exists());
verify(readChannel, blob);
}
示例6: testGetBlobTransferFailedException
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Test
public void testGetBlobTransferFailedException() throws Exception
{
expectedException.expect(TransferFailedException.class);
final long size = 100;
final Blob blob = createStrictMock(Blob.class);
final ReadChannel readChannel = createStrictMock(ReadChannel.class);
expect(blob.getName()).andReturn(connectionPOJO.baseId.getName() + "/foo").once();
expect(blob.getSize()).andReturn(size).once();
expect(blob.reader()).andReturn(readChannel).once();
expect(readChannel.read(anyObject()))
.andReturn(-1).once();
readChannel.close();
expectLastCall().once();
replay(readChannel, blob);
final File outFile = temporaryFolder.newFile();
outFile.delete();
assertFalse(outFile.exists());
gsWagon.swapAndCloseConnection(connectionPOJO);
gsWagon.get(blob, outFile);
}
示例7: testGetBlobIOException
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Test
public void testGetBlobIOException() throws Exception
{
expectedException.expect(IOException.class);
final long size = 100;
final Blob blob = createStrictMock(Blob.class);
final ReadChannel readChannel = createStrictMock(ReadChannel.class);
expect(blob.getName()).andReturn(connectionPOJO.baseId.getName() + "/foo").once();
expect(blob.getSize()).andReturn(size).once();
expect(blob.reader()).andReturn(readChannel).once();
expect(readChannel.read(anyObject()))
.andThrow(new IOException("test")).once();
readChannel.close();
expectLastCall().once();
replay(readChannel, blob);
final File outFile = temporaryFolder.newFile();
outFile.delete();
assertFalse(outFile.exists());
gsWagon.swapAndCloseConnection(connectionPOJO);
gsWagon.get(blob, outFile);
}
示例8: store
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Override
public void store(BuildCacheKey buildCacheKey, BuildCacheEntryWriter buildCacheEntryWriter)
throws BuildCacheException {
Blob blob = cloudStorage.get(cacheKeyToBlobId(buildCacheKey));
if (blob == null || !blob.exists()) {
blob =
cloudStorage.create(
BlobInfo.newBuilder(cacheKeyToBlobId(buildCacheKey))
.setContentType(BUILD_CACHE_CONTENT_TYPE)
.build());
}
try (OutputStream os = Channels.newOutputStream(blob.writer())) {
buildCacheEntryWriter.writeTo(os);
} catch (IOException e) {
throw new UncheckedIOException("Error writing file from cloud storage.", e);
}
}
示例9: testWritableOutputStreamWithAutoCreateOnNonExistantBlob
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Test
public void testWritableOutputStreamWithAutoCreateOnNonExistantBlob()
throws Exception {
String location = "gs://test-spring/test";
Storage storage = mock(Storage.class);
BlobId blobId = BlobId.of("test-spring", "test");
Blob nonExistantBlob = mock(Blob.class);
when(nonExistantBlob.exists()).thenReturn(false);
when(storage.get(blobId)).thenReturn(nonExistantBlob);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
WriteChannel writeChannel = mock(WriteChannel.class);
Blob blob = mock(Blob.class);
when(blob.writer()).thenReturn(writeChannel);
when(storage.create(blobInfo)).thenReturn(blob);
GoogleStorageResourceObject resource = new GoogleStorageResourceObject(storage, location);
GoogleStorageResourceObject spyResource = spy(resource);
doReturn(this.bucketResource).when(spyResource).getBucket();
OutputStream os = spyResource.getOutputStream();
Assert.assertNotNull(os);
}
示例10: mockStorage
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Bean
public static Storage mockStorage() throws Exception {
Storage storage = mock(Storage.class);
BlobId validBlob = BlobId.of("test-spring", "images/spring.png");
Bucket mockedBucket = mock(Bucket.class);
Blob mockedBlob = mock(Blob.class);
WriteChannel writeChannel = mock(WriteChannel.class);
when(mockedBlob.exists()).thenReturn(true);
when(mockedBucket.exists()).thenReturn(true);
when(mockedBlob.getSize()).thenReturn(4096L);
when(storage.get(eq(validBlob))).thenReturn(mockedBlob);
when(storage.get("test-spring")).thenReturn(mockedBucket);
when(mockedBucket.getName()).thenReturn("test-spring");
when(mockedBlob.writer()).thenReturn(writeChannel);
return storage;
}
示例11: gcs
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Bean
public Storage gcs() {
Storage gcsMock = mock(Storage.class);
Blob blob1 = mock(Blob.class);
Blob blob2 = mock(Blob.class);
willAnswer(invocation -> "legend of heroes").given(blob1).getName();
willAnswer(invocation -> "trails in the sky").given(blob2).getName();
willAnswer(invocation -> "estelle".getBytes()).given(gcsMock)
.readAllBytes(eq("test-bucket"), eq("legend of heroes"));
willAnswer(invocation -> "joshua".getBytes()).given(gcsMock)
.readAllBytes(eq("test-bucket"), eq("trails in the sky"));
willAnswer(invocation -> new PageImpl<>(null, null,
Stream.of(blob1, blob2)
.collect(Collectors.toList())))
.given(gcsMock).list("test-bucket");
return gcsMock;
}
示例12: storeBlob
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@Override
public Boolean storeBlob(CallingContext context, RaptureURI blobUri, Boolean append, InputStream content) {
// Google cloud storage objects are immutable.
// Not sure what happens to the old blob, if it's deleted.
// But we don't want to delete the old until after the new is written
String key = blobUri.getDocPath();
Blob blob = bucket.get(key);
if (blob != null) {
if (append) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
baos.write(blob.getContent());
IOUtils.copy(content, baos);
bucket.create(key, baos.toByteArray());
return true;
} catch (IOException e) {
logger.error(e.getMessage());
return false;
}
}
System.out.println("Overwriting bucket " + bucketName + " Key " + key);
}
Blob x = bucket.create(key, content);
System.out.println("Wrote to bucket " + bucketName + " with key " + key);
return true;
}
示例13: getSubKeys
import com.google.cloud.storage.Blob; //导入依赖的package包/类
/**
* Note that this only gets immediate children - it's not recursive. Should it be?
*/
@Override
public List<RaptureFolderInfo> getSubKeys(String prefix) {
List<RaptureFolderInfo> list = new ArrayList<>();
Map<String, RaptureFolderInfo> map = new HashMap<>();
for (Blob key : blobStore.listBlobs(prefix)) {
String jordan = key.getName();
int l = prefix.length();
if (jordan.startsWith(prefix)) {
while (jordan.charAt(l) == '/') {
l++;
}
String keegan = jordan.substring(l);
int idx = keegan.indexOf('/');
if (idx > 0) {
list.add(new RaptureFolderInfo(keegan.substring(0, idx), true));
} else {
list.add(new RaptureFolderInfo(keegan, false));
}
}
}
list.addAll(map.values());
return list;
}
示例14: blobExistsInner
import com.google.cloud.storage.Blob; //导入依赖的package包/类
private static Blob blobExistsInner(URI uri) {
final String bucketName = uri.getAuthority();
final String prefix = uri.getPath().startsWith("/") ? uri.getPath().substring(1) : uri.getPath();
final Page<Blob> list = STORAGE.list(bucketName, prefix(prefix));
final Iterator<Blob> blobIterator = list.iterateAll();
Blob blob = null;
while (blobIterator.hasNext()) {
blob = blobIterator.next();
if (blob.getName().equals(prefix)) {
break;
}
}
if (blob != null) {
return blob;
} else {
throw new NotReady();
}
}
示例15: tearDownClass
import com.google.cloud.storage.Blob; //导入依赖的package包/类
@AfterClass
public static void tearDownClass() {
// Clear the datastore
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
Batch batch = datastore.newBatch();
StructuredQuery<Key> query = Query.newKeyQueryBuilder()
.setKind("Book3").build();
for (QueryResults<Key> keys = datastore.run(query); keys.hasNext(); ) {
batch.delete(keys.next());
}
batch.submit();
// Delete any objects in the bucket
Storage storage = StorageOptions.getDefaultInstance().getService();
Page<Blob> blobs = storage.list(System.getProperty("bookshelf.bucket"));
List<BlobId> blobIds = new ArrayList<BlobId>();
for (Blob b : blobs.iterateAll()) {
blobIds.add(b.getBlobId());
}
storage.delete(blobIds);
service.stop();
}