Skip to content

Commit ebab7e6

Browse files
chingor13frankyn
andauthored
chore: generate README from templates (#238)
* chore: generate README from templates * chore: adjust product name * chore: restore Example Applications section Co-authored-by: Frank Natividad <[email protected]>
1 parent 03eace6 commit ebab7e6

File tree

5 files changed

+261
-113
lines changed

5 files changed

+261
-113
lines changed

.readme-partials.yaml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
custom_content: |
2+
#### Creating an authorized service object
3+
4+
To make authenticated requests to Google Cloud Storage, you must create a service object with credentials. You can
5+
then make API calls by calling methods on the Storage service object. The simplest way to authenticate is to use
6+
[Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials).
7+
These credentials are automatically inferred from your environment, so you only need the following code to create your
8+
service object:
9+
10+
```java
11+
import com.google.cloud.storage.Storage;
12+
import com.google.cloud.storage.StorageOptions;
13+
14+
Storage storage = StorageOptions.getDefaultInstance().getService();
15+
```
16+
17+
For other authentication options, see the [Authentication](https://github.com/googleapis/google-cloud-java#authentication) page.
18+
19+
#### Storing data
20+
Stored objects are called "blobs" in `google-cloud` and are organized into containers called "buckets". `Blob`, a
21+
subclass of `BlobInfo`, adds a layer of service-related functionality over `BlobInfo`. Similarly, `Bucket` adds a
22+
layer of service-related functionality over `BucketInfo`. In this code snippet, we will create a new bucket and
23+
upload a blob to that bucket.
24+
25+
Add the following imports at the top of your file:
26+
27+
```java
28+
import static java.nio.charset.StandardCharsets.UTF_8;
29+
30+
import com.google.cloud.storage.Blob;
31+
import com.google.cloud.storage.Bucket;
32+
import com.google.cloud.storage.BucketInfo;
33+
```
34+
35+
Then add the following code to create a bucket and upload a simple blob.
36+
37+
*Important: Bucket names have to be globally unique (among all users of Cloud Storage). If you choose a bucket name
38+
that already exists, you'll get a helpful error message telling you to choose another name. In the code below, replace
39+
"my_unique_bucket" with a unique bucket name. See more about naming rules
40+
[here](https://cloud.google.com/storage/docs/bucket-naming?hl=en#requirements).*
41+
42+
```java
43+
// Create a bucket
44+
String bucketName = "my_unique_bucket"; // Change this to something unique
45+
Bucket bucket = storage.create(BucketInfo.of(bucketName));
46+
47+
// Upload a blob to the newly created bucket
48+
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
49+
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
50+
Blob blob = storage.create(blobInfo, "a simple blob".getBytes(UTF_8));
51+
```
52+
53+
A complete example for creating a blob can be found at
54+
[CreateBlob.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/CreateBlob.java).
55+
56+
At this point, you will be able to see your newly created bucket and blob on the Google Developers Console.
57+
58+
#### Retrieving data
59+
Now that we have content uploaded to the server, we can see how to read data from the server. Add the following line
60+
to your program to get back the blob we uploaded.
61+
62+
```java
63+
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
64+
byte[] content = storage.readAllBytes(blobId);
65+
String contentString = new String(content, UTF_8);
66+
```
67+
68+
A complete example for accessing blobs can be found at
69+
[CreateBlob.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/CreateBlob.java).
70+
71+
#### Updating data
72+
Another thing we may want to do is update a blob. The following snippet shows how to update a Storage blob if it exists.
73+
74+
``` java
75+
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
76+
Blob blob = storage.get(blobId);
77+
if (blob != null) {
78+
byte[] prevContent = blob.getContent();
79+
System.out.println(new String(prevContent, UTF_8));
80+
WritableByteChannel channel = blob.writer();
81+
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
82+
channel.close();
83+
}
84+
```
85+
86+
The complete source code can be found at
87+
[UpdateBlob.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/UpdateBlob.java).
88+
89+
#### Listing buckets and contents of buckets
90+
Suppose that you've added more buckets and blobs, and now you want to see the names of your buckets and the contents
91+
of each one. Add the following code to list all your buckets and all the blobs inside each bucket.
92+
93+
```java
94+
// List all your buckets
95+
System.out.println("My buckets:");
96+
for (Bucket bucket : storage.list().iterateAll()) {
97+
System.out.println(bucket);
98+
99+
// List all blobs in the bucket
100+
System.out.println("Blobs in the bucket:");
101+
for (Blob blob : bucket.list().iterateAll()) {
102+
System.out.println(blob);
103+
}
104+
}
105+
```
106+
107+
#### Complete source code
108+
109+
In
110+
[CreateAndListBucketsAndBlobs.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/CreateAndListBucketsAndBlobs.java)
111+
we put together examples creating and listing buckets and blobs into one program. The program assumes that you are
112+
running on Compute Engine or from your own desktop. To run the example on App Engine, simply move
113+
the code from the main method to your application's servlet class and change the print statements to
114+
display on your webpage.
115+
116+
### Example Applications
117+
118+
- [`StorageExample`](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/StorageExample.java) is a simple command line interface that provides some of Cloud Storage's functionality. Read more about using the application on the [`StorageExample` docs page](https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/README.md).
119+
- [`Bookshelf`](https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/bookshelf) - An App Engine application that manages a virtual bookshelf.
120+
- This app uses `google-cloud` to interface with Cloud Datastore and Cloud Storage. It also uses Cloud SQL, another Google Cloud Platform service.
121+
- [`Flexible Environment/Storage example`](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/flexible/cloudstorage) - An app that uploads files to a public Cloud Storage bucket on the App Engine Flexible Environment runtime.

.repo-metadata.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
22
"name": "storage",
3-
"name_pretty": "Google Cloud Storage",
3+
"name_pretty": "Cloud Storage",
44
"product_documentation": "https://cloud.google.com/storage",
55
"client_documentation": "https://googleapis.dev/java/java-storage/latest/",
6+
"api_description": "is a durable and highly available object storage service. Google Cloud Storage is almost infinitely scalable and guarantees consistency: when a write succeeds, the latest copy of the object will be returned to any GET, globally.",
67
"issue_tracker": "https://issuetracker.google.com/savedsearches/559782",
78
"release_level": "ga",
89
"language": "java",
910
"repo": "googleapis/java-storage",
1011
"repo_short": "java-storage",
1112
"distribution_name": "com.google.cloud:google-cloud-storage",
12-
"api_id": "storage.googleapis.com"
13+
"api_id": "storage.googleapis.com",
14+
"requires_billing": true
1315
}

0 commit comments

Comments
 (0)