在我的应用程序中,我有一个字符串,它是文件内容。我需要在 blobstore 中创建包含此内容的新文件。我尝试像这样使用文件 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);
但是由于文件 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());
}
最佳答案
您应该写信给 Google Cloud Storage反而。 文件API是deprecated你是对的,Blobstore API 不提供直接写入的编程方式。
稍后您可以使用其自己的 API 直接从 Google Cloud Storage 读取数据,或者 您还可以使用 Blobstore API 来执行此操作 creating它的 BlobKey。
关于java - 在 GAE blobstore java 中创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35871620/