[英]Connect to Google Cloud Datastore from Google Managed VM (Custom/Java)?
[英]Credentials Java application connect to google cloud datastore
我正在編寫一個用於訪問我的Google數據存儲區的本地Java應用程序。 我在這里遵循了該教程, 網址為http://googlecloudplatform.github.io/gcloud-java/0.2.0/index.html
這是我的基本代碼
package myproject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import com.google.cloud.AuthCredentials;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
public class Main {
private String projID = "myID";
public static void main(String[] args) throws IOException {
System.out.println("correct");
Main test = new Main();
Datastore dstore = test.getDatastore();
KeyFactory keyFactory = dstore.newKeyFactory().kind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = Entity.builder(key)
.set("name", "John Doe")
.set("age", 30)
.build();
dstore.put(entity);
}
private Datastore getDatastore() throws IOException {
File file = new File("./resource/mycredential.json");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
System.out.println("Total file size to read (in bytes) : "
+ fis.available());
} catch (IOException e) {
e.printStackTrace();
}
DatastoreOptions options = DatastoreOptions.builder()
.projectId(projID)
.authCredentials(AuthCredentials.createForJson(fis)).build();
Datastore datastore = options.service();
return datastore;
}
}
我從雲控制台創建了JSON憑證密鑰。 運行該程序后,它顯示com.google.datastore.v1beta3.client.DatastoreFactory makeClient?:未使用任何憑據
我真的被困在這里。 如何創建正確的憑證並正確使用它? 提前致謝。
這是“ INFO”消息,它毫無意義。 我同意這令人困惑,但是每次我啟動本地程序時都會顯示此消息,該程序運行良好-連接到數據存儲區,檢索和保存數據等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.