Allow users to upload and save file to my GCS Bucket using BlobstoreService, how to identify the uploaded files?
请帮助。我正在尝试使用BlobstoreService将视频文件(.mp4)上传到GCS存储桶。
文件已成功上传并自动保存到我的GCS Bucket中,客户端收到的键" upload_result"的值为" YES"。
问题是我不知道如何识别BlobstoreService保存在我的存储桶中的上传文件,以及如何从请求中获取其他信息,例如'foo'和'bar'键值。
文档说我可以使用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--
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | ///// 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对象名称。这是此情况的工作代码。
1 2 3 4 5 | 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(); // <-- |
blob密钥是gcs和blobstore的唯一标识符-有关更多详细信息,请参见https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage。对于gcs,请使用以下
1 2 3 4 | BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); BlobKey blobKey = blobstoreService.createGsBlobKey( "/gs/" + fileName.getBucketName() +"/" + fileName.getObjectName()); blobstoreService.serve(blobKey, resp); |