谷歌云存储管理与Android
问题描述:
我使用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
请帮帮忙!由于
答
我能够通过复制私有密钥内部存储文件夹以解决这个问题,然后我把路径位置在private.key.path
不知道这是正确的方式,但它为我工作。
答
这里有您需要使用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
嘿,你知道如何从云存储中获取直接图像URL,以列表视图显示图像吗? – 2014-09-10 04:26:50
@LOG_TAG我没有实现了,但是我建议你使用这个[库](https://github.com/pliablematter/simple-cloud-storage)。它很好地解释了,是的,你可以从你的云存储获得文件列表。希望对你有用 – monchoz 2014-09-10 05:33:08
我不确定它是否可以从GCS文件获取公共URL,但有一种方法应该是将文件下载到内部存储,然后以列表视图显示它们。 – monchoz 2014-09-10 05:42:09