放弃到 Azure Blob 存储的分段上传

Abandoned multi-part uploads to Azure Blob Storage

我们正在使用 Microsoft 的 Java 客户端库将数据上传到 Azure Blob 存储。首先我们创建一个用于上传的 blob:

CloudBlobContainer container = client.getContainerReference(containerName);
CloudBlockBlob blob = container.getBlockBlobReference(nativePath);

每个要上传的部分都是用这样的代码完成的:

try (InputStream inputStream = new FileInputStream(part.path.toFile())) {
  String blockId = String.format("%05d", part.index);
  String blockIdEncoded = DatatypeConverter.printBase64Binary(blockId.getBytes());
  blob.uploadBlock(blockIdEncoded, inputStream, part.size);
}

最后,使用以下方法将所有部分拼接成一个 blob:

blob.commitBlockList(blockList);

如果在调用 commitBlockList() 之前放弃此进程(由于终止或逻辑错误)会怎样?零件是否泄漏?他们最终会被垃圾收集吗?我可以在 Azure 门户的某个地方看到它们吗?

我应该如何优雅地终止这个过程,以便删除所有已上传但未使用的部分?

What happens if this process is abandoned (due to termination or logic error) before the commitBlockList() is called?

您上传但未提交的任何块都会在 Azure 存储中保留 7 天。

Are they garbage collected eventually?

是的,7 天后。

Can I see them somewhere on the Azure portal?

没有。在 Azure 门户中看不到这些块。但是,您可以通过调用 Get Block List REST API 操作(或等效的 SDK 操作)来获取 blob 的块列表。

How should I terminate this process gracefully so that all of the uploaded parts are deleted?

目前您无法删除未提交的块。解决此问题的一种方法是上传一个具有相同名称的零字节 blob 并删除该 blob。当您上传同名的 blob 时,现有的块将被删除。

您可以在此处阅读更多相关信息:https://docs.microsoft.com/en-us/rest/api/storageservices/put-block#remarks