[英]Resumable upload text file in Google cloud using Cloud Storage JSON API in java, facing issue of access forbidden with 403 error code
I am using resumable upload api of Cloud Storage JSON API as mentioned in below code. 我正在使用以下代码中提到的Cloud Storage JSON API的可恢复上传API。
I have configured my credential related json in bash file GOOGLE_APPLICATION_CREDENTIALS = {json path} 我已在bash文件GOOGLE_APPLICATION_CREDENTIALS = {json path}中配置了与凭证相关的json
When I am trying to access google API I am getting access forbidden error with 403 code. 当我尝试访问Google API时,出现403代码的访问禁止错误。
Do I need to pass signed url in upload function? 我需要在上传功能中传递签名网址吗?
RetryHttpInitializerWrapper class, I am not able to find so i Passed HttpRequestInitializer httpRequestInitializer = null; RetryHttpInitializerWrapper类,我找不到,所以我通过了HttpRequestInitializer httpRequestInitializer = null;
I am facing some issue because of request URL only that I am sure. 我只因为确定URL才遇到一些问题。
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);
}
I have fixed this problem using authentication header. 我已经使用身份验证标头修复了此问题。 We can set auth header when calling upload method as mentioned in code.
如代码中所述,我们可以在调用上载方法时设置auth标头。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.