How to fetch the file list from gcs?

2019-07-12 19:19发布

问题:

Following google's Getting Started I use following code to get the list of all files in a remote directory

class GCSFileStorage {
    String bucket = "bucket_name";
    String remoteDirectoryPath = "remote/path";
    int fetchBlockSize = 1024 * 1024;
    GcsService gcsService =
      GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());

    List<String> list() {
        List<String> filenames = new List();
        ListResult listResult = gcsService.list(bucket, ListOptions.DEFAULT);
        while (listResult.hasNext()) {
            ListItem listItem = listResult.next();
            filenames += listItem.getName();
        }
        return filenames;
    }
}

GCSFileStorage gcs = new GCSFileStorage();
gcs.list();

But this code fails with an exception:

java.io.IOException: com.google.appengine.tools.cloudstorage.RetriesExhaustedException:
...
Caused by: java.io.IOException: java.lang.NullPointerException
...
Caused by: java.lang.NullPointerException
    at com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService$BlobStorageAdapter.<init>(LocalRawGcsService.java:123)
    at com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService$BlobStorageAdapter.getInstance(LocalRawGcsService.java:184)

I suspect that I somehow should authorize in gcs and this may be the reason of failure. However I haven't found proper way to init everything that gcs needs for work.

回答1:

As @ozarov mentioned the client I was using is specific for App Engine. It was added through dependency

com.google.appengine.tools:appengine-gcs-client:0.5

Instead REST API client should be used. Its dependency is

com.google.apis:google-api-services-storage:v1-rev44-1.20.0

Then the code to fetch files list may look as follows

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.Objects;
import com.google.api.services.storage.model.StorageObject;
import com.google.common.collect.Lists;

import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.LinkedList;
import java.util.List;


class GCSFileStorage {
    String bucket = "bucket_name";
    String remoteDirectoryPath = "remote/path";
    Storage storage

    public GCSFileStorage() throws GeneralSecurityException, IOException {
        storage = setupStorage();
    }

    List<String> list() throws IOException {
        List<String> allItems = new LinkedList<String>();
        Objects response = storage.objects().list(bucket).
            setPrefix(remoteDirectoryPath).execute();
        for (StorageObject obj: response.getItems()) {
            allItems.add(obj.getName());
        }
        while (response.getNextPageToken() != null) {
            String pageToken = response.getNextPageToken();
            response = storage.objects().list(bucket).
                setPrefix(remoteDirectoryPath).setPageToken(pageToken).execute();
            for (StorageObject obj: response.getItems()) {
                allItems.add(obj.getName());
            }
        }
        return allItems;
    }


    Storage setupStorage() throws GeneralSecurityException, IOException {
        GoogleCredential credential = new GoogleCredential.Builder().
            setTransport(new NetHttpTransport()).
            setJsonFactory(new JacksonFactory()).
            setServiceAccountId("your_account_id").
            setServiceAccountScopes(
                Lists.newArrayList(StorageScopes.DEVSTORAGE_FULL_CONTROL)).
            setServiceAccountPrivateKeyFromP12File(
                new File("/local/path/to/private/key.p12")).
            build();

        return new Storage.
            Builder(new NetHttpTransport(),
                new JacksonFactory(), credential).
            setApplicationName("foo").build();
    }
}


回答2:

How do you run this code? This GCS client is specific for App Engine and should run by either a deployed app or locally using the AE dev appserver or unit-tests (which should configure the AE runtime environment using LocalServiceTestHelper).