【问题标题】:Google Cloud Resumable download not happening谷歌云可恢复下载没有发生
【发布时间】: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


    【解决方案1】:

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

    假设我有一个 570 字节的文本文件,其开头为:

    The next morning Hanna distracts Dieter
    

    如果我发送一个带有值bytes=0-21Range 标头,那么我会返回The next morning Hanna

    如果我发送一个带有值bytes=3-32Range 标头,那么我会返回next morning Hanna distracts

    (注意:Range 标头使用包含间隔。)

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

    【讨论】:

      猜你喜欢
      • 2018-05-08
      • 2014-01-02
      • 2012-03-02
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多