简体   繁体   English

无法访问 Cloud 中的 Google Cloud Storage 服务帐户密钥文件

[英]Unable to access Google Cloud Storage service account key file in Cloud

My target is to upload file to GCP bucket from cloudhub and I am using service account and my only option is to use Json Key file.我的目标是从 cloudhub 将文件上传到 GCP 存储桶,我正在使用服务帐户,我唯一的选择是使用 Json 密钥文件。

https://cloud.google.com/storage/docs/reference/libraries https://cloud.google.com/storage/docs/reference/libraries

https://www.linkedin.com/pulse/upload-objects-google-cloud-storage-using-client-libraries-sahu https://www.linkedin.com/pulse/upload-objects-google-cloud-storage-using-client-libraries-sahu

  1. I have referred to these 2 links to upload a file to GCP cloud storage and I am able to push the file via local but in Cloudhub I am not able to push the file to bucket.我参考了这 2 个链接将文件上传到 GCP 云存储,我可以通过本地推送文件,但在 Cloudhub 中我无法将文件推送到存储桶。 I am receiving 401 unauthorized error .In both the links a step is mentioned to set GOOGLE_APPLICATION_CREDENTIALS = "path-to-json-key" in environment variables.我收到401 未经授权的错误。在两个链接中都提到了在环境变量中设置GOOGLE_APPLICATION_CREDENTIALS = "path-to-json-key" 的步骤。 When I try passing a absolute path( /Users/..json-key ) locally it works and the file gets uploaded to bucket but when I try in cloudhub runtime manager passing ${app.home}/"json-key" as value and it fails.当我尝试在本地传递绝对路径( /Users/..json-key )时,它可以工作并且文件被上传到存储桶但是当我尝试在 cloudhub 运行时管理器中传递${app.home}/"json-key"作为值它失败了。 The file is located in src/main/resources directory and I am using java class (invoke static as mentioned in 2nd link) to upload the file.该文件位于 src/main/resources 目录中,我正在使用 java class (如第二个链接中所述调用 static 上传文件) Need some guidance regarding this.需要一些关于这方面的指导。

  2. An alternative approach I thought, is it possible somehow to use this json key file in Java class itself rather than passing it via environment variable?我认为的另一种方法是,是否有可能以某种方式在 Java class 本身中使用这个 json 密钥文件,而不是通过环境变量传递它?

  3. If at all the above two option does not work, any other approach wrt service account for app deployment in cloudhub?如果以上两个选项都不起作用,还有其他方法可以在 cloudhub 中部署应用程序的服务帐户吗? (APIkey is an approach I tried but it does not restrict APIKey to a particular bucket) (APIkey 是我尝试过的一种方法,但它并不将 APIKey 限制为特定的存储桶)

Java Class 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);
    }
}

Error Log错误日志

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

If the problem is that the file can not be located in CloudHub try instead this method to reference it as described in this KB article :如果问题是无法在 CloudHub 中找到该文件,请尝试使用此方法来引用它,如本知识库文章中所述:

${mule.home}/apps/${app.name}/json-key

Note that you should not be using System.out.println().请注意,您不应该使用 System.out.println()。 It doesn't scale and can cause issues.它不会扩展并且可能会导致问题。 Use a Log4j2 logger to log from Java code.使用 Log4j2 记录器从 Java 代码记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM