如何从gcs获取文件列表?

时间:2015-09-16 16:23:11

标签: java google-app-engine google-cloud-storage

关注google' Getting Started我使用以下代码获取远程目录中所有文件的列表

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

但此代码失败,但有例外:

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)

我怀疑我不知何故应该在gcs中授权,这可能是失败的原因。但是,我还没有找到正确的方法来启动gcs工作所需的一切。

2 个答案:

答案 0 :(得分:1)

正如@ozarov提到的the client我使用的是专门针对App Engine的。它是通过依赖添加的

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

应该使用REST API client。它的依赖性是

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

然后,获取文件列表的代码可能如下所示

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

答案 1 :(得分:0)

如何运行此代码?此GCS客户端特定于App Engine,应由部署的应用程序运行,或者使用AE dev appserver或单元测试(应使用LocalServiceTestHelper配置AE运行时环境)在本地运行。