Skip to content

Commit 48b99be

Browse files
docs: Copy all storage samples from java-docs-samples (#1258)
* Copy all storage samples from java-docs-samples * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * use gcloud-devel testing account * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix cleanup config * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix cfg * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix cfg and owlbot * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix csek and request pays tests * fix cfg and owlbot ignore * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix requester pays samples * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix download requester pays sample * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix system out capture * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix disable requester pays sample * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix label and kms tests * add remaining samples and tests * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix hmac cleanup * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix snippet output capture * fix standardOut reset * Fix standard out capture * increase retry backoff multiplier * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix UBLA sample and HMAC service account * fix copyright header formatting * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix checkstyle * add sleeps for hmac tests * use java docs samples testing account * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix snapshot dependency * fix kms resource * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix hmac list test * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add sleeps to fix 429 errors * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix hmac service account * Fix hmac service account * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix standard out * fix bucket retention test * fix checkstyle * Add comment about setout * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix acl sample comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent ae7229a commit 48b99be

File tree

109 files changed

+6103
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+6103
-88
lines changed

.kokoro/nightly/samples.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ env_vars: {
4343
key: "IT_SERVICE_ACCOUNT_EMAIL"
4444
value: "samples@java-docs-samples-testing.iam.gserviceaccount.com"
4545
}
46+

.kokoro/presubmit/integration.cfg

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ env_vars: {
2828
}
2929

3030
env_vars: {
31-
key: "IT_SERVICE_ACCOUNT_EMAIL"
32-
value: "it-service-account@gcloud-devel.iam.gserviceaccount.com"
31+
key: "SECRET_MANAGER_KEYS"
32+
value: "java-it-service-account"
3333
}
3434

3535
env_vars: {
36-
key: "SECRET_MANAGER_KEYS"
37-
value: "java-it-service-account"
36+
key: "IT_SERVICE_ACCOUNT_EMAIL"
37+
value: "it-service-account@gcloud-devel.iam.gserviceaccount.com"
3838
}
39+

.kokoro/presubmit/samples.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ env_vars: {
3838
key: "IT_SERVICE_ACCOUNT_EMAIL"
3939
value: "samples@java-docs-samples-testing.iam.gserviceaccount.com"
4040
}
41+

CONTRIBUTING.md

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,17 @@ integration tests.
4848

4949
```bash
5050
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account.json
51-
export IT_SERVICE_ACCOUNT_EMAIL=my-service-account@my-project.iam.gserviceaccount.com
5251
mvn -Penable-integration-tests clean verify
5352
```
5453

5554
## Code Samples
5655

57-
Code Samples must be bundled in separate Maven modules, and guarded by a
58-
Maven profile with the name `enable-samples`.
56+
All code samples must be in compliance with the [java sample formatting guide][3].
57+
Code Samples must be bundled in separate Maven modules.
5958

6059
The samples must be separate from the primary project for a few reasons:
61-
1. Primary projects have a minimum Java version of Java 7 whereas samples have
62-
a minimum Java version of Java 8. Due to this we need the ability to
60+
1. Primary projects have a minimum Java version of Java 8 whereas samples can have
61+
Java version of Java 11. Due to this we need the ability to
6362
selectively exclude samples from a build run.
6463
2. Many code samples depend on external GCP services and need
6564
credentials to access the service.
@@ -69,39 +68,16 @@ The samples must be separate from the primary project for a few reasons:
6968
### Building
7069

7170
```bash
72-
mvn -Penable-samples clean verify
71+
mvn clean verify
7372
```
7473

7574
Some samples require access to GCP services and require a service account:
7675

7776
```bash
7877
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account.json
79-
mvn -Penable-samples clean verify
78+
mvn clean verify
8079
```
8180

82-
### Profile Config
83-
84-
1. To add samples in a profile to your Maven project, add the following to your
85-
`pom.xml`
86-
87-
```xml
88-
<project>
89-
[...]
90-
<profiles>
91-
<profile>
92-
<id>enable-samples</id>
93-
<modules>
94-
<module>sample</module>
95-
</modules>
96-
</profile>
97-
</profiles>
98-
[...]
99-
</project>
100-
```
101-
102-
2. [Activate](#profile-activation) the profile.
103-
3. Define your samples in a normal Maven project in the `samples/` directory.
104-
10581
### Code Formatting
10682

10783
Code in this repo is formatted with
@@ -111,30 +87,6 @@ To run formatting on your project, you can run:
11187
mvn com.coveo:fmt-maven-plugin:format
11288
```
11389

114-
### Profile Activation
115-
116-
To include code samples when building and testing the project, enable the
117-
`enable-samples` Maven profile.
118-
119-
#### Command line
120-
121-
To activate the Maven profile on the command line add `-Penable-samples` to your
122-
Maven command.
123-
124-
#### Maven `settings.xml`
125-
126-
To activate the Maven profile in your `~/.m2/settings.xml` add an entry of
127-
`enable-samples` following the instructions in [Active Profiles][2].
128-
129-
This method has the benefit of applying to all projects you build (and is
130-
respected by IntelliJ IDEA) and is recommended if you are going to be
131-
contributing samples to several projects.
132-
133-
#### IntelliJ IDEA
134-
135-
To activate the Maven Profile inside IntelliJ IDEA, follow the instructions in
136-
[Activate Maven profiles][3] to activate `enable-samples`.
137-
13890
[1]: https://cloud.google.com/docs/authentication/getting-started#creating_a_service_account
13991
[2]: https://maven.apache.org/settings.html#Active_Profiles
140-
[3]: https://www.jetbrains.com/help/idea/work-with-maven-profiles.html#activate_maven_profiles
92+
[3]: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md

README.md

Lines changed: 85 additions & 0 deletions
Large diffs are not rendered by default.

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3017,7 +3017,7 @@ private void retentionPolicyLockRequesterPays(boolean requesterPays)
30173017
storage.update(bucketInfo, Storage.BucketTargetOption.userProject(projectId));
30183018
assertFalse(updateBucket.requesterPays());
30193019
}
3020-
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
3020+
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS, projectId);
30213021
}
30223022
}
30233023

samples/install-without-bom/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545
<version>1.1.3</version>
4646
<scope>test</scope>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.apache.httpcomponents</groupId>
50+
<artifactId>httpclient</artifactId>
51+
<version>4.5.13</version>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.apache.httpcomponents</groupId>
56+
<artifactId>httpmime</artifactId>
57+
<version>4.5.13</version>
58+
<scope>test</scope>
59+
</dependency>
4860
</dependencies>
4961

5062
<!-- compile and run all snippet tests -->

samples/snapshot/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>com.google.cloud</groupId>
3030
<artifactId>google-cloud-storage</artifactId>
31-
<version>2.4.5</version>
31+
<version>2.4.6-SNAPSHOT</version>
3232
</dependency>
3333

3434
<dependency>
@@ -43,6 +43,12 @@
4343
<version>1.1.3</version>
4444
<scope>test</scope>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.apache.httpcomponents</groupId>
48+
<artifactId>httpmime</artifactId>
49+
<version>4.5.13</version>
50+
<scope>test</scope>
51+
</dependency>
4652
</dependencies>
4753
<!-- {x-version-update-end} -->
4854

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2020 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+
// [START storage_generate_signed_post_policy_v4]
20+
21+
import com.google.cloud.storage.BlobInfo;
22+
import com.google.cloud.storage.PostPolicyV4;
23+
import com.google.cloud.storage.Storage;
24+
import com.google.cloud.storage.StorageOptions;
25+
import java.util.Map;
26+
import java.util.concurrent.TimeUnit;
27+
28+
public class GenerateSignedPostPolicyV4 {
29+
/**
30+
* Generating a signed POST policy requires Credentials which implement ServiceAccountSigner.
31+
* These can be set explicitly using the Storage.PostPolicyV4Option.signWith(ServiceAccountSigner)
32+
* option. If you don't, you could also pass a service account signer to StorageOptions, i.e.
33+
* StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
34+
* neither of these options are used, which means the following code only works when the
35+
* credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
36+
* credentials are authorized to sign a policy. See the documentation for
37+
* Storage.generateSignedPostPolicyV4 for more details.
38+
*/
39+
public static void generateSignedPostPolicyV4(
40+
String projectId, String bucketName, String blobName) {
41+
// The ID of your GCP project
42+
// String projectId = "your-project-id";
43+
44+
// The ID of the GCS bucket to upload to
45+
// String bucketName = "your-bucket-name"
46+
47+
// The name to give the object uploaded to GCS
48+
// String blobName = "your-object-name"
49+
50+
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
51+
52+
PostPolicyV4.PostFieldsV4 fields =
53+
PostPolicyV4.PostFieldsV4.newBuilder().setCustomMetadataField("test", "data").build();
54+
55+
PostPolicyV4 policy =
56+
storage.generateSignedPostPolicyV4(
57+
BlobInfo.newBuilder(bucketName, blobName).build(), 10, TimeUnit.MINUTES, fields);
58+
59+
StringBuilder htmlForm =
60+
new StringBuilder(
61+
"<form action='"
62+
+ policy.getUrl()
63+
+ "' method='POST' enctype='multipart/form-data'>\n");
64+
for (Map.Entry<String, String> entry : policy.getFields().entrySet()) {
65+
htmlForm.append(
66+
" <input name='"
67+
+ entry.getKey()
68+
+ "' value='"
69+
+ entry.getValue()
70+
+ "' type='hidden' />\n");
71+
}
72+
htmlForm.append(" <input type='file' name='file'/><br />\n");
73+
htmlForm.append(" <input type='submit' value='Upload File'/><br />\n");
74+
htmlForm.append("</form>\n");
75+
76+
System.out.println(
77+
"You can use the following HTML form to upload an object to bucket "
78+
+ bucketName
79+
+ " for the next ten minutes:");
80+
System.out.println(htmlForm.toString());
81+
}
82+
}
83+
// [END storage_generate_signed_post_policy_v4]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2020 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+
// [START storage_get_service_account]
20+
import com.google.cloud.storage.ServiceAccount;
21+
import com.google.cloud.storage.Storage;
22+
import com.google.cloud.storage.StorageOptions;
23+
24+
public class GetServiceAccount {
25+
public static void getServiceAccount(String projectId) {
26+
// The ID of your GCP project
27+
// String projectId = "your-project-id";
28+
29+
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
30+
ServiceAccount serviceAccount = storage.getServiceAccount(projectId);
31+
System.out.println(
32+
"The GCS service account for project " + projectId + " is: " + serviceAccount.getEmail());
33+
}
34+
}
35+
// [END storage_get_service_account]

0 commit comments

Comments
 (0)