.net - 无法在 StorageException 处理程序中续订 Windows Azure SAS key

标签 .net azure exception azure-storage azure-blob-storage

当 SAS key 过期时,我能够从 StorageException 捕获“AuthenticationFailed”错误代码(在我的 catch block 中)。然后,我再次调用 SAS 生成方法(在 catch block 内),以创建一个稍后到期的新 SAS key 。当我拥有新的 SAS key 时,我使用它来创建另一个 CloudBlockBlob 对象,以将失败的 block 写入到 azure blob。但是,当 SAS key 过期时,我不断收到相同的“403 禁止”身份验证错误。当我使用不同的 SAS key (过期时间较晚)创建新的 CloudBlockBlob 对象时,为什么会发生这种情况?它与时钟偏差无关,因为我什至没有在 SAS key 生成中指定“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();
    }
}

最佳答案

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

更改此:

var test = stex.RequestInformation.ExtendedErrorInformation.ErrorCode;

对此:

var test = ex.RequestInformation.ExtendedErrorInformation.ErrorCode;

然后重试看看新的错误代码是什么。

关于.net - 无法在 StorageException 处理程序中续订 Windows Azure SAS key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18688605/

相关文章:

c# - 为什么 Enumerable.Range 被迭代两次?

c# - 可以将泛型类型列表转换为其父类(super class)型的泛型列表吗?

javascript - Application Insights 不会跟踪 Angular 应用程序中未处理的浏览器异常

azure - 如何通过图形 API 将用户管理员角色设置为 azure Active Directory 应用程序

azure - 仅为一名用户保护 Azure blob?

python - 类型错误 : writelines() argument must be a sequence of strings

java - 如何在 Android 中解决此错误? java.net.MalformedURLException : Protocol not found:

wcf - 如何确保不会出现 WCF 故障状态异常?

Java.util.NoSuchElementException

c# - 在 C#.NET 项目中使用 (ffmpeg) 可执行文件