【发布时间】:2019-06-04 09:00:26
【问题描述】:
我的代码分为三个部分
- CustomDownloadProgressListener
- downloadToOutputStream() 函数,我在其中编写了从云存储下载文件的代码
- RetryHttpInitializerWrapper 一个 HttpRequestInitializer 的包装器,如 MediaHttpDownloader 类 java 文档中所建议的那样,以防响应错误或网络问题 休息。
我用来实现可恢复上传的相同 RetryHttpInitializerWrapper 并按预期工作,它以 10-10 MB 的块上传。请提出我在这里想念的东西,因为对于单个请求它可以正常工作,但对于可恢复下载它很重要。我只是在下载时断开了我的互联网以测试恢复的东西,但是当我再次连接时,它会恢复下载。
public void downloadToOutputStream(String bucketName, String objectName, OutputStream data)
throws IOException, GeneralSecurityException {
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// custom HttpRequestInitializer for automatic retry upon failures.
HttpRequestInitializer httpRequestInitializer = new RetryHttpInitializerWrapper(credential);
GenericUrl requestUrl = new GenericUrl(
"https://www.googleapis.com/storage/v1/b/" + bucketName + "/o/" + objectName);
MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport, httpRequestInitializer);
downloader.setProgressListener(new CustomDownloadProgressListener());
downloader.download(requestUrl, data);
}
public class RetryHttpInitializerWrapper implements HttpRequestInitializer {
private static final Logger LOG = Logger.getLogger(RetryHttpInitializerWrapper.class.getName());
private final Credential wrappedCredential;
private final Sleeper sleeper;
private static final int MILLIS_PER_MINUTE = 60 * 1000;
/**
* A constructor using the default Sleeper.
*
* @param wrappedCredential
* the credential used to authenticate with a Google Cloud
* Platform project
*/
public RetryHttpInitializerWrapper(Credential wrappedCredential) {
this(wrappedCredential, Sleeper.DEFAULT);
}
/**
* A constructor used only for testing.
*
* @param wrappedCredential
* the credential used to authenticate with a Google Cloud
* Platform project
* @param sleeper
* a user-supplied Sleeper
*/
RetryHttpInitializerWrapper(Credential wrappedCredential, Sleeper sleeper) {
this.wrappedCredential = Preconditions.checkNotNull(wrappedCredential);
this.sleeper = sleeper;
}
/**
* Initialize an HttpRequest.
*
* @param request
* an HttpRequest that should be initialized
*/
public void initialize(HttpRequest request) {
request.setReadTimeout(2 * MILLIS_PER_MINUTE); // 2 minutes read
// timeout
final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(
new ExponentialBackOff()).setSleeper(sleeper);
request.setInterceptor(wrappedCredential);
request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {
public boolean handleResponse(final HttpRequest request, final HttpResponse response,
final boolean supportsRetry) throws IOException {
if (wrappedCredential.handleResponse(request, response, supportsRetry)) {
// If credential decides it can handle it, the return
// code or message indicated
// something specific to authentication, and no backoff
// is desired.
return true;
} else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
// Otherwise, we defer to the judgement of our internal
// backoff handler.
System.out.println("Retrying " + request.getUrl().toString());
return true;
} else {
return false;
}
}
});
request.setIOExceptionHandler(
new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
}
public class CustomDownloadProgressListener implements MediaHttpDownloaderProgressListener {
public void progressChanged(MediaHttpDownloader downloader) {
switch (downloader.getDownloadState()) {
case MEDIA_IN_PROGRESS:
System.out.println(downloader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Download is complete!");
}
}
}
【问题讨论】:
标签: google-cloud-storage google-api-java-client