【问题标题】:Google Cloud Storage management with Android使用 Android 进行 Google Cloud Storage 管理
【发布时间】:2014-09-03 20:07:52
【问题描述】:

我正在使用 pliablematter simple-cloud-storage 来管理使用 Google Cloud Storage 上传/下载的文件。但我无法让它工作,有一个包含此内容的属性文件:

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

我知道我的 project.id、应用程序名称和帐户 ID,但我应该在私钥路径中输入什么?我生成并下载了服务帐户私钥,但无论在哪个路径位置,我总是得到java.io.FileNotFoundException

此外,我应该在 Android 应用程序中将私钥保存在哪里?

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

请帮忙!谢谢

【问题讨论】:

    标签: android google-cloud-storage


    【解决方案1】:

    我可以通过将私钥复制到内部存储文件夹来解决这个问题,然后我将路径位置放在 private.key.path 中

    不知道这是否正确,但它对我有用。

    【讨论】:

    • 您知道如何从云存储中获取直接图像 URL 以在列表视图中显示图像吗?
    • @LOG_TAG 我还没有做到这一点,但我建议你使用这个library。它的解释很好,是的,您可以从云存储中获取文件列表。希望对你有用
    • 我不确定是否可以从 GCS 文件中获取公共 URL,但一种方法应该是将文件下载到内部存储,然后在列表视图中显示它们。
    【解决方案2】:

    以下是使用that example 使其在android 中正常运行后需要进行的更改。

    • 从 Google 控制台下载 private_key.p12 文件并放入 Assets。
    • 将您的 cloudstorage.properties 文件也保存在 Assets 中。

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

    【讨论】:

    • * 确保您已授予写入文件权限、清单中的 Internet 权限并使用 AsyncTask 或任何类似的类在运行时调用这些方法。
    • 我们在哪里可以获得“private_key.p12”。实际上我们只有“.ppk”文件。我们如何使用它?
    • 是的,请使用 PPK 文件。以前是 P12 文件,现在 Google 提供 PPK 文件。
    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多