Typescript 如何捕获Azure blob存储中的上载失败暂时错误?
我有以下从前端typescipt代码上传文件到Azure blob存储的工作代码。 由于网络或任何其他问题,我必须捕获与上传失败场景相关的错误。我们如何在前端捕获这些错误? 假设我正在上传一个1GB大小的文件,在上传过程中如果有任何网络问题,那么一旦网络可用,它会继续还是从开始重新开始上传Typescript 如何捕获Azure blob存储中的上载失败暂时错误?,typescript,azure,error-handling,transactions,azure-blob-storage,Typescript,Azure,Error Handling,Transactions,Azure Blob Storage,我有以下从前端typescipt代码上传文件到Azure blob存储的工作代码。 由于网络或任何其他问题,我必须捕获与上传失败场景相关的错误。我们如何在前端捕获这些错误? 假设我正在上传一个1GB大小的文件,在上传过程中如果有任何网络问题,那么一旦网络可用,它会继续还是从开始重新开始上传 const blobServiceClient = new BlobServiceClient(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,
const blobServiceClient = new BlobServiceClient(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,credentials);
const containerClient = blobServiceClient.getContainerClient(containerName);
const fileName = path.basename(localFilePath);
const blobClient = containerClient.getBlobClient("folder1/"+fileName);
//const blobClient = containerClient.getBlobClient(fileName);
const blockBlobClient = blobClient.getBlockBlobClient();
blockBlobClient.uploadFile(localFilePath);
由于以下原因,我必须捕获与上载失败场景相关的错误:
网络或任何其他问题。我们怎样才能抓住前面的错误呢
结束
您可以简单地将代码包装在try/catch块中。每当存储端发生错误时,就会返回一个错误对象。该错误对象将包含一个名为statusCode
的属性。您可以检查状态代码以确定是否应重试该操作。例如,如果状态代码>=400且小于500,则不应重试该操作。可以重试任何状态代码>=500
假设我上传的文件大小为1GB,在上传过程中如果
有任何网络问题,那么一旦网络可用,会吗
恢复或上传将从开始重新开始
const blobServiceClient = new BlobServiceClient(`https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,credentials);
const containerClient = blobServiceClient.getContainerClient(containerName);
const fileName = path.basename(localFilePath);
const blobClient = containerClient.getBlobClient("folder1/"+fileName);
//const blobClient = containerClient.getBlobClient(fileName);
const blockBlobClient = blobClient.getBlockBlobClient();
blockBlobClient.uploadFile(localFilePath);
SDK不会恢复失败的上载。它将从头开始。您的应用程序有责任维护上载状态