2017-01-31 13 views
0

도와주세요. BlobstoreService를 사용하여 GCS 버킷에 비디오 파일 (.mp4)을 업로드하려고합니다.사용자가 BlobstoreService를 사용하여 GCS Bucket에 파일을 업로드하고 저장할 수 있도록 허용합니다. 업로드 한 파일을 식별하는 방법은 무엇입니까?

파일이 내 GCS Bucket에 자동으로 업로드되고 자동 저장되며 클라이언트는 키 "upload_result"에 대해 "YES"값을 받았습니다.
문제는 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; 

편집했다. BlobInfo 대신 FileInfo를 사용하여 생성 된 GCS 객체 이름을 가져옵니다. 이 경우의 작업 코드는 다음과 같습니다. 여기 https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage 자세한 내용은 -

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 저장소 모두에 대한 고유 식별자입니다. gcs의 경우 다음을 사용하십시오.

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

해결되었습니다. 이 경우 'BlobInfo'대신 'FileInfo'를 사용해야합니다. –