Search in sources :

Example 41 with Blob

use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testUpdateBlobUnsetMetadata.

@Test
public void testUpdateBlobUnsetMetadata() {
    String blobName = "test-update-blob-unset-metadata";
    ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a", "k2", "b");
    Map<String, String> newMetadata = new HashMap<>();
    newMetadata.put("k1", "a");
    newMetadata.put("k2", null);
    ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a");
    BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).setMetadata(metadata).build();
    Blob remoteBlob = storage.create(blob);
    assertNotNull(remoteBlob);
    Blob updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update();
    assertNotNull(updatedBlob);
    assertEquals(blob.getName(), updatedBlob.getName());
    assertEquals(blob.getBucket(), updatedBlob.getBucket());
    assertEquals(expectedMetadata, updatedBlob.getMetadata());
    assertTrue(updatedBlob.delete());
}
Also used : Blob(com.google.cloud.storage.Blob) HashMap(java.util.HashMap) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 42 with Blob

use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.

the class ITStorageTest method testUpdateBlobs.

@Test
public void testUpdateBlobs() {
    String sourceBlobName1 = "test-update-blobs-1";
    String sourceBlobName2 = "test-update-blobs-2";
    BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
    BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
    Blob remoteBlob1 = storage.create(sourceBlob1);
    Blob remoteBlob2 = storage.create(sourceBlob2);
    assertNotNull(remoteBlob1);
    assertNotNull(remoteBlob2);
    List<Blob> updatedBlobs = storage.update(remoteBlob1.toBuilder().setContentType(CONTENT_TYPE).build(), remoteBlob2.toBuilder().setContentType(CONTENT_TYPE).build());
    assertEquals(sourceBlob1.getBucket(), updatedBlobs.get(0).getBucket());
    assertEquals(sourceBlob1.getName(), updatedBlobs.get(0).getName());
    assertEquals(CONTENT_TYPE, updatedBlobs.get(0).getContentType());
    assertEquals(sourceBlob2.getBucket(), updatedBlobs.get(1).getBucket());
    assertEquals(sourceBlob2.getName(), updatedBlobs.get(1).getName());
    assertEquals(CONTENT_TYPE, updatedBlobs.get(1).getContentType());
    assertTrue(updatedBlobs.get(0).delete());
    assertTrue(updatedBlobs.get(1).delete());
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test)

Example 43 with Blob

use of com.google.cloud.storage.Blob in project protoman by spotify.

the class GcsContentAddressedBlobStorage method get.

@Override
public Optional<byte[]> get(final HashCode contentHash) {
    final String blobName = blobName(contentHash);
    final Blob blob = storage.get(BlobId.of(bucket, blobName));
    return Optional.ofNullable(blob.getContent());
}
Also used : Blob(com.google.cloud.storage.Blob)

Example 44 with Blob

use of com.google.cloud.storage.Blob in project protoman by spotify.

the class GcsGenerationalFile method replace.

public long replace(byte[] bytes) {
    Preconditions.checkState(generation != null, "File is not loaded.");
    try {
        final Blob blob = storage.create(BlobInfo.newBuilder(bucket, path, generation).build(), bytes, Storage.BlobTargetOption.generationMatch());
        generation = blob.getGeneration();
        return generation;
    } catch (StorageException ex) {
        // allow precondition failed, which means we already have a file
        if (ex.getCode() != GCS_PRECONDITION_FAILED_CODE) {
            throw ex;
        }
        throw new OptimisticLockingException("Index file has been modified.", ex);
    }
}
Also used : Blob(com.google.cloud.storage.Blob) StorageException(com.google.cloud.storage.StorageException)

Example 45 with Blob

use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.

the class BlobSnippets method copyToId.

/**
   * Example of copying the blob to a different bucket with a different name.
   */
// [TARGET copyTo(BlobId, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "copy_blob_name"]
public Blob copyToId(String bucketName, String blobName) {
    // [START copyToId]
    CopyWriter copyWriter = blob.copyTo(BlobId.of(bucketName, blobName));
    Blob copiedBlob = copyWriter.getResult();
    // [END copyToId]
    return copiedBlob;
}
Also used : Blob(com.google.cloud.storage.Blob) CopyWriter(com.google.cloud.storage.CopyWriter)

Aggregations

Blob (com.google.cloud.storage.Blob)80 BlobInfo (com.google.cloud.storage.BlobInfo)50 Test (org.junit.Test)50 BlobId (com.google.cloud.storage.BlobId)23 StorageException (com.google.cloud.storage.StorageException)16 Storage (com.google.cloud.storage.Storage)12 CopyWriter (com.google.cloud.storage.CopyWriter)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 StorageBatch (com.google.cloud.storage.StorageBatch)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 ReadChannel (com.google.cloud.ReadChannel)3 Acl (com.google.cloud.storage.Acl)3 Bucket (com.google.cloud.storage.Bucket)3 CopyRequest (com.google.cloud.storage.Storage.CopyRequest)3 HashMap (java.util.HashMap)3 StorageBatchResult (com.google.cloud.storage.StorageBatchResult)2 FileInputStream (java.io.FileInputStream)2 ByteBuffer (java.nio.ByteBuffer)2