如何在Java中判断Azure文件存储中的文件是否完整?

huangapple go评论75阅读模式
英文:

How to determine if a file is complete on Azure File Storage in Java?

问题

在我们的项目中,我们正在使用Azure文件存储。可以在其中上传大文件(最大为500 MB),这些文件必须由基于Spring Boot的Java微服务(使用Azure SDK for Java)进行处理。该微服务会定期轮询目录,以查看是否上传了新文件。是否有某种方式可以确定上传的文件何时完全上传完成,而不使用像监视文件大小这样的明显解决方案呢?

英文:

For our project we are using Azure File Storage, in which large files (at most 500 MB) can be uploaded and must be processed by Java microservices (based on Spring Boot), by using Azure SDK for Java, that periodically polls the directory to see if new files have been uploaded.
Is it possible, in some ways, to determine when the uploaded file is completely uploaded, without the obvious solutions like monitoring the size?

答案1

得分: 3

很遗憾,无法直接监视文件上传何时完成(包括监视文件大小)。这是因为文件上传分为两个阶段:

  1. 首先,会创建一个特定大小的空文件。这对应于 Create File REST API 操作。
  2. 接下来,会将内容写入该文件。这对应于 Put Range REST API 操作。这是实际数据被写入文件的地方。

假设数据按顺序写入文件(即从字节0到文件大小),一个可能的方法是不断检查文件的最后“n”个字节,并查看它们是否都是非零字节。这将表示在文件末尾写入了一些数据。然而,这并不是一个绝对可靠的解决方案,因为可能存在最后“n”个字节都是真正的零的情况。

英文:

Unfortunately it is not directly possible to monitor when a file upload has been completed (including monitoring the size). This is because the file upload happens in two stages:

  1. First, an empty file of certain size is created. This maps to Create File REST API operation.
  2. Next, content is written to that file. This maps to Put Range REST API operation. This is where the actual data is written to the file.

Assuming data is written to the file in sequential order (i.e. from byte 0 to file size), one possibility would be to keep on checking last "n" number of bytes of the file and see if all of them are non-zero bytes. That would indicate some data has been written at the end of the file. Again, this is not a fool-proof solution as there may be a case where last "n" bytes are genuinely zero.

huangapple
  • 本文由 发表于 2020年5月4日 21:30:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61593428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定