0
votes

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);
    }
1
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
@ChristopherP Yes agree with documentation but I am not directly calling HttpRequest here, I am using google API class MediaHttpUploader.Avish Shah

1 Answers

0
votes

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);