Refine search
/** * Checks if this blob exists. * * <p>Example of checking if the blob exists. * * <pre>{@code * boolean exists = blob.exists(); * if (exists) { * // the blob exists * } else { * // the blob was not found * } * }</pre> * * @param options blob read options * @return true if this blob exists, false otherwise * @throws StorageException upon failure */ public boolean exists(BlobSourceOption... options) { int length = options.length; Storage.BlobGetOption[] getOptions = Arrays.copyOf(toGetOptions(this, options), length + 1); getOptions[length] = Storage.BlobGetOption.fields(); return storage.get(getBlobId(), getOptions) != null; }
@Test public void testGetBlobFail() { String blobName = "test-get-blob-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); BlobId wrongGenerationBlob = BlobId.of(BUCKET, blobName); try { storage.get(wrongGenerationBlob, Storage.BlobGetOption.generationMatch(-1)); fail("StorageException was expected"); } catch (StorageException ex) { // expected } }
@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); }
@Before public void before() throws IOException { when(metadata.getSize()).thenReturn(42L); when(metadata.getGeneration()).thenReturn(2L); when(gcsStorage.get( file, Storage.BlobGetOption.fields(Storage.BlobField.GENERATION, Storage.BlobField.SIZE))) .thenReturn(metadata); when(gcsStorage.reader(file, Storage.BlobSourceOption.generationMatch(2L))) .thenReturn(gcsChannel); when(gcsChannel.isOpen()).thenReturn(true); chan = CloudStorageReadChannel.create( gcsStorage, file, 0, 1, CloudStorageConfiguration.DEFAULT, ""); verify(gcsStorage) .get( eq(file), eq(Storage.BlobGetOption.fields(Storage.BlobField.GENERATION, Storage.BlobField.SIZE))); verify(gcsStorage).reader(eq(file), eq(Storage.BlobSourceOption.generationMatch(2L))); }
BlobInfo blobInfo; if (isNullOrEmpty(userProject)) { blobInfo = storage.get(fromPath.getBlobId()); } else { blobInfo = storage.get(fromPath.getBlobId(), BlobGetOption.userProject(userProject)); BlobSourceOption.userProject(toPath.getFileSystem().config().userProject())); CopyWriter copyWriter = storage.copy(copyReqBuilder.build()); copyWriter.getResult(); break;
String blobName = "test-batch-request-blob-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); BlobInfo updatedBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build(); StorageBatch batch = storage.batch(); StorageBatchResult<Blob> updateResult = batch.update(updatedBlob, Storage.BlobTargetOption.generationMatch()); StorageBatchResult<Boolean> deleteResult2 = batch.delete(BlobId.of(BUCKET, blobName, -1L)); StorageBatchResult<Blob> getResult1 = batch.get(BUCKET, blobName, Storage.BlobGetOption.generationMatch(-1L)); StorageBatchResult<Blob> getResult2 = batch.get(BlobId.of(BUCKET, blobName, -1L)); batch.submit();
/** * Returns an option for blob's data generation mismatch. If this option is used the request * will fail if blob's generation matches. The generation value to compare with the actual * blob's generation is taken from a source {@link BlobId} object. When this option is passed to * a {@link Storage} method and {@link BlobId#getGeneration()} is {@code null} or no {@link * BlobId} is provided an exception is thrown. */ public static BlobGetOption generationNotMatch() { return new BlobGetOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, (Long) null); }
/** * Returns an option for blob's data generation mismatch. If this option is used the request * will fail if blob's generation matches the provided value. */ public static BlobGetOption generationNotMatch(long generation) { return new BlobGetOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, generation); }
/** * Returns an option for blob's data generation match. If this option is used the request will * fail if blob's generation does not match. The generation value to compare with the actual * blob's generation is taken from a source {@link BlobId} object. When this option is passed to * a {@link Storage} method and {@link BlobId#getGeneration()} is {@code null} or no {@link * BlobId} is provided an exception is thrown. */ public static BlobGetOption generationMatch() { return new BlobGetOption(StorageRpc.Option.IF_GENERATION_MATCH, (Long) null); }
/** * Returns an option for blob's metageneration mismatch. If this option is used the request will * fail if blob's metageneration matches the provided value. */ public static BlobGetOption metagenerationNotMatch(long metageneration) { return new BlobGetOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH, metageneration); }
/** * 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; }
/** * Returns an option for blob's data generation match. If this option is used the request will * fail if blob's generation does not match the provided value. */ public static BlobGetOption generationMatch(long generation) { return new BlobGetOption(StorageRpc.Option.IF_GENERATION_MATCH, generation); }
/** * Returns an option for blob's metageneration match. If this option is used the request will * fail if blob's metageneration does not match the provided value. */ public static BlobGetOption metagenerationMatch(long metageneration) { return new BlobGetOption(StorageRpc.Option.IF_METAGENERATION_MATCH, metageneration); }
/** * Returns an option for blob's billing user project. This option is only used by the buckets * with 'requester_pays' flag. */ public static BlobGetOption userProject(String userProject) { return new BlobGetOption(StorageRpc.Option.USER_PROJECT, userProject); } }
/** * Returns an option to specify the blob's fields to be returned by the RPC call. If this option * is not provided all blob's fields are returned. {@code BlobGetOption.fields}) can be used to * specify only the fields of interest. Blob name and bucket are always returned, even if not * specified. */ public static BlobGetOption fields(BlobField... fields) { return new BlobGetOption( StorageRpc.Option.FIELDS, Helper.selector(BlobField.REQUIRED_FIELDS, fields)); }
private Storage.BlobGetOption toGetOption(BlobInfo blobInfo) { switch (getRpcOption()) { case IF_GENERATION_MATCH: return Storage.BlobGetOption.generationMatch(blobInfo.getGeneration()); case IF_GENERATION_NOT_MATCH: return Storage.BlobGetOption.generationNotMatch(blobInfo.getGeneration()); case IF_METAGENERATION_MATCH: return Storage.BlobGetOption.metagenerationMatch(blobInfo.getMetageneration()); case IF_METAGENERATION_NOT_MATCH: return Storage.BlobGetOption.metagenerationNotMatch(blobInfo.getMetageneration()); case USER_PROJECT: return Storage.BlobGetOption.userProject((String) getValue()); default: throw new AssertionError("Unexpected enum value"); } }
/** Returns the requested storage object or {@code null} if not found. */ @Override public StorageObject get(StorageObject object, Map<Option, ?> options) throws StorageException { // we allow the "ID" option because we need to, but then we give a whole answer anyways // because the caller won't mind the extra fields. if (throwIfOption && !options.isEmpty() && options.size() > 1 && options.keySet().toArray()[0] != Storage.BlobGetOption.fields(Storage.BlobField.ID)) { throw new UnsupportedOperationException(); } String key = fullname(object); if (metadata.containsKey(key)) { StorageObject ret = metadata.get(key); if (contents.containsKey(key)) { ret.setSize(BigInteger.valueOf(contents.get(key).length)); } ret.setId(key); return ret; } return null; }
/** * Returns an option for blob's data generation mismatch. If this option is used the request * will fail if blob's generation matches. The generation value to compare with the actual * blob's generation is taken from a source {@link BlobId} object. When this option is passed to * a {@link Storage} method and {@link BlobId#getGeneration()} is {@code null} or no {@link * BlobId} is provided an exception is thrown. */ public static BlobGetOption generationNotMatch() { return new BlobGetOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, (Long) null); }
/** * Returns an option for blob's data generation match. If this option is used the request will * fail if blob's generation does not match the provided value. */ public static BlobGetOption generationMatch(long generation) { return new BlobGetOption(StorageRpc.Option.IF_GENERATION_MATCH, generation); }
/** * Returns an option for blob's data generation mismatch. If this option is used the request * will fail if blob's generation matches the provided value. */ public static BlobGetOption generationNotMatch(long generation) { return new BlobGetOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, generation); }