/** Example of getting information on a blob. */ // [TARGET get(BlobId)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob getBlobFromId(String bucketName, String blobName) { // [START getBlobFromId] BlobId blobId = BlobId.of(bucketName, blobName); Blob blob = storage.get(blobId); // [END getBlobFromId] return blob; }
@Test public void testReloadNull() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BLOB_INFO.getBlobId(), new Storage.BlobGetOption[0])).andReturn(null); replay(storage); initializeBlob(); Blob reloadedBlob = blob.reload(); assertNull(reloadedBlob); }
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(); }
@Test public void testExists_False() throws Exception { Storage.BlobGetOption[] expectedOptions = {Storage.BlobGetOption.fields()}; expect(storage.getOptions()).andReturn(null); expect(storage.get(BLOB_INFO.getBlobId(), expectedOptions)).andReturn(null); replay(storage); initializeBlob(); assertFalse(blob.exists()); }
@Provides @Singleton Bucket getBucket(@ProjectId String projectId) { Storage storage = StorageOptions.getDefaultInstance().getService(); // Must match BUCKET_NAME for user data bucket in setup_gke_environment.sh String bucketId = "user-data-" + projectId; return storage.get(bucketId); }
@Test public void testReloadNull() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BUCKET_INFO.getName())).andReturn(null); replay(storage); initializeBucket(); assertNull(bucket.reload()); }
private void run(Storage storage, ServiceAccountCredentials cred, BlobInfo blobInfo) { Blob blob = storage.get(blobInfo.getBlobId()); System.out.printf( "Signed URL: %s%n", blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.signWith(cred))); }
@Test public void testExists_True() throws Exception { initializeExpectedBlob(1); Storage.BlobGetOption[] expectedOptions = {Storage.BlobGetOption.fields()}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(expectedBlob.getBlobId(), expectedOptions)).andReturn(expectedBlob); replay(storage); initializeBlob(); assertTrue(blob.exists()); }
private void run(Storage storage, BlobId blobId, Map<String, String> metadata) { Blob blob = storage.get(blobId); if (blob == null) { System.out.println("No such object"); return; } Blob updateBlob = blob.toBuilder().setMetadata(metadata).build().update(); System.out.printf("Updated %s%n", updateBlob); }
@Test public void testReloadWithOptions() throws Exception { initializeExpectedBlob(2); Blob expectedReloadedBlob = expectedBlob.toBuilder().setCacheControl("c").build(); Storage.BlobGetOption[] options = {Storage.BlobGetOption.metagenerationMatch(42L)}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BLOB_INFO.getBlobId(), options)).andReturn(expectedReloadedBlob); replay(storage); initializeBlob(); Blob updatedBlob = blob.reload(BlobSourceOption.metagenerationMatch()); assertEquals(expectedReloadedBlob, updatedBlob); }
@Override public CloudStorageFileAttributes readAttributes() throws IOException { if (path.seemsLikeADirectory() && path.getFileSystem().config().usePseudoDirectories()) { return new CloudStoragePseudoDirectoryAttributes(path); } BlobInfo blobInfo = storage.get(path.getBlobId()); if (blobInfo == null) { throw new NoSuchFileException(path.toUri().toString()); } return new CloudStorageObjectAttributes(blobInfo); }
@Test public void testGetBlobWithOptions() { EasyMock.expect( storageRpcMock.get(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_GET_OPTIONS)) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.get(BUCKET_NAME1, BLOB_NAME1, BLOB_GET_METAGENERATION, BLOB_GET_GENERATION); assertEquals(expectedBlob1, blob); }
@Test public void testGetBucket() { EasyMock.expect(storageRpcMock.get(BucketInfo.of(BUCKET_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.get(BUCKET_NAME1); assertEquals(expectedBucket1, bucket); }
@Test public void testGetBucketWithOptions() { EasyMock.expect(storageRpcMock.get(BucketInfo.of(BUCKET_NAME1).toPb(), BUCKET_GET_OPTIONS)) .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.get(BUCKET_NAME1, BUCKET_GET_METAGENERATION); assertEquals(expectedBucket1, bucket); }
@Test public void testGetBlob() { EasyMock.expect( storageRpcMock.get(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.get(BUCKET_NAME1, BLOB_NAME1); assertEquals(expectedBlob1, blob); }
@Test public void testGetBlobWithOptionsFromBlobId() { EasyMock.expect(storageRpcMock.get(BLOB_INFO1.getBlobId().toPb(), BLOB_GET_OPTIONS)) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.get( BLOB_INFO1.getBlobId(), BLOB_GET_METAGENERATION, BLOB_GET_GENERATION_FROM_BLOB_ID); assertEquals(expectedBlob1, blob); }
@Test public void testGetBucketAllSelectedFields() { Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.values())); assertEquals(BUCKET, remoteBucket.getName()); assertNotNull(remoteBucket.getCreateTime()); assertNotNull(remoteBucket.getSelfLink()); }
@Test public void testUpdateBucketLabel() { Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID)); assertNull(remoteBucket.getLabels()); remoteBucket = remoteBucket.toBuilder().setLabels(BUCKET_LABELS).build(); Bucket updatedBucket = storage.update(remoteBucket); assertEquals(BUCKET_LABELS, updatedBucket.getLabels()); }
@Test public void testGetBlobKmsKeyNameField() { String blobName = "test-get-selected-kms-key-name-field-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); assertNotNull(storage.create(blob, Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath))); Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields(BlobField.KMS_KEY_NAME)); assertEquals(blob.getBlobId(), remoteBlob.getBlobId()); assertTrue(remoteBlob.getKmsKeyName().startsWith(kmsKeyOneResourcePath)); assertNull(remoteBlob.getContentType()); }
@Test public void testGetBlobEmptySelectedFields() { String blobName = "test-get-empty-selected-fields-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); assertNotNull(storage.create(blob)); Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields()); assertEquals(blob.getBlobId(), remoteBlob.getBlobId()); assertNull(remoteBlob.getContentType()); }