【发布时间】:2020-10-01 04:48:11
【问题描述】:
我有一个带有 Spring Boot 的 API 休息,并用它上传了一个视频,问题是当我在本地环境中运行它时,它非常适合长视频,并且实际上将它们上传到我在 gcloud 中的存储,但是当我为这种类型的视频上传到生产会引发错误 413 实体太大。
知道为什么它在本地工作而不是在生产中工作
这是控制器:
@RequestMapping(value = "/{videoId}/upload", method = RequestMethod.POST )
public ResponseEntity<Object> uploadTest(@PathVariable Long videoId, @RequestParam("file") MultipartFile file) {
try {
return ResponseEntity.ok(uploadVideo(videoId, file));
} catch (Exception e) {
return processException(e);
}
}
这个函数是从控制器调用的:
public String uploadVideo(MultipartFile fileStream, final String folder, final String bucketName) throws IOException, ServletException {
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("project.json");
storage = getInstance(resourceAsStream);
String blobName = fileStream.getName();
BlobId blobId = BlobId.of(bucketName, blobName);
byte[] content = fileStream.getBytes();
DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
DateTime dt = DateTime.now(DateTimeZone.UTC);
String dtString = dt.toString(dtf);
String extension = fileStream.getOriginalFilename().substring(fileStream.getOriginalFilename().lastIndexOf(".") + 1);
final String fileName = fileStream.getName() + dtString + "." + extension;
BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, folder + "/" + fileName)
.setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
.build();
try (WriteChannel writer = storage.writer(blobInfo)) {
try {
writer.write(ByteBuffer.wrap(content, 0, content.length));
return "https://storage.googleapis.com/" + bucketName + "/" + folder + "/" + fileName;
} catch (Exception ex) {
System.out.println(ex.toString());
throw new BadRequestException("Error uploading");
}
}
}
非常感谢您的帮助
【问题讨论】:
-
App Engine(标准?)将请求大小限制为 32MB (cloud.google.com/appengine/quotas#Requests)。虽然可以理解,但我认为假设开发服务器是生产服务的代理并不是一个好习惯。我建议您将文件直接上传到 Cloud Storage,并考虑使用通知 (cloud.google.com/storage/docs/pubsub-notifications),例如将对象的元数据排入队列,最终触发您的 App Engine 处理程序。
-
@DazWilkin 我认为您的评论是此问题的正确答案,请将您的评论移至答案