Java Code to upload HTML file into Google Cloud Storage

Dear all forumers, My devops team had created a google cloud storage space where I will upload HTML file to the bucket. I wonder where I can find such guide on Java code.

public void uploadFile() {
    try {
        // butv3
        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
        BlobId blobId = BlobId.of(bucketName, butv3ObjectName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
        storage.createFrom(blobInfo, Paths.get(butv3FilePath));
    }
}

The machine that I run on MacOS saying that com.google.cloud.storage.StorageException: 401 Unauthorized.

Caused by: com.google.api.client.http.HttpResponseException: 401 Unauthorized

Please help. Thanks.

1 Like

Hi @nicholaswkc ,

Welcome to Google Cloud Community!

You are receiving the 401 error because the client library looks for credentials using the following set of rules:

  1. GOOGLE_APPLICATION_CREDENTIALS environment variable, pointing to a service account key JSON file path.
  2. Cloud SDK credentials gcloud auth application-default login
  3. App Engine standard environment credentials.
  4. Compute Engine credentials.

I would suggest checking out this documentation on setting up authentication. You may also check the following links for your reference:

Hope these help.

1 Like