尝试从Play应用程序中删除Google Cloud Storage Bucket中的所有文件会导致线程关闭ActorSystem导致未捕获的致命错误

时间:2018-10-17 15:30:27

标签: java playframework google-api

我有一个在Google App Engine上运行的Play Framework应用程序,并且有一个尝试删除Google Cloud Platform(GCP)存储桶中所有文件的过程。

当我运行此过程时,它将产生以下错误: a.a.ActorSystemImpl - Uncaught fatal error from thread [application-akka.actor.default-dispatcher-91] shutting down ActorSystem [application]

这是我用来运行此过程的代码:

private static final String BUCKET_NAME = Configuration.root().getString("google.storage.bucket.name");
public static final Boolean debugOn = true; // Set to true to log...

public static void removeAllFilesFromBucket() throws Exception {
    if (debugOn == true) {
        Logger.info("GoogleControl.removeAllFilesFromBucket: Starting...");
    }

    String credentialsFileName = "";
    Storage storage = null;

    try {
        if (debugOn == true) {
            Logger.info("GoogleControl.removeAllFilesFromBucket: Getting credentialsFileName path...");
        }
        credentialsFileName = Configuration.root().getString("google.storage.credentials.file");
        if (debugOn == true) {
            Logger.info("GoogleControl.removeAllFilesFromBucket: credentialsFileName = " + credentialsFileName);
        }

        if (debugOn == true) {
            Logger.info("GoogleControl.removeAllFilesFromBucket: Setting InputStream...");
        }
        InputStream in = GoogleControl.class.getClassLoader().getResourceAsStream(credentialsFileName);
        if (in == null) {
            if (debugOn == true) {
                Logger.info("GoogleControl.removeAllFilesFromBucket: InputStream is null");
            }
        }
        if (debugOn == true) {
            Logger.info("GoogleControl.removeAllFilesFromBucket: InputStream set...");
        }

        try {
            storage = StorageOptions.newBuilder().setCredentials(ServiceAccountCredentials.fromStream(in)).build()
                    .getService();
        } catch (Exception se) {
            System.out.println("--- START ERROR WITH SETTING STORAGE OBJECT ---");
            se.printStackTrace();
            System.out.println("--- END ERROR WITH SETTING STORAGE OBJECT ---");
        }

        try {
            if (debugOn == true) {
                Logger.info("GoogleControl.removeAllFilesFromBucket: Cycling through blob objects...");
            }

            String blobName = "";
            if (debugOn == true) {
                Logger.info("GoogleControl.removeAllFilesFromBucket: Setting Page<Blob> blobs...");
            }
            Page<Blob> blobs = storage.list(BUCKET_NAME, BlobListOption.currentDirectory(),
                    BlobListOption.prefix(""));
            if (debugOn == true) {
                Logger.info("GoogleControl.removeAllFilesFromBucket: Set Page<Blob> blobs...");
            }
            for (Blob blob : blobs.iterateAll()) {
                // do something with the blob
                blobName = blob.getName();
                if (debugOn == true) {
                    Logger.info("GoogleControl.removeAllFilesFromBucket: blobName = " + blobName);
                }
                BlobId blobId = BlobId.of(BUCKET_NAME, blobName);
                boolean deleted = storage.delete(blobId);
                if (deleted) {
                    // the blob was deleted
                    if (debugOn == true) {
                        Logger.info("GoogleControl.removeAllFilesFromBucket: Blob is deleted...");
                    }
                } else {
                    // the blob was not found
                    if (debugOn == true) {
                        Logger.info("GoogleControl.removeAllFilesFromBucket: Blob is not found/not deleted...");
                    }
                }
            }
            if (debugOn == true) {
                Logger.info("GoogleControl.removeAllFilesFromBucket: Blob Object set...");
            }
        } catch (Exception se) {
            System.out.println("--- START ERROR WITH SETTING BLOB OBJECT ---");
            se.printStackTrace();
            System.out.println("--- END ERROR WITH SETTING BLOB OBJECT ---");
        }

    } catch (Exception ex) {
        System.out.println("--- START ERROR SENDFILETOBUCKET ---");
        ex.printStackTrace();
        System.out.println("--- END ERROR SENDFILETOBUCKET ---");
    }
}

输出如下:

application - GoogleControl.removeAllFilesFromBucket: Starting...

application - GoogleControl.removeAllFilesFromBucket: Getting credentialsFileName path...

application - GoogleControl.removeAllFilesFromBucket: credentialsFileName = ptp_test_default_service_account.json

application - GoogleControl.removeAllFilesFromBucket: Setting InputStream...

application - GoogleControl.removeAllFilesFromBucket: InputStream set...

application - GoogleControl.removeAllFilesFromBucket: Cycling through blob objects...

application - GoogleControl.removeAllFilesFromBucket: Setting Page<Blob> blobs...

 a.a.ActorSystemImpl - Uncaught fatal error from thread [application-akka.actor.default-dispatcher-91] shutting down ActorSystem [application]

在尝试设置Blob对象之前,它似乎已到达该行。我正在使用以下示例:

https://cloud.google.com/storage/docs/listing-objects

https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java

我发现了有关此错误的一些帖子,但没有任何帮助:

https://github.com/playframework/play-java-starter-example/issues/65

https://groups.google.com/forum/#!topic/akka-user/R9utH4VIeXU

我正在寻求有关解决此问题以及代码方面的帮助-可能不正确。

0 个答案:

没有答案