我正在尝试在Google App Engine上创建一个REST API,允许将视频上传到Blobstore。在我的本地开发服务器上一切正常,但在部署到GAE时失败。在GAE上它存储视频,但不调用我的回调URL。相反,它返回302,其中位置标头设置为我的回调URL。我尝试从我的客户端手动访问回调URL,但GAE然后抛出异常:java.lang.IllegalStateException: Must be called from a blob upload callback request
。我正在使用Spring MVC。
/**
* URL has been generated by going to /game/{gameId}/video
*/
private void uploadVideo(String uploadUrl) throws Exception {
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
HttpPost post = new HttpPost(uploadUrl);
MultipartEntity mp = new MultipartEntity();
File video = new File("src/test/resources/video.mp4");
assertTrue("Couldn't find video file.", video.exists());
mp.addPart("video", new FileBody(video));
post.setEntity(mp);
HttpResponse resp = httpClient.execute(post);
//Getting 302 here.
assertEquals("Unexpected HTTP status code.", 200, resp.getStatusLine().getStatusCode());
}
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
/**
* Create a video entity and generate an upload URL.
*/
@RequestMapping(value = "/game/{gameId}/video", method=RequestMethod.POST)
@ResponseBody
public VideoMeta createVideo(@PathVariable Long gameId, @RequestBody VideoMeta meta, Principal auth) {
//Omitted authentication and video creation logic
//Get blobstore upload URL. Callback is /video/{videoId}/blob.
String uploadUrl = blobstoreService.createUploadUrl("/video/" + video.getId() + "/blob");
//Create response
VideoMeta videoMeta = new VideoMeta();
videoMeta.setVideoId(video.getId());
videoMeta.setUploadUrl(uploadUrl);
return videoMeta;
}
/**
* This method is never called on GAE!
*/
@RequestMapping(value = "/video/{videoId}/blob", method=RequestMethod.POST)
@ResponseBody
public VideoMeta uploadVideo(@PathVariable Long videoId, HttpServletRequest req) {
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
List<BlobKey> blobKeys = blobs.get("video");
blobstoreService.getByteRange(req);
int numBlobs = blobKeys.size();
if(numBlobs != 1) {
throw new RestException(HttpServletResponse.SC_NOT_ACCEPTABLE, "You should upload exactly one blob.");
}
BlobKey blobKey = blobKeys.get(0);
//Omitted video update logic
VideoMeta videoMeta = new VideoMeta();
videoMeta.setVideoId(video.getId());
return videoMeta;
}