簡體   English   中英

在GAE Blobstore Java中創建文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM