Skip to content

Commit af27369

Browse files
Capstanwonderfly
authored andcommitted
Refactor, simplify and modernize GCS sample
Also, * Pull up api client dependencies to associated 1.20.0 version and update Guava to latest, 18.0. * Move use of API to v1. * Pull out specific uses into \*Example.java files. * Exercise several such uses by the main StorageSample class. * Demonstrate md5 and crc32c validation. * Upload/download progress and ACL editing from the original sample have been removed. * Update instructions to reference github instead of the old code.google.com. https://codereview.appspot.com/236910043/ Closes googleapis/google-api-java-client#938
1 parent 012bf76 commit af27369

15 files changed

Lines changed: 774 additions & 465 deletions

storage-cmdline-sample/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/*

storage-cmdline-sample/instructions.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ <h3>Browse Online</h3>
88

99
<ul>
1010
<li><a
11-
href="http://code.google.com/p/google-api-java-client/source/browse?repo=samples#hg/storage-cmdline-sample">Browse
11+
href="https://github.com/google/google-api-java-client-samples/tree/master/storage-cmdline-sample">Browse
1212
Source</a>, or main file <a
13-
href="http://code.google.com/p/google-api-java-client/source/browse/storage-cmdline-sample/src/main/java/com/google/api/services/samples/storage/cmdline/StorageSample.java?repo=samples">StorageSample.java</a></li>
13+
href="https://github.com/google/google-api-java-client-samples/blob/master/storage-cmdline-sample/src/main/java/com/google/api/services/samples/storage/cmdline/StorageSample.java">StorageSample.java</a></li>
1414
</ul>
1515

1616
<h3>Register Your Application</h3>
1717

1818
<ul>
19-
<li>Visit the <a href="https://cloud.google.com/console/start/api?id=storage_api">Google Developers Console</a>.
19+
<li>Visit the <a href="https://console.developers.google.com/flows/enableapi?apiid=storage_api">Google Developers Console</a>.
2020
</li>
2121
<li>If necessary, sign in to your Google Account, select or create a project,
2222
and agree to the terms of service. Click Continue.</li>
@@ -30,7 +30,8 @@ <h3>Register Your Application</h3>
3030
</li>
3131
</ul>
3232
<p>For more information about registering your application, see the Google Cloud Storage documentation
33-
<a href="https://developers.google.com/storage/docs/json_api/v1/json-api-java-samples">JSON Java Example</a>.
33+
<a href="https://developers.google.com/storage/docs/json_api/v1/json-api-java-samples">JSON Java Example</a>,
34+
as setup for that simpler example is similar.
3435
</p>
3536
<h3>Checkout Instructions</h3>
3637

@@ -45,7 +46,7 @@ <h3>Checkout Instructions</h3>
4546

4647
<pre>
4748
cd <i>[someDirectory]</i>
48-
hg clone https://code.google.com/p/google-api-java-client.samples/ google-api-java-client-samples
49+
git clone https://github.com/google/google-api-java-client-samples.git
4950
cd google-api-java-client-samples/storage-cmdline-sample
5051
cp ~/Downloads/client_secrets.json src/main/resources/client_secrets.json
5152
# Edit the settings file and enter in appropriate values.

storage-cmdline-sample/pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@
8080

8181

8282
<dependencies>
83+
<dependency>
84+
<groupId>com.google.guava</groupId>
85+
<artifactId>guava</artifactId>
86+
<version>18.0</version>
87+
</dependency>
8388
<dependency>
8489
<groupId>com.google.api-client</groupId>
8590
<artifactId>google-api-client</artifactId>
8691
<version>${project.api.version}</version>
8792
</dependency>
88-
<dependency>
89-
<groupId>com.google.guava</groupId>
90-
<artifactId>guava</artifactId>
91-
<version>14.0.1</version>
92-
</dependency>
9393
<dependency>
9494
<groupId>com.google.oauth-client</groupId>
9595
<artifactId>google-oauth-client-jetty</artifactId>
@@ -98,7 +98,7 @@
9898
<dependency>
9999
<groupId>com.google.apis</groupId>
100100
<artifactId>google-api-services-storage</artifactId>
101-
<version>v1beta2-rev51-1.19.0</version>
101+
<version>v1-rev33-1.20.0</version>
102102
</dependency>
103103
<dependency>
104104
<groupId>com.google.http-client</groupId>
@@ -109,9 +109,9 @@
109109

110110
<properties>
111111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
112-
<project.http.version>1.19.0</project.http.version>
113-
<project.oauth.version>1.19.0</project.oauth.version>
114-
<project.api.version>1.19.0</project.api.version>
112+
<project.http.version>1.20.0</project.http.version>
113+
<project.oauth.version>1.20.0</project.oauth.version>
114+
<project.api.version>1.20.0</project.api.version>
115115
</properties>
116116

117117
</project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2012 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.storage.cmdline;
16+
17+
import java.io.InputStream;
18+
import java.util.Random;
19+
20+
21+
/**
22+
* Support classes for the command-line sample.
23+
*/
24+
public class Helpers {
25+
26+
/**
27+
* Generates a random data block and repeats it to provide the stream.
28+
*
29+
* <p>Using a buffer instead of just filling from java.util.Random because the latter causes
30+
* noticeable lag in stream reading, which detracts from upload speed. This class takes all that
31+
* cost in the constructor.
32+
*/
33+
public static class RandomDataBlockInputStream extends InputStream {
34+
35+
private long byteCountRemaining;
36+
private final byte[] buffer;
37+
38+
public RandomDataBlockInputStream(long size, int blockSize) {
39+
byteCountRemaining = size;
40+
final Random random = new Random();
41+
buffer = new byte[blockSize];
42+
random.nextBytes(buffer);
43+
}
44+
45+
/*
46+
* (non-Javadoc)
47+
*
48+
* @see java.io.InputStream#read()
49+
*/
50+
@Override
51+
public int read() {
52+
throw new AssertionError("Not implemented; too slow.");
53+
}
54+
55+
/*
56+
* (non-Javadoc)
57+
*
58+
* @see java.io.InputStream#read(byte [], int, int)
59+
*/
60+
@Override
61+
public int read(byte b[], int off, int len) {
62+
if (b == null) {
63+
throw new NullPointerException();
64+
} else if (off < 0 || len < 0 || len > b.length - off) {
65+
throw new IndexOutOfBoundsException();
66+
} else if (len == 0) {
67+
return 0;
68+
} else if (byteCountRemaining == 0) {
69+
return -1;
70+
}
71+
int actualLen = len > byteCountRemaining ? (int) byteCountRemaining : len;
72+
for (int i = off; i < actualLen; i++) {
73+
b[i] = buffer[i % buffer.length];
74+
}
75+
byteCountRemaining -= actualLen;
76+
return actualLen;
77+
}
78+
}
79+
80+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2012 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.storage.cmdline;
16+
17+
import com.google.api.client.json.GenericJson;
18+
import com.google.api.client.json.JsonFactory;
19+
import com.google.api.client.util.Key;
20+
21+
import java.io.IOException;
22+
23+
/** Samples settings JSON Model. */
24+
public final class SampleSettings extends GenericJson {
25+
26+
@Key("project")
27+
private String project;
28+
29+
@Key("bucket")
30+
private String bucket;
31+
32+
@Key("prefix")
33+
private String prefix;
34+
35+
public String getProject() {
36+
return project;
37+
}
38+
39+
public String getBucket() {
40+
return bucket;
41+
}
42+
43+
public String getPrefix() {
44+
return prefix;
45+
}
46+
47+
public static SampleSettings load(JsonFactory jsonFactory) throws IOException {
48+
try {
49+
return jsonFactory.fromInputStream(
50+
StorageSample.class.getResourceAsStream("/sample_settings.json"), SampleSettings.class);
51+
} catch (IOException e) {
52+
IOException e2 = new IOException("Unable to read sample_settings.json: " + e.getMessage(), e);
53+
throw e2;
54+
}
55+
}
56+
}
57+

0 commit comments

Comments
 (0)