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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations