Resumable upload text file in Google cloud using Cloud Storage JSON API in java, facing issue of access...












0















I am using resumable upload api of Cloud Storage JSON API as mentioned in below code.



I have configured my credential related json in bash file GOOGLE_APPLICATION_CREDENTIALS = {json path}



When I am trying to access google API I am getting access forbidden error with 403 code.



Do I need to pass signed url in upload function?



RetryHttpInitializerWrapper class, I am not able to find so i Passed
HttpRequestInitializer httpRequestInitializer = null;



I am facing some issue because of request URL only that I am sure.



InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
mediaContent.setLength(mediaContent.getLength());
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}

Storage client = StorageOptions.getDefaultInstance().getService();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// custom HttpRequestInitializer for automatic retry upon failures.
HttpRequestInitializer httpRequestInitializer = null;
//HttpRequestInitializer httpRequestInitializer = new RetryHttpInitializerWrapper(credential);
GenericUrl requestUrl = new GenericUrl("https://www.googleapis.com/upload/storage/v1/b/"+bucket+"/o?uploadType=resumable&name="+name);
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
uploader.setProgressListener(new CustomProgressListener());
HttpResponse response = uploader.upload(requestUrl);
if (!response.isSuccessStatusCode()) {
throw GoogleJsonResponseException.from(JSON_FACTORY, response);
}









share|improve this question























  • You need to include a Bearer token as a header. Also, following the instructions in the documentation, a regular post request should be sufficient.

    – Christopher P
    Nov 22 '18 at 16:18











  • @ChristopherP Yes agree with documentation but I am not directly calling HttpRequest here, I am using google API class MediaHttpUploader.

    – Avish Shah
    Nov 23 '18 at 5:08
















0















I am using resumable upload api of Cloud Storage JSON API as mentioned in below code.



I have configured my credential related json in bash file GOOGLE_APPLICATION_CREDENTIALS = {json path}



When I am trying to access google API I am getting access forbidden error with 403 code.



Do I need to pass signed url in upload function?



RetryHttpInitializerWrapper class, I am not able to find so i Passed
HttpRequestInitializer httpRequestInitializer = null;



I am facing some issue because of request URL only that I am sure.



InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
mediaContent.setLength(mediaContent.getLength());
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}

Storage client = StorageOptions.getDefaultInstance().getService();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// custom HttpRequestInitializer for automatic retry upon failures.
HttpRequestInitializer httpRequestInitializer = null;
//HttpRequestInitializer httpRequestInitializer = new RetryHttpInitializerWrapper(credential);
GenericUrl requestUrl = new GenericUrl("https://www.googleapis.com/upload/storage/v1/b/"+bucket+"/o?uploadType=resumable&name="+name);
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
uploader.setProgressListener(new CustomProgressListener());
HttpResponse response = uploader.upload(requestUrl);
if (!response.isSuccessStatusCode()) {
throw GoogleJsonResponseException.from(JSON_FACTORY, response);
}









share|improve this question























  • You need to include a Bearer token as a header. Also, following the instructions in the documentation, a regular post request should be sufficient.

    – Christopher P
    Nov 22 '18 at 16:18











  • @ChristopherP Yes agree with documentation but I am not directly calling HttpRequest here, I am using google API class MediaHttpUploader.

    – Avish Shah
    Nov 23 '18 at 5:08














0












0








0








I am using resumable upload api of Cloud Storage JSON API as mentioned in below code.



I have configured my credential related json in bash file GOOGLE_APPLICATION_CREDENTIALS = {json path}



When I am trying to access google API I am getting access forbidden error with 403 code.



Do I need to pass signed url in upload function?



RetryHttpInitializerWrapper class, I am not able to find so i Passed
HttpRequestInitializer httpRequestInitializer = null;



I am facing some issue because of request URL only that I am sure.



InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
mediaContent.setLength(mediaContent.getLength());
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}

Storage client = StorageOptions.getDefaultInstance().getService();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// custom HttpRequestInitializer for automatic retry upon failures.
HttpRequestInitializer httpRequestInitializer = null;
//HttpRequestInitializer httpRequestInitializer = new RetryHttpInitializerWrapper(credential);
GenericUrl requestUrl = new GenericUrl("https://www.googleapis.com/upload/storage/v1/b/"+bucket+"/o?uploadType=resumable&name="+name);
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
uploader.setProgressListener(new CustomProgressListener());
HttpResponse response = uploader.upload(requestUrl);
if (!response.isSuccessStatusCode()) {
throw GoogleJsonResponseException.from(JSON_FACTORY, response);
}









share|improve this question














I am using resumable upload api of Cloud Storage JSON API as mentioned in below code.



I have configured my credential related json in bash file GOOGLE_APPLICATION_CREDENTIALS = {json path}



When I am trying to access google API I am getting access forbidden error with 403 code.



Do I need to pass signed url in upload function?



RetryHttpInitializerWrapper class, I am not able to find so i Passed
HttpRequestInitializer httpRequestInitializer = null;



I am facing some issue because of request URL only that I am sure.



InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
mediaContent.setLength(mediaContent.getLength());
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}

Storage client = StorageOptions.getDefaultInstance().getService();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// custom HttpRequestInitializer for automatic retry upon failures.
HttpRequestInitializer httpRequestInitializer = null;
//HttpRequestInitializer httpRequestInitializer = new RetryHttpInitializerWrapper(credential);
GenericUrl requestUrl = new GenericUrl("https://www.googleapis.com/upload/storage/v1/b/"+bucket+"/o?uploadType=resumable&name="+name);
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
uploader.setProgressListener(new CustomProgressListener());
HttpResponse response = uploader.upload(requestUrl);
if (!response.isSuccessStatusCode()) {
throw GoogleJsonResponseException.from(JSON_FACTORY, response);
}






java google-cloud-platform google-cloud-storage google-storage-api






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 12:39









Avish ShahAvish Shah

867




867













  • You need to include a Bearer token as a header. Also, following the instructions in the documentation, a regular post request should be sufficient.

    – Christopher P
    Nov 22 '18 at 16:18











  • @ChristopherP Yes agree with documentation but I am not directly calling HttpRequest here, I am using google API class MediaHttpUploader.

    – Avish Shah
    Nov 23 '18 at 5:08



















  • You need to include a Bearer token as a header. Also, following the instructions in the documentation, a regular post request should be sufficient.

    – Christopher P
    Nov 22 '18 at 16:18











  • @ChristopherP Yes agree with documentation but I am not directly calling HttpRequest here, I am using google API class MediaHttpUploader.

    – Avish Shah
    Nov 23 '18 at 5:08

















You need to include a Bearer token as a header. Also, following the instructions in the documentation, a regular post request should be sufficient.

– Christopher P
Nov 22 '18 at 16:18





You need to include a Bearer token as a header. Also, following the instructions in the documentation, a regular post request should be sufficient.

– Christopher P
Nov 22 '18 at 16:18













@ChristopherP Yes agree with documentation but I am not directly calling HttpRequest here, I am using google API class MediaHttpUploader.

– Avish Shah
Nov 23 '18 at 5:08





@ChristopherP Yes agree with documentation but I am not directly calling HttpRequest here, I am using google API class MediaHttpUploader.

– Avish Shah
Nov 23 '18 at 5:08












1 Answer
1






active

oldest

votes


















0














I have fixed this problem using authentication header.
We can set auth header when calling upload method as mentioned in code.



Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(CLIENTSECRETS_LOCATION));
InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = null;
GenericUrl genericUrl = new GenericUrl(
"https://www.googleapis.com/upload/storage/v1/b/" + bucket + "/o?uploadType=resumable&name=" + name);
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
HttpHeaders requestHeaders = new HttpHeaders();
credentials = ((GoogleCredentials) credentials).createScoped(StorageScopes.all());
Map<String, List<String>> credentialHeaders = credentials.getRequestMetadata();
if (credentialHeaders == null) {
return;
}
for (Map.Entry<String, List<String>> entry : credentialHeaders.entrySet()) {
String headerName = entry.getKey();
List<String> requestValues = new ArrayList<>();
requestValues.addAll(entry.getValue());
requestHeaders.put(headerName, requestValues);
}


HttpResponse response = uploader.setInitiationHeaders(requestHeaders).setDisableGZipContent(true)
.upload(genericUrl);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431253%2fresumable-upload-text-file-in-google-cloud-using-cloud-storage-json-api-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have fixed this problem using authentication header.
    We can set auth header when calling upload method as mentioned in code.



    Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(CLIENTSECRETS_LOCATION));
    InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    HttpRequestInitializer httpRequestInitializer = null;
    GenericUrl genericUrl = new GenericUrl(
    "https://www.googleapis.com/upload/storage/v1/b/" + bucket + "/o?uploadType=resumable&name=" + name);
    MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
    HttpHeaders requestHeaders = new HttpHeaders();
    credentials = ((GoogleCredentials) credentials).createScoped(StorageScopes.all());
    Map<String, List<String>> credentialHeaders = credentials.getRequestMetadata();
    if (credentialHeaders == null) {
    return;
    }
    for (Map.Entry<String, List<String>> entry : credentialHeaders.entrySet()) {
    String headerName = entry.getKey();
    List<String> requestValues = new ArrayList<>();
    requestValues.addAll(entry.getValue());
    requestHeaders.put(headerName, requestValues);
    }


    HttpResponse response = uploader.setInitiationHeaders(requestHeaders).setDisableGZipContent(true)
    .upload(genericUrl);





    share|improve this answer




























      0














      I have fixed this problem using authentication header.
      We can set auth header when calling upload method as mentioned in code.



      Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(CLIENTSECRETS_LOCATION));
      InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
      HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
      HttpRequestInitializer httpRequestInitializer = null;
      GenericUrl genericUrl = new GenericUrl(
      "https://www.googleapis.com/upload/storage/v1/b/" + bucket + "/o?uploadType=resumable&name=" + name);
      MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
      HttpHeaders requestHeaders = new HttpHeaders();
      credentials = ((GoogleCredentials) credentials).createScoped(StorageScopes.all());
      Map<String, List<String>> credentialHeaders = credentials.getRequestMetadata();
      if (credentialHeaders == null) {
      return;
      }
      for (Map.Entry<String, List<String>> entry : credentialHeaders.entrySet()) {
      String headerName = entry.getKey();
      List<String> requestValues = new ArrayList<>();
      requestValues.addAll(entry.getValue());
      requestHeaders.put(headerName, requestValues);
      }


      HttpResponse response = uploader.setInitiationHeaders(requestHeaders).setDisableGZipContent(true)
      .upload(genericUrl);





      share|improve this answer


























        0












        0








        0







        I have fixed this problem using authentication header.
        We can set auth header when calling upload method as mentioned in code.



        Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(CLIENTSECRETS_LOCATION));
        InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        HttpRequestInitializer httpRequestInitializer = null;
        GenericUrl genericUrl = new GenericUrl(
        "https://www.googleapis.com/upload/storage/v1/b/" + bucket + "/o?uploadType=resumable&name=" + name);
        MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
        HttpHeaders requestHeaders = new HttpHeaders();
        credentials = ((GoogleCredentials) credentials).createScoped(StorageScopes.all());
        Map<String, List<String>> credentialHeaders = credentials.getRequestMetadata();
        if (credentialHeaders == null) {
        return;
        }
        for (Map.Entry<String, List<String>> entry : credentialHeaders.entrySet()) {
        String headerName = entry.getKey();
        List<String> requestValues = new ArrayList<>();
        requestValues.addAll(entry.getValue());
        requestHeaders.put(headerName, requestValues);
        }


        HttpResponse response = uploader.setInitiationHeaders(requestHeaders).setDisableGZipContent(true)
        .upload(genericUrl);





        share|improve this answer













        I have fixed this problem using authentication header.
        We can set auth header when calling upload method as mentioned in code.



        Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(CLIENTSECRETS_LOCATION));
        InputStreamContent mediaContent = new InputStreamContent(contentType, stream);
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        HttpRequestInitializer httpRequestInitializer = null;
        GenericUrl genericUrl = new GenericUrl(
        "https://www.googleapis.com/upload/storage/v1/b/" + bucket + "/o?uploadType=resumable&name=" + name);
        MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer);
        HttpHeaders requestHeaders = new HttpHeaders();
        credentials = ((GoogleCredentials) credentials).createScoped(StorageScopes.all());
        Map<String, List<String>> credentialHeaders = credentials.getRequestMetadata();
        if (credentialHeaders == null) {
        return;
        }
        for (Map.Entry<String, List<String>> entry : credentialHeaders.entrySet()) {
        String headerName = entry.getKey();
        List<String> requestValues = new ArrayList<>();
        requestValues.addAll(entry.getValue());
        requestHeaders.put(headerName, requestValues);
        }


        HttpResponse response = uploader.setInitiationHeaders(requestHeaders).setDisableGZipContent(true)
        .upload(genericUrl);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 9:17









        Avish ShahAvish Shah

        867




        867
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431253%2fresumable-upload-text-file-in-google-cloud-using-cloud-storage-json-api-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith