I have a problem using the Google Cloud Storage in my app, basically where I'm failing is retrieving the GoogleCredential from this line, that is giving me a NullPointerException =
GoogleCredential credential = new GoogleCredential.Builder()
I'm trying to get the Google Application Default Credentials, but I don´t have to a Google Compute Engine, I would prefer using Google App Engine.
I'm using that line of code for making this method that is the responsable for the uploading and downloading the file =
private static Storage getStorage() {
if (storage == null) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
try {
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(
getProperties().getProperty(ACCOUNT_ID_PROPERTY))
.setServiceAccountPrivateKeyFromP12File(
new File(getProperties().getProperty(
PRIVATE_KEY_PATH_PROPERTY)))
.setServiceAccountScopes(scopes).build();
storage = new Storage.Builder(httpTransport, jsonFactory,
credential).setApplicationName(
getProperties().getProperty(APPLICATION_NAME_PROPERTY))
.build();
} catch (Exception e) {
e.printStackTrace();
}
}
return storage;
}
to add more into the question, after debugging I noticed that the line that is giving me the nullPointerException is in this method =
private static Properties getProperties() throws Exception {
if (properties == null) {
properties = new Properties();
InputStream stream = CloudStorage.class
.getResourceAsStream("/cloudstorage.properties");
try {
properties.load(stream);
} catch (IOException e) {
throw new RuntimeException(
"cloudstorage.properties must be present in classpath",
e);
} finally {
stream.close();
}
}
return properties;
}
specifically this line of code =
InputStream stream = CloudStorage.class
.getResourceAsStream("/cloudstorage.properties");