Skip to content

Commit f3a1592

Browse files
committed
Merge remote-tracking branch 'upstream/master' into pubsub-doc-tag-fix
2 parents a28f3e1 + 776d39f commit f3a1592

15 files changed

Lines changed: 678 additions & 43 deletions

File tree

google-cloud-datastore/README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ Java idiomatic client for [Google Cloud Datastore](https://cloud.google.com/data
1212
- [Homepage](https://googlecloudplatform.github.io/google-cloud-java/)
1313
- [API Documentation](https://googlecloudplatform.github.io/google-cloud-java/apidocs/index.html?com/google/cloud/datastore/package-summary.html)
1414

15-
> Note: This client is a work-in-progress, and may occasionally
16-
> make backwards-incompatible changes.
17-
1815
Quickstart
1916
----------
2017
If you are using Maven, add this to your pom.xml file
@@ -170,9 +167,7 @@ Versioning
170167

171168
This library follows [Semantic Versioning](http://semver.org/).
172169

173-
It is currently in major version zero (``0.y.z``), which means that anything
174-
may change at any time and the public API should not be considered
175-
stable.
170+
It is currently in major version one (``1.y.z``), which means that the public API should be considered stable.
176171

177172
Contributing
178173
------------
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
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+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in READMEs. Any change to this file should be reflected in
20+
* the project's READMEs.
21+
*/
22+
23+
package com.google.cloud.examples.language.snippets;
24+
25+
import com.google.cloud.language.spi.v1.LanguageServiceClient;
26+
27+
import com.google.cloud.language.v1.Document;
28+
import com.google.cloud.language.v1.Document.Type;
29+
import com.google.cloud.language.v1.Sentiment;
30+
31+
/**
32+
* A snippet for Google Cloud Speech API showing how to analyze text message sentiment.
33+
*/
34+
public class AnalyzeSentiment {
35+
36+
public static void main(String... args) throws Exception {
37+
// Instantiates a client
38+
LanguageServiceClient language = LanguageServiceClient.create();
39+
40+
// The text to analyze
41+
String[] texts = {"I love this!", "I hate this!"};
42+
for (String text : texts) {
43+
Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
44+
// Detects the sentiment of the text
45+
Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();
46+
47+
System.out.printf("Text: \"%s\"%n", text);
48+
System.out.printf(
49+
"Sentiment: score = %s, magnitude = %s%n",
50+
sentiment.getScore(), sentiment.getMagnitude());
51+
}
52+
}
53+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
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+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in READMEs. Any change to this file should be reflected in
20+
* the project's READMEs.
21+
*/
22+
23+
package com.google.cloud.examples.speech.snippets;
24+
25+
import com.google.cloud.speech.spi.v1.SpeechClient;
26+
import com.google.cloud.speech.v1.RecognitionAudio;
27+
import com.google.cloud.speech.v1.RecognitionConfig;
28+
import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
29+
import com.google.cloud.speech.v1.RecognizeResponse;
30+
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
31+
import com.google.cloud.speech.v1.SpeechRecognitionResult;
32+
import com.google.protobuf.ByteString;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
35+
import java.nio.file.Paths;
36+
import java.util.List;
37+
38+
/**
39+
* A snippet for Google Natural Language API showing how to convert houman speech from an audio file
40+
* into a text form.
41+
*/
42+
public class RecognizeSpeech {
43+
public static void main(String... args) throws Exception {
44+
// Instantiates a client
45+
SpeechClient speech = SpeechClient.create();
46+
47+
// The path to the audio file to transcribe
48+
String fileName = "your/speech/audio/file.raw"; // for example "./resources/audio.raw";
49+
50+
// Reads the audio file into memory
51+
Path path = Paths.get(fileName);
52+
byte[] data = Files.readAllBytes(path);
53+
ByteString audioBytes = ByteString.copyFrom(data);
54+
55+
// Builds the sync recognize request
56+
RecognitionConfig config = RecognitionConfig.newBuilder()
57+
.setEncoding(AudioEncoding.LINEAR16)
58+
.setSampleRateHertz(16000)
59+
.setLanguageCode("en-US")
60+
.build();
61+
RecognitionAudio audio = RecognitionAudio.newBuilder()
62+
.setContent(audioBytes)
63+
.build();
64+
65+
// Performs speech recognition on the audio file
66+
RecognizeResponse response = speech.recognize(config, audio);
67+
List<SpeechRecognitionResult> results = response.getResultsList();
68+
69+
for (SpeechRecognitionResult result: results) {
70+
List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
71+
for (SpeechRecognitionAlternative alternative: alternatives) {
72+
System.out.printf("Transcription: %s%n", alternative.getTranscript());
73+
}
74+
}
75+
speech.close();
76+
}
77+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
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.google.cloud.examples.storage.snippets;
18+
19+
import com.google.cloud.Identity;
20+
import com.google.cloud.Policy;
21+
import com.google.cloud.Role;
22+
import com.google.cloud.storage.Storage;
23+
import com.google.cloud.storage.StorageOptions;
24+
25+
import java.util.Map;
26+
import java.util.Set;
27+
28+
/**
29+
* This class contains Bucket-level IAM snippets for the {@link Storage} interface.
30+
*/
31+
public class BucketIamSnippets {
32+
33+
/**
34+
* Example of listing the Bucket-Level IAM Roles and Members
35+
*/
36+
public Policy listBucketIamMembers(String bucketName) {
37+
// [START view_bucket_iam_members]
38+
// Initialize a Cloud Storage client
39+
Storage storage = StorageOptions.getDefaultInstance().getService();
40+
41+
// Get IAM Policy for a bucket
42+
Policy policy = storage.getIamPolicy(bucketName);
43+
44+
// Print Roles and its identities
45+
Map<Role, Set<Identity>> policyBindings = policy.getBindings();
46+
for(Map.Entry<Role, Set<Identity>> entry : policyBindings.entrySet()) {
47+
System.out.printf("Role: %s Identities: %s\n", entry.getKey(), entry.getValue());
48+
}
49+
// [END view_bucket_iam_members]
50+
return policy;
51+
}
52+
53+
/**
54+
* Example of adding a member to the Bucket-level IAM
55+
*/
56+
public Policy addBucketIamMember(String bucketName, Role role, Identity identity) {
57+
// [START add_bucket_iam_member]
58+
// Initialize a Cloud Storage client
59+
Storage storage = StorageOptions.getDefaultInstance().getService();
60+
61+
// Get IAM Policy for a bucket
62+
Policy policy = storage.getIamPolicy(bucketName);
63+
64+
// Add identity to Bucket-level IAM role
65+
Policy updatedPolicy = storage.setIamPolicy(bucketName,
66+
policy.toBuilder().addIdentity(role, identity).build());
67+
68+
if (updatedPolicy.getBindings().get(role).contains(identity)) {
69+
System.out.printf("Added %s with role %s to %s\n", identity, role, bucketName);
70+
}
71+
// [END add_bucket_iam_member]
72+
return updatedPolicy;
73+
}
74+
75+
/**
76+
* Example of removing a member from the Bucket-level IAM
77+
*/
78+
public Policy removeBucketIamMember(String bucketName, Role role, Identity identity) {
79+
// [START remove_bucket_iam_member]
80+
// Initialize a Cloud Storage client
81+
Storage storage = StorageOptions.getDefaultInstance().getService();
82+
83+
// Get IAM Policy for a bucket
84+
Policy policy = storage.getIamPolicy(bucketName);
85+
86+
// Remove an identity from a Bucket-level IAM role
87+
Policy updatedPolicy = storage.setIamPolicy(bucketName,
88+
policy.toBuilder().removeIdentity(role, identity).build());
89+
90+
if (updatedPolicy.getBindings().get(role) == null ||
91+
!updatedPolicy.getBindings().get(role).contains(identity)) {
92+
System.out.printf("Removed %s with role %s from %s\n", identity, role, bucketName);
93+
}
94+
// [END remove_bucket_iam_member]
95+
return updatedPolicy;
96+
}
97+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
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+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in READMEs. Any change to this file should be reflected in
20+
* the project's READMEs.
21+
*/
22+
23+
package com.google.cloud.examples.vision.snippets;
24+
import com.google.cloud.vision.spi.v1.ImageAnnotatorClient;
25+
import com.google.cloud.vision.v1.AnnotateImageRequest;
26+
import com.google.cloud.vision.v1.AnnotateImageResponse;
27+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
28+
import com.google.cloud.vision.v1.EntityAnnotation;
29+
import com.google.cloud.vision.v1.Feature;
30+
import com.google.cloud.vision.v1.Feature.Type;
31+
import com.google.cloud.vision.v1.Image;
32+
import com.google.protobuf.ByteString;
33+
import com.google.protobuf.Descriptors.FieldDescriptor;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.nio.file.Paths;
37+
import java.util.ArrayList;
38+
import java.util.List;
39+
import java.util.Map;
40+
41+
/**
42+
* A snippet for Google Cloud Vision API demonstrating how to determine what is shown on a picture.
43+
*/
44+
public class AnnotateImage {
45+
public static void main(String... args) throws Exception {
46+
// Instantiates a client
47+
ImageAnnotatorClient vision = ImageAnnotatorClient.create();
48+
49+
// The path to the image file to annotate
50+
String fileName = "your/image/path.jpg"; // for example "./resources/wakeupcat.jpg";
51+
52+
// Reads the image file into memory
53+
Path path = Paths.get(fileName);
54+
byte[] data = Files.readAllBytes(path);
55+
ByteString imgBytes = ByteString.copyFrom(data);
56+
57+
// Builds the image annotation request
58+
List<AnnotateImageRequest> requests = new ArrayList<>();
59+
Image img = Image.newBuilder().setContent(imgBytes).build();
60+
Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
61+
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
62+
.addFeatures(feat)
63+
.setImage(img)
64+
.build();
65+
requests.add(request);
66+
67+
// Performs label detection on the image file
68+
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
69+
List<AnnotateImageResponse> responses = response.getResponsesList();
70+
71+
for (AnnotateImageResponse res : responses) {
72+
if (res.hasError()) {
73+
System.out.printf("Error: %s\n", res.getError().getMessage());
74+
return;
75+
}
76+
77+
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
78+
for (Map.Entry<FieldDescriptor, Object> entry : annotation.getAllFields().entrySet()) {
79+
System.out.printf("%s : %s\n", entry.getKey(), entry.getValue());
80+
}
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)