Google 云存储 - 从 Android 应用下载对象
Google cloud storage - Download object from Android app
我正在编写一个简单的 android 库,用于下载存储在
全球控制系统。下面的代码工作正常
Storage storage = StorageOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(ServiceAccountCredentials.fromStream(getAssets().open(JSON_CREDENTIAL)))
.build()
.getService();
Blob blob = storage.get(BlobId.of(BUCKET_NAME, BLOB_NAME));
ReadChannel readChannel = blob.reader();
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE);
fileOutputStream.close();
1) 但问题是我需要给出下载进度,我找不到上述方法的任何回调?
2) 此外,这是从 GCS 下载文件到 android 应用程序的最佳方式吗?我不能使用 firebase,因为此代码存在于库中并被许多客户端应用程序使用,而 firebase 要求所有客户端应用程序都链接到 firebase 帐户,我不想这样做。
正如 @Doug 所说,使用流可以更好地获得进度条。
您的代码将是这样的:
Storage storage = StorageOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(ServiceAccountCredentials.fromStream(getAssets().open(JSON_CREDENTIAL)))
.build()
.getService();
int BUFFER_SIZE = 64 * 1024;
Blob blob = storage.get(BlobId.of(BUCKET_NAME, BLOB_NAME));
ReadChannel readChannel = blob.reader();
byte[] content = null;
if (blob != null) {
reader = blob.reader();
ByteBuffer bytes = ByteBuffer.allocate(BUFFER_SIZE);
while (reader.read(bytes) > 0) {
bytes.flip();
content = ArrayUtils.addAll(content,bytes.array());
bytes.clear();
//Here you can add the progress bar logic
}
}
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(content);
fileOutputStream.close();
我正在编写一个简单的 android 库,用于下载存储在 全球控制系统。下面的代码工作正常
Storage storage = StorageOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(ServiceAccountCredentials.fromStream(getAssets().open(JSON_CREDENTIAL)))
.build()
.getService();
Blob blob = storage.get(BlobId.of(BUCKET_NAME, BLOB_NAME));
ReadChannel readChannel = blob.reader();
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE);
fileOutputStream.close();
1) 但问题是我需要给出下载进度,我找不到上述方法的任何回调?
2) 此外,这是从 GCS 下载文件到 android 应用程序的最佳方式吗?我不能使用 firebase,因为此代码存在于库中并被许多客户端应用程序使用,而 firebase 要求所有客户端应用程序都链接到 firebase 帐户,我不想这样做。
正如 @Doug 所说,使用流可以更好地获得进度条。
您的代码将是这样的:
Storage storage = StorageOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(ServiceAccountCredentials.fromStream(getAssets().open(JSON_CREDENTIAL)))
.build()
.getService();
int BUFFER_SIZE = 64 * 1024;
Blob blob = storage.get(BlobId.of(BUCKET_NAME, BLOB_NAME));
ReadChannel readChannel = blob.reader();
byte[] content = null;
if (blob != null) {
reader = blob.reader();
ByteBuffer bytes = ByteBuffer.allocate(BUFFER_SIZE);
while (reader.read(bytes) > 0) {
bytes.flip();
content = ArrayUtils.addAll(content,bytes.array());
bytes.clear();
//Here you can add the progress bar logic
}
}
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(content);
fileOutputStream.close();