2014-09-03 146 views
3

我使用pliablematter簡單的雲存儲來管理使用谷歌雲存儲文件的上傳/下載。但我不能讓它工作,有一個屬性文件與此內容:谷歌雲存儲管理與Android

project.id=0000000000 
application.name=Application Name 
[email protected] 
private.key.path=/var/key 

我知道我的project.id,aplication名稱和賬號ID,但我應該怎麼放私鑰路徑?我生成和下載的服務帳戶的私鑰,但無論哪個路徑位置我總是java.io.FileNotFoundException

而且,我應該在哪裏保存在Android應用程序的私有密鑰?

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

請幫幫忙!由於

回答

1

我能夠通過複製私有密鑰內部存儲文件夾以解決這個問題,然後我把路徑位置在private.key.path

不知道這是正確的方式,但它爲我工作。

+0

嘿,你知道如何從雲存儲中獲取直接圖像URL,以列表視圖顯示圖像嗎? – 2014-09-10 04:26:50

+0

@LOG_TAG我沒有實現了,但是我建議你使用這個[庫](https://github.com/pliablematter/simple-cloud-storage)。它很好地解釋了,是的,你可以從你的雲存儲獲得文件列表。希望對你有用 – monchoz 2014-09-10 05:33:08

+1

我不確定它是否可以從GCS文件獲取公共URL,但有一種方法應該是將文件下載到內部存儲,然後以列表視圖顯示它們。 – monchoz 2014-09-10 05:42:09

0

這裏有您需要使用that example,使其在Android的功能後作出改變。

  • 從Google Console下載private_key.p12文件並放入資產。
  • 保存cloudstorage.properties文件中的資產。

現在讓這些方法更改爲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

*請確保您有寫權限的文件在清單中授予Internet權限,並使用AsyncTask或任何類似的類在運行時調用這些方法。 – Harpreet 2017-12-19 08:46:25