2017-01-31 34 views
0

請幫助。我試圖使用BlobstoreService將視頻文件(.mp4)上傳到GCS存儲桶。允許用戶使用BlobstoreService將文件上傳並保存到我的GCS桶中,如何識別上傳的文件?

該文件已成功上載並自動保存在我的GCS Bucket中,並且客戶端接收到值爲「YES」的密鑰「upload_result」。
問題是我不知道如何識別由BlobstoreService保存在我的存儲桶中的上傳文件以及如何從請求中獲取其他信息,例如'foo'和'bar'鍵值。

Document說我可以使用BlobInfo#getGsObjectName()來獲取名稱,但似乎該方法現在不可用。
我可以從請求中獲得'blobkey',但我認爲它只適用於Blobstore而不適用於GCS。
是的,我可以得到原始文件名,但原來的名稱在GCS中丟失,而對象名稱是唯一的名稱。

com.google.appengine.api.blobstore.BlobInfo https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/blobstore/BlobInfo.html#getGsObjectName--

///// JSP /////// 
<%! 
final String BUCKT_NAME = "my_bucket"; 
final long MAX_SIZE = 1024 * 1024 * 300; 
String uploadURL; 

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 
UploadOptions uploadOptions = UploadOptions.Builder 
           .withGoogleStorageBucketName(BUCKET_NAME) 
           .maxUploadSizeBytes(MAX_SIZE); 
uploadURL = blobstoreService.createUploadUrl("/handler", uploadOptions); 
%> 


///// HTML Form /////// 
<form id="file_upload_form" action="" method="post" enctype="multipart/form-data"> 
    <input type="file" name="uploaded_file"> 
    <button type="button">UPLOAD</button> 
    <input type="hidden" name="foo" value="bar"> <-- I want to upload additional information with the video file. 
</form> 


///// ajax /////// 

function uploadFile(){ 
    var fd = new FormData($('#file_upload_form').get(0)); 
    $.ajax({ 
     url: "<%=uploadURL %>", 
     type: 'POST', 
     data: fd, 
     processData: false, 
     contentType: false, 
     dataType: 'json' 
    }) 
     .done(function(data) { 
     if(data['upload_result'] == 'YES'){ 
      //Do sometihng 
     } 
     else{ 
      //Do something 
     } 
    }); 
} 

///// SERVLET(Slim3 Controller) (/handler) /////// 

private Navigation doPost() { 
HttpServletRequest httpServletRequest = RequestLocator.get(); 
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(httpServletRequest); 
List<BlobKey> blobKeys = blobs.get("uploaded_file"); 
BlobKey fileKey = blobKeys.get(0); 
BlobInfoFactory blobInfoFactory = new BlobInfoFactory(); 
BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(fileKey); 

String originalFileName = blobInfo.getFilename(); 
long filesize = blobInfo.getSize(); 
//String gcsObjectName = blobInfo.getGsObjectName(); <<-- Most important thing is not available. 

if(blobKey!=null){ 
    String result = "{\"upload_result\":\"YES\"}"; 
     response.setCharacterEncoding("utf-8"); 
     response.setContentType("application/json"); 
     try { 
      response.getWriter().println(result); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
} 
return null; 

編輯。 使用FileInfo而不是BlobInfo來獲取生成的GCS對象名稱。這是這種情況的工作代碼。

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 
Map<String, List<FileInfo>> fileInfos = blobstoreService.getFileInfos(request); 
List<FileInfo> infos = fileInfos.get("uploaded_file"); 
FileInfo info = infos.get(0); 
String gcsObjectName = info.getGsObjectName(); // <-- 

回答

0

團塊關鍵是兩個GCS和Blob存儲的唯一標識符 - 在這裏https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage更多細節。對於你使用gcs

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 
BlobKey blobKey = blobstoreService.createGsBlobKey(
    "/gs/" + fileName.getBucketName() + "/" + fileName.getObjectName()); 
blobstoreService.serve(blobKey, resp); 
+1

已解決。在這種情況下,我應該在我的處理程序中使用'FileInfo'而不是'BlobInfo'。 –

相關問題