【问题标题】:Creating file in GAE blobstore java在 GAE blobstore java 中创建文件
【发布时间】:2016-03-08 15:37:33
【问题描述】:

在我的应用程序中,我有一个作为文件内容的字符串。我需要在 blobstore 中使用此内容创建新文件。我尝试像这样使用 File API:

    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile file = fileService.createNewBlobFile("text/plain");
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);

    writeChannel.write(ByteBuffer.wrap(content.getBytes()));
    writeChannel.closeFinally();
    BlobKey blobKey = fileService.getBlobKey(file);
    res.sendRedirect("/serve?blob-key=" + blobKey);

但由于不推荐使用 File API,我只收到此错误:

HTTP ERROR 500

Problem accessing /create_timetable. Reason:

The Files API is disabled. Further information: https://cloud.google.com/appengine/docs/deprecations/files_api
Caused by:

com.google.apphosting.api.ApiProxy$FeatureNotEnabledException: The Files API is disabled. Further information: https://cloud.google.com/appengine/docs/deprecations/files_api
at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.callInternal(ApiProxyLocalImpl.java:515)
at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:484)
at com.google.appengine.tools.development.ApiProxyLocalImpl$AsyncApiCall.call(ApiProxyLocalImpl.java:461)
at java.util.concurrent.Executors$PrivilegedCallable$1.run(Executors.java:533)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.concurrent.Executors$PrivilegedCallable.call(Executors.java:530)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我如何在 blobstore 中手动创建文件,因为用户只给我字符串形式的数据内容,而不是文件本身,所以我不能使用 <form action="blobstoreService.createUploadUrl("/upload") %>"<input type="file">

更新

正如@ozarov 建议的那样,我使用 Google Cloude Storage API 完成了它并编写了下面的函数。它还返回此文件的 BlobKey,以便您可以使用 Blobstore API 访问它。

private BlobKey saveFile(String gcsBucket, String fileName,
        String content) throws IOException {
    GcsFilename gcsFileName = new GcsFilename(gcsBucket, fileName);
    GcsOutputChannel outputChannel =
            gcsService.createOrReplace(gcsFileName, GcsFileOptions.getDefaultInstance());
    outputChannel.write(ByteBuffer.wrap(content.getBytes()));
    outputChannel.close();

    return blobstoreService.createGsBlobKey(
            "/gs/" + gcsFileName.getBucketName() + "/" + gcsFileName.getObjectName());
}

【问题讨论】:

    标签: java google-app-engine


    【解决方案1】:

    您应该改为写信给Google Cloud Storage。 文件 API 是 deprecated,您认为 Blobstore API 是正确的 不提供直接写入它的编程方式。

    稍后您可以使用 Google Cloud Storage 自己的 API 或 您也可以使用 Blobstore API 通过creating 为其提供 BlobKey。

    【讨论】:

      猜你喜欢
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 2012-10-28
      • 2012-03-11
      • 2011-05-02
      • 1970-01-01
      相关资源
      最近更新 更多