【问题标题】:How to fetch the file list from gcs?如何从 gcs 中获取文件列表?
【发布时间】:2015-09-16 16:23:11
【问题描述】:

按照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 工作所需的一切。

【问题讨论】:

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


    【解决方案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();
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2018-09-15
        • 2021-08-03
        • 1970-01-01
        • 2014-01-21
        • 2019-10-06
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多