[英]Creating file in GAE blobstore java
在我的应用程序中,我有一个字符串,它是文件内容。 我需要在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());
}
您应该改为写Google Cloud Storage 。 不推荐使用 Files API,并且您正确地认为Blobstore API没有提供直接写入该API的编程方式。
稍后,您可以使用自己的API直接从Google Cloud Storage中读取内容,也可以通过为其创建 BlobKey来使用Blobstore API进行读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.