我正在尝试将google云与现有的struts2 Web应用程序集成,该应用程序本地部署到我们的服务器。你能告诉我该如何解决这个问题吗?
我只是在寻找与谷歌云中部署的应用程序集成的示例。
答案 0 :(得分:0)
Authentication Code
package com.biperf.fsr.core.googlecloud;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Collections;
import com.biperf.fsr.ui.upload.StorageServiceAccountSample;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.Preconditions;
import com.google.api.services.storage.Storage;
import com.google.common.io.Files;
public class GoogleCloudAuthentication
{
/** Global configuration of Google Cloud Storage OAuth 2.0 scope. */
private static final String STORAGE_SCOPE =
"https://www.googleapis.com/auth/devstorage.read_write";
private static Storage client;
/** Authorizes the web application to access the cloud data. */
public Storage authorize(HttpTransport httpTransport, String serviceAccountEmail, String bucketName, JsonFactory jsonFactory, String applicationName ) throws Exception {
// Check for valid setup
Preconditions.checkArgument(!serviceAccountEmail.startsWith("[["),
"Please enter your service account e-mail from the Google APIs "
+ "Console to the SERVICE_ACCOUNT_EMAIL constant in %s",
GoogleCloudAuthentication.class.getName());
Preconditions.checkArgument(!bucketName.startsWith("[["),
"Please enter your desired Google Cloud Storage bucket name "
+ "to the BUCKET_NAME constant in %s", StorageServiceAccountSample.class.getName());
String p12Content = Files.readFirstLine(new File("key.p12"), Charset.defaultCharset());
Preconditions.checkArgument(!p12Content.startsWith("Please"), p12Content);
// Build service account credential.
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountEmail)
.setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
.setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
.build();
// XML Response Set up and execute Google Cloud Storage request.
/*String URI = "https://storage.googleapis.com/" + bucketName;
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();*/
// JSON Response Set up global Storage instance.
client = new Storage.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(applicationName).build();
//return content;
return client;
}
Action Class
import com.biperf.fsr.core.googlecloud.GoogleCloudAuthentication;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Preconditions;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.StorageObject;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.nio.charset.Charset;
import java.util.Collections;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class StorageServiceAccountSample {
/** E-mail address of the service account. */
private static final String SERVICE_ACCOUNT_EMAIL = "XXXXXXXXXXXXXXX@developer.gserviceaccount.com";
/** Bucket to list. */
private static final String BUCKET_NAME = "XXXXXX";
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String APPLICATION_NAME = "XXXXXXXXXXXXXXXXXXXXXXXXX";
public static void main(String[] args) {
try {
// Proxy Settings
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("XXXXXXXXXXXXXXX", 8080));
// Instance of the HTTP transport
HttpTransport httpTransport = new NetHttpTransport.Builder().setProxy(proxy).build();
// Authorization.
GoogleCloudAuthentication gca = new GoogleCloudAuthentication();
//String content = gca.authorize(httpTransport, SERVICE_ACCOUNT_EMAIL, BUCKET_NAME, JSON_FACTORY, APPLICATION_NAME);
Storage client = gca.authorize(httpTransport, SERVICE_ACCOUNT_EMAIL, BUCKET_NAME, JSON_FACTORY, APPLICATION_NAME);
// Instantiate transformer input
/*Source xmlInput = new StreamSource(new StringReader(content));
StreamResult xmlOutput = new StreamResult(new StringWriter());
// Configure transformer
Transformer transformer = TransformerFactory.newInstance().newTransformer(); // An identity
// transformer
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
// Pretty print the output XML.
System.out.println("\nBucket listing for " + BUCKET_NAME + ":\n");
System.out.println(xmlOutput.getWriter().toString());*/
//System.exit(0);
// Get metadata about the specified bucket.
Storage.Buckets.Get getBucket = client.buckets().get(BUCKET_NAME);
getBucket.setProjection("full");
Bucket bucket = getBucket.execute();
System.out.println("name: " + BUCKET_NAME);
System.out.println("location: " + bucket.getLocation());
System.out.println("timeCreated: " + bucket.getTimeCreated());
System.out.println("owner: " + bucket.getOwner());
// List the contents of the bucket.
Storage.Objects.List listObjects = client.objects().list(BUCKET_NAME);
com.google.api.services.storage.model.Objects objects;
do {
objects = listObjects.execute();
for (StorageObject object : objects.getItems()) {
System.out.println("File Name: " + object.getName() + " (" + object.getSize() + " bytes)");
System.out.println("File Link: " + object.getSelfLink());
}
listObjects.setPageToken(objects.getNextPageToken());
} while (null != objects.getNextPageToken());
} catch (IOException e) {
System.err.println(e.getMessage());
}
catch( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
}