Search in sources :

Example 46 with BlobInfo

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

the class StorageSnippets method createBlobWithSubArrayFromByteArray.

/**
 * Example of creating a blob with sub array from a byte array.
 */
// [TARGET create(BlobInfo, byte[], offset, length, BlobTargetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob createBlobWithSubArrayFromByteArray(String bucketName, String blobName, int offset, int length) {
    // [START createBlobWithSubArrayFromByteArray]
    BlobId blobId = BlobId.of(bucketName, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, "Hello, World!".getBytes(UTF_8), offset, length);
    // [END createBlobWithSubArrayFromByteArray]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 47 with BlobInfo

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

the class StorageSnippets method batchUpdateIterable.

/**
 * Example of updating information on several blobs using a single batch request.
 */
// [TARGET update(Iterable)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public List<Blob> batchUpdateIterable(String bucketName, String blobName1, String blobName2) {
    // [START batchUpdateIterable]
    Blob firstBlob = storage.get(bucketName, blobName1);
    Blob secondBlob = storage.get(bucketName, blobName2);
    List<BlobInfo> blobs = new LinkedList<>();
    blobs.add(firstBlob.toBuilder().setContentType("text/plain").build());
    blobs.add(secondBlob.toBuilder().setContentType("text/plain").build());
    List<Blob> updatedBlobs = storage.update(blobs);
    // [END batchUpdateIterable]
    return updatedBlobs;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) LinkedList(java.util.LinkedList)

Example 48 with BlobInfo

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

the class StorageSnippets method createBlobFromByteArray.

/**
 * Example of creating a blob from a byte array.
 */
// [TARGET create(BlobInfo, byte[], BlobTargetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromByteArray(String bucketName, String blobName) {
    // [START createBlobFromByteArray]
    BlobId blobId = BlobId.of(bucketName, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, "Hello, World!".getBytes(UTF_8));
    // [END createBlobFromByteArray]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 49 with BlobInfo

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

the class StorageSnippets method createBlobFromInputStream.

/**
 * Example of creating a blob from an input stream.
 */
// [TARGET create(BlobInfo, InputStream, BlobWriteOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromInputStream(String bucketName, String blobName) {
    // [START createBlobFromInputStream]
    InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
    BlobId blobId = BlobId.of(bucketName, blobName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, content);
    // [END createBlobFromInputStream]
    return blob;
}
Also used : Blob(com.google.cloud.storage.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Example 50 with BlobInfo

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

the class UploadObjectFromMemory method uploadObjectFromMemory.

public static void uploadObjectFromMemory(String projectId, String bucketName, String objectName, String contents) throws IOException {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    // The ID of your GCS object
    // String objectName = "your-object-name";
    // The string of contents you wish to upload
    // String contents = "Hello world!";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(bucketName, objectName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
    byte[] content = contents.getBytes(StandardCharsets.UTF_8);
    storage.createFrom(blobInfo, new ByteArrayInputStream(content));
    System.out.println("Object " + objectName + " uploaded to bucket " + bucketName + " with contents " + contents);
}
Also used : Storage(com.google.cloud.storage.Storage) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId)

Aggregations

BlobInfo (com.google.cloud.storage.BlobInfo)94 Test (org.junit.Test)61 Blob (com.google.cloud.storage.Blob)56 BlobId (com.google.cloud.storage.BlobId)31 Storage (com.google.cloud.storage.Storage)21 StorageException (com.google.cloud.storage.StorageException)17 WriteChannel (com.google.cloud.WriteChannel)13 ReadChannel (com.google.cloud.ReadChannel)7 CopyWriter (com.google.cloud.storage.CopyWriter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteBuffer (java.nio.ByteBuffer)7 InputStream (java.io.InputStream)4 URL (java.net.URL)4 StorageBatch (com.google.cloud.storage.StorageBatch)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 TestRunner (org.apache.nifi.util.TestRunner)3 Acl (com.google.cloud.storage.Acl)2 Bucket (com.google.cloud.storage.Bucket)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2