|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.storage; |
| 18 | + |
| 19 | +import com.google.cloud.BatchResult.Callback; |
| 20 | +import com.google.cloud.storage.Blob; |
| 21 | +import com.google.cloud.storage.BlobId; |
| 22 | +import com.google.cloud.storage.BlobInfo; |
| 23 | +import com.google.cloud.storage.BucketInfo; |
| 24 | +import com.google.cloud.storage.Storage; |
| 25 | +import com.google.cloud.storage.StorageBatch; |
| 26 | +import com.google.cloud.storage.StorageException; |
| 27 | +import com.google.cloud.storage.StorageOptions; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.util.UUID; |
| 30 | + |
| 31 | +/** Sample Storage application compiled with Native Image. */ |
| 32 | +public class NativeImageStorageSample { |
| 33 | + |
| 34 | + static String BUCKET_NAME = "nativeimage-sample-bucket-" + UUID.randomUUID(); |
| 35 | + static String FILENAME = "nativeimage-sample-file-" + UUID.randomUUID(); |
| 36 | + |
| 37 | + /** Runs the storage sample application. */ |
| 38 | + public static void main(String[] args) { |
| 39 | + |
| 40 | + Storage storageClient = StorageOptions.getDefaultInstance().getService(); |
| 41 | + |
| 42 | + try { |
| 43 | + createBucket(storageClient, BUCKET_NAME); |
| 44 | + createFile(storageClient, BUCKET_NAME, FILENAME); |
| 45 | + runBatchOperations(storageClient, BUCKET_NAME, FILENAME); |
| 46 | + } finally { |
| 47 | + System.out.println("Deleting resources."); |
| 48 | + storageClient.delete(BUCKET_NAME, FILENAME); |
| 49 | + storageClient.delete(BUCKET_NAME); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static void runBatchOperations( |
| 54 | + Storage storageClient, String bucketName, String fileName) { |
| 55 | + BlobId blobId = BlobId.of(bucketName, fileName); |
| 56 | + |
| 57 | + StorageBatch batch = storageClient.batch(); |
| 58 | + batch |
| 59 | + .update(BlobInfo.newBuilder(blobId).build()) |
| 60 | + .notify( |
| 61 | + new Callback<Blob, StorageException>() { |
| 62 | + @Override |
| 63 | + public void success(Blob blob) { |
| 64 | + System.out.println("Batch update succeeded on " + fileName); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void error(StorageException e) { |
| 69 | + System.out.println("Batch update failed with cause: " + e); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + batch.submit(); |
| 74 | + } |
| 75 | + |
| 76 | + private static void createBucket(Storage storageClient, String bucketName) { |
| 77 | + BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setLocation("us-east1").build(); |
| 78 | + storageClient.create(bucketInfo); |
| 79 | + System.out.println("Created bucket " + bucketName); |
| 80 | + } |
| 81 | + |
| 82 | + private static void createFile(Storage storageClient, String bucketName, String fileName) { |
| 83 | + BlobInfo blobInfo = |
| 84 | + BlobInfo.newBuilder(bucketName, fileName).setContentType("text/plain").build(); |
| 85 | + storageClient.create(blobInfo, "Hello World!".getBytes(StandardCharsets.UTF_8)); |
| 86 | + System.out.println("Created file " + blobInfo.getName()); |
| 87 | + |
| 88 | + Blob blob = storageClient.get(bucketName, fileName); |
| 89 | + String content = new String(blob.getContent(), StandardCharsets.UTF_8); |
| 90 | + System.out.println("Successfully wrote to file: " + content); |
| 91 | + } |
| 92 | +} |
0 commit comments