Firestore import/export per API

229 Views Asked by At

I'm searching for a way to call the firestore import/export functionality programmatically from java code.

What i found so far is that the nice 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 i tried the following:

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();

Which sadly ends with missing permissions when run in app engine:

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"

I cannot get the official way to login to work, because the firestore builder does not have a method to accept a instance of AppEngineCredentials.

I already checked the python client library which also seems not support these methods (yet). Does anyone have a idea how i can either login with the old rest api or get a client library which supports these methods (some language which runs on app engine please :) )

Thanks for reading! Carsten

1

There are 1 best solutions below

0
Juan Lara On BEST ANSWER

You can adapt this Cloud Datastore example for Cloud Firestore. See how they get an access token here:

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());