Google Cloud可恢复下载未发生

时间:2019-01-09 14:43:27

标签: google-cloud-storage google-api-java-client

  

我的代码分为三个部分

     
      
  • CustomDownloadProgressListener
  •   
  • downloadToOutputStream()函数,其中编写了从云存储下载文件的代码
  •   
  • RetryHttpInitializerWrapper,如MediaHttpDownloader类java doc中所建议的那样,围绕HttpRequestInitializer进行包装,以防响应不良或出现网络问题   休息。
  •   
     

相同的RetryHttpInitializerWrapper我曾经实现可恢复的上传,并且按预期的方式以10-10 MB的块上传。请在这里提出我想念的内容,因为对于单个请求,它可以正常工作,但对于可恢复的下载,它却很简单。我只是在下载时断开了Internet,以测试重新同步,但是当我再次连接时,它又恢复了下载。

    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!");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我没有在此处发布任何代码,但是最近我成功使用Range标头从Google Cloud Storage实现了可恢复的下载。

假设我有一个以以下内容开头的570字节文本文件:

The next morning Hanna distracts Dieter

如果我发送一个值为Range的{​​{1}}头,那么我将返回bytes=0-21

如果我发送一个值为The next morning Hanna的{​​{1}}头,那么我将返回Range

(注意:bytes=3-32标头使用包含的时间间隔。)

除非我的块大小大于文件大小,并且除非出现某些网络错误,否则我应该始终期望响应包含一个next morning Hanna distracts头,该头指示我刚刚下载的字节范围以及整个文件尺寸。例如,对我的第一个请求的响应将包含Range,因此我的下一个请求的Content-Range将从字节22开始,例如Content-Range: bytes 0-21/570