[英]Unable to access Google Cloud Storage service account key file in Cloud
我的目標是從 cloudhub 將文件上傳到 GCP 存儲桶,我正在使用服務帳戶,我唯一的選擇是使用 Json 密鑰文件。
https://cloud.google.com/storage/docs/reference/libraries
https://www.linkedin.com/pulse/upload-objects-google-cloud-storage-using-client-libraries-sahu
我參考了這 2 個鏈接將文件上傳到 GCP 雲存儲,我可以通過本地推送文件,但在 Cloudhub 中我無法將文件推送到存儲桶。 我收到401 未經授權的錯誤。在兩個鏈接中都提到了在環境變量中設置GOOGLE_APPLICATION_CREDENTIALS = "path-to-json-key" 的步驟。 當我嘗試在本地傳遞絕對路徑( /Users/..json-key )時,它可以工作並且文件被上傳到存儲桶但是當我嘗試在 cloudhub 運行時管理器中傳遞${app.home}/"json-key"作為值它失敗了。 該文件位於 src/main/resources 目錄中,我正在使用 java class (如第二個鏈接中所述調用 static 上傳文件) 需要一些關於這方面的指導。
我認為的另一種方法是,是否有可能以某種方式在 Java class 本身中使用這個 json 密鑰文件,而不是通過環境變量傳遞它?
如果以上兩個選項都不起作用,還有其他方法可以在 cloudhub 中部署應用程序的服務帳戶嗎? (APIkey 是我嘗試過的一種方法,但它並不將 APIKey 限制為特定的存儲桶)
Java Class
package com.mule.google.cloud.storage;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static java.nio.charset.StandardCharsets.UTF_8;
public class Upload {
public static void uploadObject(String projectId, String bucketName, String objectName, String filePath)
throws IOException {
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
storage.create(blobInfo, filePath.getBytes(UTF_8));
System.out.println(
// Logger to get success message
"File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
}
}
錯誤日志
Message : Invocation of static Method 'uploadObject(java.lang.String,java.lang.String,java.lang.String,java.lang.String)' from Class 'com.mule.google.cloud.storage.Upload' with arguments [org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider arg0, org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider arg1, org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider arg2, org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider arg3] resulted in an error.
Cause: com.google.cloud.storage.StorageException - 401 Unauthorized
Element : upload-logFile/processors/3 @ poc-mule-gcs:poc-mule-gcs.xml:300 (Invoke static)
Element DSL : <java:invoke-static method="uploadObject(java.lang.String,java.lang.String,java.lang.String,java.lang.String)" doc:name="Invoke static" doc:id="3646e20f-0fef-4f57-84f8-d1ecc0f8afd5" class="com.mule.google.cloud.storage.Upload">
<java:args>#[{
arg0: vars.projectId,
arg1: vars.bucketName,
arg2: vars.objectName,
arg3: vars.agg_msg default [],
}]</java:args>
</java:invoke-static>
Error type : JAVA:INVOCATION
FlowStack : at upload-logFile(upload-logFile/processors/3 @ poc-mule-gcs:poc-mule-gcs.xml:300 (Invoke static))
at consume-CustomerQ(consume-CustomerQ/processors/6 @ poc-mule-gcs:poc-mule-gcs.xml:270 (Flow Reference upload-logFile))
(set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
如果問題是無法在 CloudHub 中找到該文件,請嘗試使用此方法來引用它,如本知識庫文章中所述:
${mule.home}/apps/${app.name}/json-key
請注意,您不應該使用 System.out.println()。 它不會擴展並且可能會導致問題。 使用 Log4j2 記錄器從 Java 代碼記錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.