Resumable upload text file in Google cloud using Cloud Storage JSON API in java, facing issue of access...
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
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 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 at 5:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
java google-cloud-platform google-cloud-storage google-storage-api
asked Nov 22 at 12:39
Avish 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 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 at 5:08
add a comment |
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 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 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 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 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 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 at 5:08
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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);
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
up vote
0
down vote
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);
add a comment |
up vote
0
down vote
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);
add a comment |
up vote
0
down vote
up vote
0
down vote
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);
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);
answered Nov 28 at 9:17
Avish Shah
867
867
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 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 at 5:08