Firestore import/export 每 API
Firestore import/export per API
我正在寻找一种从 java 代码以编程方式调用 firestore import/export 功能的方法。
到目前为止,我发现 firestore client library does not yet support the import/export calls. But the more low level rest/grpc api already supports them. Using the java library 我尝试了以下方法:
Firestore firestoreApi = new Firestore
.Builder(UrlFetchTransport.getDefaultInstance(), new GsonFactory(), null)
.setApplicationName(SystemProperty.applicationId.get())
.build();
GoogleFirestoreAdminV1beta2ImportDocumentsRequest importRequest = new GoogleFirestoreAdminV1beta2ImportDocumentsRequest();
importRequest.setInputUriPrefix(String.format("gs://{}/{}/", BUCKET, image));
GoogleLongrunningOperation operation = firestoreApi
.projects()
.databases()
.importDocuments("projects/" + SystemProperty.applicationId.get() + "/databases/(default)", importRequest)
.execute();
在应用引擎中 运行 时,遗憾的是缺少权限:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401
{
"code": 401,
"errors": [
{
"domain": "global",
"location": "Authorization",
"locationType": "header",
"message": "Login Required.",
"reason": "required"
}
],
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
我无法让 official way 登录工作,因为 firestore 构建器没有接受 AppEngineCredentials
.
实例的方法
我已经检查了 python 客户端库,它似乎也不支持这些方法(但)。
有谁知道我如何使用旧的 rest api 登录或获得支持这些方法的客户端库(请在应用程序引擎上使用 运行s 的一些语言 :))
感谢阅读!
卡斯滕
您可以针对 Cloud Firestore 调整此 Cloud Datastore example。在此处查看他们如何获得访问令牌:
import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
// Get an access token to authorize export request
ArrayList<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/datastore");
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
final AppIdentityService.GetAccessTokenResult accessToken =
AppIdentityServiceFactory.getAppIdentityService().getAccessToken(scopes);
connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());
我正在寻找一种从 java 代码以编程方式调用 firestore import/export 功能的方法。
到目前为止,我发现 firestore client library does not yet support the import/export calls. But the more low level rest/grpc api already supports them. Using the java library 我尝试了以下方法:
Firestore firestoreApi = new Firestore
.Builder(UrlFetchTransport.getDefaultInstance(), new GsonFactory(), null)
.setApplicationName(SystemProperty.applicationId.get())
.build();
GoogleFirestoreAdminV1beta2ImportDocumentsRequest importRequest = new GoogleFirestoreAdminV1beta2ImportDocumentsRequest();
importRequest.setInputUriPrefix(String.format("gs://{}/{}/", BUCKET, image));
GoogleLongrunningOperation operation = firestoreApi
.projects()
.databases()
.importDocuments("projects/" + SystemProperty.applicationId.get() + "/databases/(default)", importRequest)
.execute();
在应用引擎中 运行 时,遗憾的是缺少权限:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401
{
"code": 401,
"errors": [
{
"domain": "global",
"location": "Authorization",
"locationType": "header",
"message": "Login Required.",
"reason": "required"
}
],
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
我无法让 official way 登录工作,因为 firestore 构建器没有接受 AppEngineCredentials
.
我已经检查了 python 客户端库,它似乎也不支持这些方法(但)。 有谁知道我如何使用旧的 rest api 登录或获得支持这些方法的客户端库(请在应用程序引擎上使用 运行s 的一些语言 :))
感谢阅读! 卡斯滕
您可以针对 Cloud Firestore 调整此 Cloud Datastore example。在此处查看他们如何获得访问令牌:
import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
// Get an access token to authorize export request
ArrayList<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/datastore");
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
final AppIdentityService.GetAccessTokenResult accessToken =
AppIdentityServiceFactory.getAppIdentityService().getAccessToken(scopes);
connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());