Managing Google Cloud Storage Using Android

I am using flexible simple cloud storage to manage file upload / download with Google Cloud Storage. But I can't get it to work, there is a properties file with this content:

project.id=0000000000
application.name=Application Name
account.id=0000000000@developer.gserviceaccount.com
private.key.path=/var/key

      

I know my project.id, app name, and account ID, but what should I put in the private key? I have generated and loaded the private key of the service account, but no matter where in the path I always getjava.io.FileNotFoundException

Also, where should I store secret keys in Android apps?

Github project https://github.com/pliablematter/simple-cloud-storage

Please, help! Thanks to

+3


source to share


2 answers


I was able to solve this by copying the private key to the internal storage folder and then putting the path in private.key.path



Don't know if this is correct, but it worked for me.

+1


source


Here are the changes you need to make after using this example to make it workable in android.

  • Download the private_key.p12 file from the Google Console and place it in Assets.
  • Save the cloudstorage.properties file in assets.


Now change these methods in the class CloudStorage

.

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;
    }

      

0


source







All Articles