8000 [Asset] Add quick start code for ExportAssets API (#1254) · ulfjack/java-docs-samples@806436b · GitHub
[go: up one dir, main page]

Skip to content

Commit 806436b

Browse files
peter-zheng-gdzlier-gcp
authored andcommitted
[Asset] Add quick start code for ExportAssets API (GoogleCloudPlatform#1254)
* [Asset] Add quick start code for ExportAssets. * [Asset] Add pom.xml. * [Asset] Add pom.xml. * [Asset] Test: Assert bucket was created successfully. * [Asset] Use shorter bucket name in case bucket name exceeds limit. * [Asset] Print console output for easier troubleshooting. * [Asset] Avoid random bucket name to avoid gcs file writing failure. * [Asset] Fix build issue. * [Asset] Minor update on comment. * [Asset] Add quick start code for ExportAssets API * [Asset] Minor comment fix
1 parent 34e7ecc commit 806436b

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

asset/cloud-client/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!--
2+
Copyright 2018 Google LLC
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
<project>
14+
<modelVersion>4.0.0</modelVersion>
15+
<groupId>com.example.asset</groupId>
16+
<artifactId>asset-google-cloud-samples</artifactId>
17+
<packaging>jar</packaging>
18+
19+
<!--
20+
The parent pom defines common style checks and testing strategies for our samples.
21+
Removing or replacing it should not affect the execution of the samples in anyway.
22+
-->
23+
<parent>
24+
<groupId>com.google.cloud.samples</groupId>
25+
<artifactId>shared-configuration</artifactId>
26+
<version>1.0.10</version>
27+
</parent>
28+
29+
<properties>
30+
<maven.compiler.target>1.8</maven.compiler.target>
31+
<maven.compiler.source>1.8</maven.compiler.source>
32+
</properties>
33+
34+
<dependencies>
35+
<!-- [START asset_java_dependencies] -->
36+
<dependency>
37+
<groupId>com.google.cloud</groupId>
38+
<artifactId>google-cloud-asset</artifactId>
39+
<version>0.63.0-beta</version>
40+
</dependency>
41+
<!-- [END asset_java_dependencies] -->
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.42</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.google.cloud</groupId>
58+
<artifactId>google-cloud-storage</artifactId>
59+
<version>1.38.0</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2018 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.asset;
18+
19+
import com.google.cloud.ServiceOptions;
20+
import com.google.cloud.asset.v1beta1.AssetServiceClient;
21+
import com.google.cloud.asset.v1beta1.ExportAssetsRequest;
22+
import com.google.cloud.asset.v1beta1.ExportAssetsResponse;
23+
import com.google.cloud.asset.v1beta1.GcsDestination;
24+
import com.google.cloud.asset.v1beta1.OutputConfig;
25+
import com.google.cloud.asset.v1beta1.ProjectName;
26+
27+
// [START asset_quickstart_export_assets]
28+
// Imports the Google Cloud client library
29+
30+
public class ExportAssetsExample {
31+
32+
// Use the default project Id.
33+
private static final String projectId = ServiceOptions.getDefaultProjectId();
34+
35+
// Export assets for a project.
36+
// @param args path where the results will be exported to.
37+
public static void main(String... args) throws Exception {
38+
// Gcs path, e.g.: "gs://<my_asset_bucket>/<my_asset_dump_file>"
39+
String exportPath = args[0];
40+
try (AssetServiceClient client = AssetServiceClient.create()) {
41+
ProjectName parent = ProjectName.of(projectId);
42+
OutputConfig outputConfig =
43+
OutputConfig.newBuilder()
44+
.setGcsDestination(GcsDestination.newBuilder().setUri(exportPath).build())
45+
.build();
46+
ExportAssetsRequest request =
47+
ExportAssetsRequest.newBuilder()
48+
.setParent(parent.toString())
49+
.setOutputConfig(outputConfig)
50+
.build();
51+
ExportAssetsResponse response = client.exportAssetsAsync(request).get();
52+
System.out.println(response);
53+
}
54+
}
55+
}
56+
// [END asset_quickstart]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2018 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.asset;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.storage.BlobInfo;
22+
import com.google.cloud.storage.Bucket;
23+
import com.google.cloud.storage.BucketInfo;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.Storage.BlobListOption;
26+
import com.google.cloud.storage.StorageOptions;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
import java.util.UUID;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
/** Tests for quickstart sample. */
37+
@RunWith(JUnit4.class)
38+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
39+
public class QuickStartIT {
40+
private static final String bucketName = UUID.randomUUID().toString();
41+
private ByteArrayOutputStream bout;
42+
private PrintStream out;
43+
44+
private static final void deleteBucket(String bucketName) {
45+
Storage storage = StorageOptions.getDefaultInstance().getService();
46+
for (BlobInfo info : storage.list(bucketName, BlobListOption.versions(true)).getValues()) {
47+
storage.delete(info.getBlobId());
48+
101FB }
49+
storage.delete(bucketName);
50+
}
51+
52+
private static final void createBucket(String bucketName) {
53+
Storage storage = StorageOptions.getDefaultInstance().getService();
54+
if (storage.get(bucketName) != null) {
55+
// Bucket exists.
56+
return;
57+
}
58+
Bucket bucket = storage.create(BucketInfo.of(bucketName));
59+
assertThat(bucket).isNotNull();
60+
}
61+
62+
@Before
63+
public void setUp() {
64+
createBucket(bucketName);
65+
bout = new ByteArrayOutputStream();
66+
out = new PrintStream(bout);
67+
System.setOut(out);
68+
}
69+
70+
@After
71+
public void tearDown() {
72+
String consoleOutput = bout.toString();
73+
System.setOut(null);
74+
deleteBucket(bucketName);
75+
}
76+
77+
@Test
78+
public void testExportAssetExample() throws Exception {
79+
String assetDumpPath = String.format("gs://%s/my-assets-dump.txt", bucketName);
80+
ExportAssetsExample.main(assetDumpPath);
81+
String got = bout.toString();
82+
assertThat(got).contains(String.format("uri: \"%s\"", assetDumpPath));
83+
}
84+
}

0 commit comments

Comments
 (0)
0