|
27 | 27 | import com.google.api.client.util.Preconditions;
|
28 | 28 | import com.google.common.io.Files;
|
29 | 29 |
|
30 |
| -import java.io.File; |
31 | 30 | import java.io.IOException;
|
32 | 31 | import java.io.StringReader;
|
33 | 32 | import java.io.StringWriter;
|
34 | 33 | import java.nio.charset.Charset;
|
35 | 34 | import java.util.Collections;
|
| 35 | +import java.net.URLEncoder; |
36 | 36 |
|
37 | 37 | import javax.xml.transform.OutputKeys;
|
38 | 38 | import javax.xml.transform.Source;
|
|
43 | 43 |
|
44 | 44 | public class StorageServiceAccountSample {
|
45 | 45 |
|
46 |
| - /** E-mail address of the service account. */ |
47 |
| - private static final String SERVICE_ACCOUNT_EMAIL = "[[INSERT_SERVICE_ACCOUNT_EMAIL_HERE]]"; |
48 |
| - |
49 |
| - /** Bucket to list. */ |
50 |
| - private static final String BUCKET_NAME = "[[INSERT_YOUR_BUCKET_NAME_HERE]]"; |
51 |
| - |
52 | 46 | /** Global configuration of Google Cloud Storage OAuth 2.0 scope. */
|
53 | 47 | private static final String STORAGE_SCOPE =
|
54 | 48 | "https://www.googleapis.com/auth/devstorage.read_write";
|
55 | 49 |
|
56 | 50 | /** Global instance of the HTTP transport. */
|
57 | 51 | private static HttpTransport httpTransport;
|
58 | 52 |
|
59 |
| - /** Global instance of the JSON factory. */ |
60 |
| - private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); |
61 |
| - |
62 | 53 | public static void main(String[] args) {
|
63 | 54 | try {
|
64 |
| - try { |
65 |
| - httpTransport = GoogleNetHttpTransport.newTrustedTransport(); |
66 |
| - // Check for valid setup. |
67 |
| - Preconditions.checkArgument(!SERVICE_ACCOUNT_EMAIL.startsWith("[["), |
68 |
| - "Please enter your service account e-mail from the Google APIs " |
69 |
| - + "Console to the SERVICE_ACCOUNT_EMAIL constant in %s", |
70 |
| - StorageServiceAccountSample.class.getName()); |
71 |
| - Preconditions.checkArgument(!BUCKET_NAME.startsWith("[["), |
72 |
| - "Please enter your desired Google Cloud Storage bucket name " |
73 |
| - + "to the BUCKET_NAME constant in %s", StorageServiceAccountSample.class.getName()); |
74 |
| - String p12Content = Files.readFirstLine(new File("key.p12"), Charset.defaultCharset()); |
75 |
| - Preconditions.checkArgument(!p12Content.startsWith("Please"), p12Content); |
76 |
| - |
77 |
| - //[START snippet] |
78 |
| - // Build a service account credential. |
79 |
| - GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport) |
80 |
| - .setJsonFactory(JSON_FACTORY) |
81 |
| - .setServiceAccountId(SERVICE_ACCOUNT_EMAIL) |
82 |
| - .setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE)) |
83 |
| - .setServiceAccountPrivateKeyFromP12File(new File("key.p12")) |
84 |
| - .build(); |
85 |
| - |
86 |
| - // Set up and execute a Google Cloud Storage request. |
87 |
| - String URI = "https://storage.googleapis.com/" + BUCKET_NAME; |
88 |
| - HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); |
89 |
| - GenericUrl url = new GenericUrl(URI); |
90 |
| - HttpRequest request = requestFactory.buildGetRequest(url); |
91 |
| - HttpResponse response = request.execute(); |
92 |
| - String content = response.parseAsString(); |
93 |
| - //[END snippet] |
94 |
| - |
95 |
| - // Instantiate transformer input. |
96 |
| - Source xmlInput = new StreamSource(new StringReader(content)); |
97 |
| - StreamResult xmlOutput = new StreamResult(new StringWriter()); |
98 |
| - |
99 |
| - // Configure transformer. |
100 |
| - Transformer transformer = TransformerFactory.newInstance().newTransformer(); // An identity |
101 |
| - // transformer |
102 |
| - transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); |
103 |
| - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
104 |
| - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
105 |
| - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
106 |
| - transformer.transform(xmlInput, xmlOutput); |
107 |
| - |
108 |
| - // Pretty print the output XML. |
109 |
| - System.out.println("\nBucket listing for " + BUCKET_NAME + ":\n"); |
110 |
| - System.out.println(xmlOutput.getWriter().toString()); |
111 |
| - System.exit(0); |
112 |
| - |
113 |
| - } catch (IOException e) { |
| 55 | + httpTransport = GoogleNetHttpTransport.newTrustedTransport(); |
| 56 | + // Check for valid setup. |
| 57 | + Preconditions.checkArgument(args.length == 1, |
| 58 | + "Please pass in the Google Cloud Storage bucket name to display"); |
| 59 | + String bucketName = args[0]; |
| 60 | + |
| 61 | + //[START snippet] |
| 62 | + // Build a service account credential. |
| 63 | + GoogleCredential credential = GoogleCredential.getApplicationDefault() |
| 64 | + .createScoped(Collections.singleton(STORAGE_SCOPE)); |
| 65 | + |
| 66 | + // Set up and execute a Google Cloud Storage request. |
| 67 | + String URI = "https://storage.googleapis.com/" + URLEncoder.encode(bucketName, "UTF-8"); |
| 68 | + HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); |
| 69 | + GenericUrl url = new GenericUrl(URI); |
| 70 | + HttpRequest request = requestFactory.buildGetRequest(url); |
| 71 | + HttpResponse response = request.execute(); |
| 72 | + String content = response.parseAsString(); |
| 73 | + //[END snippet] |
| 74 | + |
| 75 | + // Instantiate transformer input. |
| 76 | + Source xmlInput = new StreamSource(new StringReader(content)); |
| 77 | + StreamResult xmlOutput = new StreamResult(new StringWriter()); |
| 78 | + |
| 79 | + // Configure transformer. |
| 80 | + Transformer transformer = TransformerFactory.newInstance().newTransformer(); // An identity |
| 81 | + // transformer |
| 82 | + transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); |
| 83 | + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 84 | + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 85 | + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
| 86 | + transformer.transform(xmlInput, xmlOutput); |
| 87 | + |
| 88 | + // Pretty print the output XML. |
| 89 | + System.out.println("\nBucket listing for " + bucketName + ":\n"); |
| 90 | + System.out.println(xmlOutput.getWriter().toString()); |
| 91 | + System.exit(0); |
| 92 | + |
| 93 | + } catch (IOException e) { |
114 | 94 | System.err.println(e.getMessage());
|
115 |
| - } |
116 | 95 | } catch (Throwable t) {
|
117 | 96 | t.printStackTrace();
|
118 | 97 | }
|
|
0 commit comments