Google云存储分段上传:异常编写的数据不足

时间:2016-03-26 02:07:31

标签: google-cloud-storage google-cloud-platform

使用Java客户端进行简单POC:上传文件(> 200 MB文件大小)。

2016年3月26日上午11:04:35 com.google.api.client.http.HttpRequest执行

  

警告:执行请求java.io.IOException时抛出异常:   

写的数据不足
sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3214)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:81)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:972)
    at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithoutGZip(MediaHttpUploader.java:545)
    at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:417)
    at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:336)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:427)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

public class AuthSample {

/**
 * RetryHttpInitializerWrapper will automatically retry upon RPC
 * failures, preserving the auto-refresh behavior of the Google
 * Credentials.
 */
public static class RetryHttpInitializerWrapper implements HttpRequestInitializer {


    // Intercepts the request for filling in the "Authorization"
    // header field, as well as recovering from certain unsuccessful
    // error codes wherein the Credential must refresh its token for a
    // retry.
    private final Credential wrappedCredential;

    // A sleeper; you can replace it with a mock in your test.
    private final Sleeper sleeper;

    public RetryHttpInitializerWrapper(Credential wrappedCredential) {
        this(wrappedCredential, Sleeper.DEFAULT);
    }

    // Use only for testing.
    RetryHttpInitializerWrapper(
            Credential wrappedCredential, Sleeper sleeper) {
        this.wrappedCredential = Preconditions.checkNotNull(wrappedCredential);
        this.sleeper = sleeper;
    }

