Please help.I'm trying to upload video file (.mp4) to GCS bucket using BlobstoreService.
The file is successfully uploaded and saved automatically in my GCS Bucket, and client received value "YES" for the key "upload_result".
The problem is that I don't know how to identify the uploaded file saved in my bucket by BlobstoreService and how to get other imformation such as 'foo'and 'bar' key-value from the request.
Document says that I can use BlobInfo#getGsObjectName() to get the name, but it seems that the method is not available now.
I can get the 'blobkey' from the request, but I think it works only for the Blobstore and not for GCS.
Yes, I can get the original file name, but the original name is lost in GCS and the object name is the only thing.
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;
EDITED. Use FileInfo instead of BlobInfo to get the generated GCS object name. Here is the working code for this case.
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(); // <--
The blob key is the unique identifier for both gcs and blobstore - more details here https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage. For gcs you use the following
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobstoreService.createGsBlobKey(
"/gs/" + fileName.getBucketName() + "/" + fileName.getObjectName());
blobstoreService.serve(blobKey, resp);