Skip to content

Commit b0c8874

Browse files
committed
Merge pull request #257 from mziccard/add-blob-bucket-load
Add static load method to Blob and Bucket, add tests, update example
2 parents 3310a82 + d51311e commit b0c8874

8 files changed

Lines changed: 141 additions & 123 deletions

File tree

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,23 @@ Here is a code snippet showing a simple usage example from within Compute/App En
104104
import static java.nio.charset.StandardCharsets.UTF_8;
105105

106106
import com.google.gcloud.storage.Blob;
107+
import com.google.gcloud.storage.BlobId;
107108
import com.google.gcloud.storage.Storage;
108109
import com.google.gcloud.storage.StorageFactory;
109110
import com.google.gcloud.storage.StorageOptions;
110111

111112
import java.nio.ByteBuffer;
112113
import java.nio.channels.WritableByteChannel;
113114

114-
Storage storage = StorageFactory.instance().get(StorageOptions.getDefaultInstance());
115-
Blob blob = new Blob(storage, "bucket", "blob_name");
116-
if (!blob.exists()) {
117-
storage2.create(blob.info(), "Hello, Cloud Storage!".getBytes(UTF_8));
115+
StorageOptions options = StorageOptions.builder().projectId("project").build();
116+
Storage storage = StorageFactory.instance().get(options);
117+
BlobId blobId = BlobId.of("bucket", "blob_name");
118+
Blob blob = Blob.load(storage, blobId);
119+
if (blob == null) {
120+
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
121+
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
118122
} else {
119-
System.out.println("Updating content for " + blob.info().name());
123+
System.out.println("Updating content for " + blobId.name());
120124
byte[] prevContent = blob.content();
121125
System.out.println(new String(prevContent, UTF_8));
122126
WritableByteChannel channel = blob.writer();

0 commit comments

Comments
 (0)