简体   繁体   English

在GAE Blobstore Java中创建文件

[英]Creating file in GAE blobstore java

In my application I have a string which is a file content. 在我的应用程序中,我有一个字符串,它是文件内容。 I need to create new file with this content in blobstore. 我需要在blobstore中使用此内容创建新文件。 I tryed to use File API like this: 我试图像这样使用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);

But since the File API is deprecated I only get this error: 但是由于不推荐使用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)

How can I manually create file in blobstore since user only gives me data content as string, not a file itself, so I can't use <form action="blobstoreService.createUploadUrl("/upload") %>" and <input type="file"> 我如何在blobstore中手动创建文件,因为用户仅将数据内容作为字符串而不是文件本身提供给我,所以我不能使用<form action="blobstoreService.createUploadUrl("/upload") %>"<input type="file">

Update 更新

As @ozarov suggested I did it using Google Cloude Storage API and wrote the function below. 正如@ozarov所建议的那样,我使用Google Cloude Storage API做到了,并在下面编写了函数。 It also returns BlobKey of this file so you can access it using Blobstore 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());
}

You should write to Google Cloud Storage instead. 您应该改为写Google Cloud Storage Files API is deprecated and you are correct that the Blobstore API does not provide a programmatic way to write directly to it. 不推荐使用 Files API,并且您正确地认为Blobstore API没有提供直接写入该API的编程方式。

Later you can read directly from Google Cloud Storage using its own API or you can also use the Blobstore API to do so by creating a BlobKey for it. 稍后,您可以使用自己的API直接从Google Cloud Storage中读取内容,也可以通过为其创建 BlobKey来使用Blobstore API进行读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM