Reputation: 355
I'm using pliablematter simple-cloud-storage to manage uploads/downloads of files using Google Cloud Storage. But I'm not able to make it work, there's a property file with this content:
project.id=0000000000
application.name=Application Name
[email protected]
private.key.path=/var/key
I know my project.id, aplication name and account id but what should I put in private key path?? I generated and downloaded the service account private key but no matter which path location I always get java.io.FileNotFoundException
Moreover, where should I save private keys in Android applications?
Github project https://github.com/pliablematter/simple-cloud-storage
Please help! Thanks
Upvotes: 3
Views: 523
Reputation: 3070
Here are the changed which you need to make after using that example to make it functional in android.
Now make these method changes in CloudStorage
class.
private Properties getProperties() throws Exception {
if (properties == null) {
properties = new Properties();
AssetManager manager = context.getAssets();
InputStream stream = manager.open("cloudstorage.properties");
try {
properties.load(stream);
} catch (IOException
e) {
throw new RuntimeException(
"cloudstorage.properties must be present in classpath",
e);
} finally {
if (stream != null)
stream.close();
}
}
return properties;
}
private Storage getStorage() throws Exception {
if (storage == null) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
Credential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(
getProperties().getProperty(ACCOUNT_ID_PROPERTY))
.setServiceAccountPrivateKeyFromP12File(getPrivateKeyFile())
.setServiceAccountScopes(scopes).build();
storage = new Storage.Builder(httpTransport, jsonFactory,
credential).setApplicationName(
getProperties().getProperty(APPLICATION_NAME_PROPERTY))
.build();
}
return storage;
}
private File getPrivateKeyFile() {
File f = new File(context.getCacheDir() + “/my_private_key.p12");
if (!f.exists())
try {
InputStream is = context.getAssets().open(“private_key.p12");
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[4 * 1024];
int read;
while ((read = is.read(buffer)) != -1)
fos.write(buffer, 0, read);
fos.flush();
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("FILE", "FETCHED FILE:: " + f.getAbsolutePath() + " with data: " + f.length());
return f;
}
Upvotes: 0
Reputation: 355
I was able to solve this by copying the private key to an internal storage folder, then I put the path location in private.key.path
Don't know if this is the right way but it worked for me.
Upvotes: 1