简体   繁体   中英

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.

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

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. 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. 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. 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. 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?

  3. If at all the above two option does not work, any other approach wrt service account for app deployment in cloudhub? (APIkey is an approach I tried but it does not restrict APIKey to a particular bucket)

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 :

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

Note that you should not be using System.out.println(). It doesn't scale and can cause issues. Use a Log4j2 logger to log from Java code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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