1

当 SAS 密钥过期时,我能够从 StorageException 捕获“AuthenticationFailed”错误代码(在我的 catch 块中)。然后我再次调用(在 catch 块内)到 SAS 生成方法,以创建一个稍后到期的新 SAS 密钥。当我拥有新的 SAS 密钥时,我使用它来创建另一个 CloudBlockBlob 对象,以将失败的块写入 azure blob。但是,当 SAS 密钥过期时,我不断收到相同的“403 禁止”身份验证错误。为什么在我使用不同的 SAS 密钥(稍后到期)创建新的 CloudBlockBlob 对象时会发生这种情况?它与时钟偏差无关,因为我什至没有在 SAS 密钥生成中指定“StartTime”。有任何想法吗?是否有更好的做法来处理 SAS 续订?将不胜感激!下面是我的代码。注意:我使用 javascript 来分块上传,所以每次调用 WriteBlock 方法都是一个全新的状态:

/// Sends the file chunk to the Azure Blob
/// <param name = "BlockId">The id for the current block</param name>
public void WriteBlockToBlob(string BlockId)
{
    try
    {
        Blob = new CloudBlockBlob(new Uri(BlobSasUri));

        Blob.PutBlock(
            BlockId,
            File.InputStream,
            null,
            null,
            uploadBlobOptions,
            null
        );
        /***********************************************************************************************/
        /* REST API Approach */
        //string queryString = (new Uri(abm.BlobContainerSasUri)).Query;
        //abm.BlobContainer = abm.BlobContainerSasUri.Substring(0, abm.BlobContainerSasUri.Length - queryString.Length);
        //string requestUri = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}/{1}{2}&comp=block&blockid={3}",
        //    abm.BlobContainer, abm.FileName, queryString, Convert.ToBase64String(Encoding.UTF8.GetBytes(abm.BlockId)));
        //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
        //request.Method = "PUT";
        //request.ContentLength = abm.File.InputStream.Length;
        //using (Stream requestStream = request.GetRequestStream())
        //{
        //    inputStream.CopyTo(requestStream);
        //}
        //using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
        //{
        //}
        /******************************************************************************************************/
    }
    catch (StorageException stex)
    {
        if (stex.RequestInformation.ExtendedErrorInformation.ErrorCode.Equals("AuthenticationFailed"))
        {
            AzureStorageTester ast = new AzureStorageTester();
            BlobSasUri = ast.getStorageLibrarySas(File.FileName);

            // Retry writing block to blob
            Blob = new CloudBlockBlob(new Uri(BlobSasUri));
            try
            {
                Blob.PutBlock(
                    BlockId,
                    File.InputStream,
                    null,
                    null,
                    uploadBlobOptions,
                    null
                );
            }
            catch (StorageException ex)
            {
                var test = stex.RequestInformation.ExtendedErrorInformation.ErrorCode;
                throw ex;
            }

        }

        //string errMsg = ComposeAndLogStorageException(ExceptionFlag.UploadLargeFileException, stex);
        //BigFileExceptionFlag = "on";
        //throw new ApplicationException(errMsg);
    }
    catch (SystemException se)
    {
        string errMsg = ComposeAndLogSystemException(ExceptionFlag.UploadLargeFileException, se);
        BigFileExceptionFlag = "on";
        throw new ApplicationException(errMsg);
    }
    catch (Exception ex)
    {
        string errMsg = ComposeAndLogException(ExceptionFlag.UploadLargeFileException, ex);
        BigFileExceptionFlag = "on";
        throw new ApplicationException(errMsg);
    }
    finally
    {
        File.InputStream.Close();
    }
}
4

1 回答 1

0

您的调试代码正在查看旧异常而不是新异常。

改变这个:

var test = stex.RequestInformation.ExtendedErrorInformation.ErrorCode;

对此:

var test = ex.RequestInformation.ExtendedErrorInformation.ErrorCode;

并再次尝试查看新的错误代码是什么。

于 2013-09-26T01:44:47.030 回答