    public void initialize(HttpRequest request) {
        System.out.println("initialize update");
        request.setReadTimeout(2 * 60000); // 2 minutes read timeout
        final HttpUnsuccessfulResponseHandler backoffHandler =
                new HttpBackOffUnsuccessfulResponseHandler(
                        new ExponentialBackOff())
                        .setSleeper(sleeper);
        request.setInterceptor(wrappedCredential);
        request.setUnsuccessfulResponseHandler(
                new HttpUnsuccessfulResponseHandler() {
                    public boolean handleResponse(
                            HttpRequest request,
                            HttpResponse response,
                            boolean supportsRetry) throws IOException {

                        System.out.println("handleResponse " + response.getStatusCode());

                        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());
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
        request.setIOExceptionHandler(
                new HttpBackOffIOExceptionHandler(new ExponentialBackOff())
                        .setSleeper(sleeper));
    }
}

public static void main(String[] args) throws Exception{

    Properties properties = new Properties();
    properties.load(AuthSample.class.getClassLoader().getResourceAsStream("gcp.properties"));

    File file = new File(properties.getProperty("p12_file_location"));
    //System.out.println(file.exists());

    HttpTransport httpTransport = new NetHttpTransport.Builder().doNotValidateCertificate()
            .build();



    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    final HttpUnsuccessfulResponseHandler responseHandler = new HttpUnsuccessfulResponseHandler() {
        public boolean handleResponse(HttpRequest httpRequest, HttpResponse httpResponse, boolean b) throws IOException {
            System.out.println(httpResponse.getStatusCode());
            return false;
        }
    };

    final HttpIOExceptionHandler exceptionHandler = new HttpIOExceptionHandler() {
        public boolean handleIOException(HttpRequest httpRequest, boolean b) throws IOException {
            Set<String> headers = httpRequest.getResponseHeaders().keySet();
            System.out.println(headers);
            return false;
        }
    };

    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setServiceAccountId
            (properties.getProperty("gs_account_email")).setServiceAccountPrivateKeyFromP12File(file)
            .setJsonFactory(jsonFactory)
            .setServiceAccountScopes(StorageScopes.all())
            .build();

    HttpRequestInitializer requestInitializer = new RetryHttpInitializerWrapper(credential);

    Storage storage = new Storage.Builder(httpTransport, jsonFactory, requestInitializer).setApplicationName
            (properties.getProperty("app_name"))
            .build();

    MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
        public void progressChanged(MediaHttpUploader uploader) throws IOException {
            switch (uploader.getUploadState()){
                case INITIATION_STARTED:
                    System.out.println("Initiation started");
                    break;
                case INITIATION_COMPLETE:
                    System.out.println("Initiation completed");
                    break;
                case MEDIA_IN_PROGRESS:
                    System.out.println("Media In Progress");
                    break;
                case MEDIA_COMPLETE:
                    System.out.println("Media Completed");
                    break;
            }
        }
    };


    File medaiFile = new File(properties.getProperty("file_to_upload"));
    InputStreamContent inputStreamContent = new InputStreamContent(properties.getProperty("file_type"), new
            FileInputStream(file));
    inputStreamContent.setLength(medaiFile.length());

    StorageObject storageObject = new StorageObject().setName(medaiFile.getName()).setAcl(Arrays.asList(new
            ObjectAccessControl().setEntity("allUsers").setRole("READER")));

    Storage.Objects.Insert insertRequest = storage.objects().insert(
            properties.getProperty("bucket_name"), storageObject, inputStreamContent);
    MediaHttpUploader mediaHttpUploader = insertRequest.getMediaHttpUploader();
    mediaHttpUploader.setProgressListener(progressListener);
    mediaHttpUploader.setChunkSize(MediaHttpUploader.DEFAULT_CHUNK_SIZE);
    mediaHttpUploader.setDirectUploadEnabled(false);
    insertRequest.execute();

}

/** * RetryHttpInitializerWrapper will automatically retry upon RPC * failures, preserving the auto-refresh behavior of the Google * Credentials. */ public static class RetryHttpInitializerWrapper implements HttpRequestInitializer { // Intercepts the request for filling in the "Authorization" // header field, as well as recovering from certain unsuccessful // error codes wherein the Credential must refresh its token for a // retry. private final Credential wrappedCredential; // A sleeper; you can replace it with a mock in your test. private final Sleeper sleeper; public RetryHttpInitializerWrapper(Credential wrappedCredential) { this(wrappedCredential, Sleeper.DEFAULT); } // Use only for testing. RetryHttpInitializerWrapper( Credential wrappedCredential, Sleeper sleeper) { this.wrappedCredential = Preconditions.checkNotNull(wrappedCredential); this.sleeper = sleeper; } public void initialize(HttpRequest request) { System.out.println("initialize update"); request.setReadTimeout(2 * 60000); // 2 minutes read timeout final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler( new ExponentialBackOff()) .setSleeper(sleeper); request.setInterceptor(wrappedCredential); request.setUnsuccessfulResponseHandler( new HttpUnsuccessfulResponseHandler() { public boolean handleResponse( HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException { System.out.println("handleResponse " + response.getStatusCode()); 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()); return true; } else { return false; } } }); request.setIOExceptionHandler( new HttpBackOffIOExceptionHandler(new ExponentialBackOff()) .setSleeper(sleeper)); } } public static void main(String[] args) throws Exception{ Properties properties = new Properties(); properties.load(AuthSample.class.getClassLoader().getResourceAsStream("gcp.properties")); File file = new File(properties.getProperty("p12_file_location")); //System.out.println(file.exists()); HttpTransport httpTransport = new NetHttpTransport.Builder().doNotValidateCertificate() .build(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); final HttpUnsuccessfulResponseHandler responseHandler = new HttpUnsuccessfulResponseHandler() { public boolean handleResponse(HttpRequest httpRequest, HttpResponse httpResponse, boolean b) throws IOException { System.out.println(httpResponse.getStatusCode()); return false; } }; final HttpIOExceptionHandler exceptionHandler = new HttpIOExceptionHandler() { public boolean handleIOException(HttpRequest httpRequest, boolean b) throws IOException { Set<String> headers = httpRequest.getResponseHeaders().keySet(); System.out.println(headers); return false; } }; GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setServiceAccountId (properties.getProperty("gs_account_email")).setServiceAccountPrivateKeyFromP12File(file) .setJsonFactory(jsonFactory) .setServiceAccountScopes(StorageScopes.all()) .build(); HttpRequestInitializer requestInitializer = new RetryHttpInitializerWrapper(credential); Storage storage = new Storage.Builder(httpTransport, jsonFactory, requestInitializer).setApplicationName (properties.getProperty("app_name")) .build(); MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() { public void progressChanged(MediaHttpUploader uploader) throws IOException { switch (uploader.getUploadState()){ case INITIATION_STARTED: System.out.println("Initiation started"); break; case INITIATION_COMPLETE: System.out.println("Initiation completed"); break; case MEDIA_IN_PROGRESS: System.out.println("Media In Progress"); break; case MEDIA_COMPLETE: System.out.println("Media Completed"); break; } } }; File medaiFile = new File(properties.getProperty("file_to_upload")); InputStreamContent inputStreamContent = new InputStreamContent(properties.getProperty("file_type"), new FileInputStream(file)); inputStreamContent.setLength(medaiFile.length()); StorageObject storageObject = new StorageObject().setName(medaiFile.getName()).setAcl(Arrays.asList(new ObjectAccessControl().setEntity("allUsers").setRole("READER"))); Storage.Objects.Insert insertRequest = storage.objects().insert( properties.getProperty("bucket_name"), storageObject, inputStreamContent); MediaHttpUploader mediaHttpUploader = insertRequest.getMediaHttpUploader(); mediaHttpUploader.setProgressListener(progressListener); mediaHttpUploader.setChunkSize(MediaHttpUploader.DEFAULT_CHUNK_SIZE); mediaHttpUploader.setDirectUploadEnabled(false); insertRequest.execute(); }

0 个答案:

没有答案