TL;DR Create a Credentials
instance:
Credentials credentials = GoogleCredentials.fromStream(new FileInputStream("path/to/file"));
From the documentation, there are some ways to load the credentials*, besides the one you are already using:
*Notice that these ways will be used if credentials aren’t specified through properties file
Environment variable
You can set up the environment variable GOOGLE_APPLICATION_CREDENTIALS
and use the default instance:
For Linux or Mac:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file"
For Windows:
set GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\file"
This is the easiest way to load the credentials.
Cloud SDK
Another way is to provide the credentials using Google Cloud SDK. The steps are as follows:
- First install the Cloud SDK.
- Then initialize the Cloud SDK. At step 4, select the project you are working on.
- Then you can run
gcloud auth application-default login
command.
As posted in this answer:
This obtains your credentials via a web flow and stores them in 'the well-known location for Application Default Credentials'. Now, any code/SDK you run will be able to find the credentials automatically. This is a good stand-in when you want to locally test code which would normally run on a server and use a server-side credentials file.
Before we can use Google Cloud storage, we have to create a service object. If we've already set up the GOOGLE_APPLICATION_CREDENTIALS
environment variable, we can use the default instance:
Storage storage = StorageOptions.getDefaultInstance().getService();
If we don't want to use the environment variable, we have to create a Credentials
instance and pass it to Storage
with the project name:
Credentials credentials = GoogleCredentials.fromStream(new FileInputStream("path/to/file"));
// The ID of your GCP project
// String projectId = "your-project-id";
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).setProjectId("your-project-id").build().getService();
Buckets are containers that hold objects. They can be used to organize and control data access.
Creating a bucket requires a BucketInfo
:
Bucket bucket = storage.create(BucketInfo.of("sample-bucket"));
For this simple example, we pass a bucket name and accept the default properties. Bucket names must be globally unique and also have to follow some requirements. For example, if we choose a name that is already used, create()
will fail.
Blobs are assigned a BlobId upon creation.
The easiest way to retrieve a Blob is with BlobId
:
Blob blob = storage.get(blobId);
String value = new String(blob.getContent());
We pass the id to Storage
and get the Blob
in return, and getContent()
returns the bytes.
If we don't have the BlobId
, we can search the Bucket by name:
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
Page<Blob> blobs = storage.list(bucketName);
for (Blob blob: blobs.getValues()) {
if (name.equals(blob.getName())) {
return new String(blob.getContent());
}
}
This is a code sample of a function that list all the objects in a Cloud Storage bucket.
import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
public class ListObjects {
public static void listObjects(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Page<Blob> blobs = storage.list(bucketName);
for (Blob blob : blobs.iterateAll()) {
System.out.println(blob.getName());
}
}
}
See also: