c# - Azure Blob 存储下载 : Server failed to authenticate,,但重试后正常

标签 c# azure blobstorage

我在本地 PC 上使用客户端 azure SDK 时遇到了一个非常奇怪的问题。

使用 CloudBlobClient 上传和删除文件的工作方式与 Charm 类似,但是当尝试下载文件时,我收到身份验证异常(基本上是 403 Forbidden)。 但是,如果我重试相同的代码,下次它会正常工作
有谁知道为什么吗???

下面是一些显示问题的简化代码:

    string blobEndpoint = "http://???.blob.core.windows.net";
    string accountName = "???";
    string accountKey = "???";

        while(retry)
        {
            FileStream stream = new FileStream("C:\\temp\\mydownload.jpg", FileMode.Append);

            try
            {
                CloudBlobClient blobClient =
                new CloudBlobClient(new Uri(blobEndpoint), new StorageCredentialsAccountAndKey(accountName, accountKey));

                CloudBlob blob = blobClient.GetBlobReference("documents/drive.png");

                blob.DownloadToStream(stream);

                retry = false;
            }
            catch (Exception ee)
            {
                if (MessageBox.Show(ee.Message.ToString(), "ARGH!", MessageBoxButtons.RetryCancel) != System.Windows.Forms.DialogResult.Retry)
                    retry = false;
            }
            finally
            {
                stream.Flush();
                stream.Close();
            }

        }

生成的异常是:

    Microsoft.WindowsAzure.StorageClient.StorageClientException was unhandled
  Message=Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
  Source=Microsoft.WindowsAzure.StorageClient
  StackTrace:
       at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.get_Result()
       at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.Execute()
       at Microsoft.WindowsAzure.StorageClient.RequestWithRetry.RequestWithRetrySyncImpl[TResult](ShouldRetry retryOracle, SynchronousTask`1 syncTask)
       at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteSyncTaskWithRetry[TResult](SynchronousTask`1 syncTask, RetryPolicy policy)
       at Microsoft.WindowsAzure.StorageClient.CloudBlob.DownloadToStream(Stream target, BlobRequestOptions options)
       at Microsoft.WindowsAzure.StorageClient.CloudBlob.DownloadToStream(Stream target)
       at TestAzure.Form1.button1_Click(Object sender, EventArgs e) in c:\users\richard.donaldson\documents\visual studio 2010\Projects\TestAzure\TestAzure\Form1.cs:line 41
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at TestAzure.Program.Main() in c:\users\richard.donaldson\documents\visual studio 2010\Projects\TestAzure\TestAzure\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Net.WebException
       Message=The remote server returned an error: (403) Forbidden.
       Source=System
       StackTrace:
            at System.Net.HttpWebRequest.GetResponse()
            at Microsoft.WindowsAzure.StorageClient.EventHelper.ProcessWebResponseSync(WebRequest req, EventHandler`1 handler, Object sender)
       InnerException: {"The remote server returned an error: (403) Forbidden."}

最佳答案

哈哈哈哈。嘎。我刚刚将其更改为 HTTPS,现在它工作正常。无论如何,我可能应该使用 SSL 所以不用担心,但它应该与 HTTP 端点一起使用

关于c# - Azure Blob 存储下载 : Server failed to authenticate,,但重试后正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8807366/

相关文章:

c# - 为什么 gzip/deflate 压缩小文件会导致许多尾随零?

c# - 如何使用 Azure.Storage.Blobs BlobClient 检索 Blob 目录路径中的 Blob?

Azure CloudPageBlob Create - 如何实现 CreateIfNotExist

java - 如何通过java代码访问和创建azure存储帐户的生命周期规则/生命周期管理策略

c# - 使用 OR 条件断言

c# - 当重定向未知时为 HttpWebRequest.Credentials 构建 CredentialCache

c# - .NET 中的方法是否也与数据成员一起序列化?

mysql - 如何在门户中授予对 Azure Database for MySQL 的访问权限?

azure - 通过服务总线消息传递使用 Azure 服务结构的用例有哪些?

python - azure devops python 获取工作项的所有链接(子/父)