Azure Blob 存储下载:服务器无法进行身份验证,但重试时正常

发布于 2024-12-26 06:09:28 字数 4945 浏览 1 评论 0原文

我在本地 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."}

I'm having a really ODD problem using the client side azure SDK from my local PC.

Using the CloudBlobClient to upload and delete files works like charm, but when trying to download a file I am getting an authentication exception (403 Forbidden basically).
However, if I retry the same code it works FINE the next time around.
Does anyone have any idea why???

Here is some simplified code that shows the problem:

    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();
            }

        }

The exception being generated is :

    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."}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝蛊 2025-01-02 06:09:28

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

Hahahaha. Gah. I've just changed it to HTTPS and now it works fine. I should probably use SSL anyway so no worries really, but it should work with the HTTP end point non the less

暖阳 2025-01-02 06:09:28

我在使用 Windows Azure SDK for PHP 时也遇到过这个问题。您当时是否注意到您的应用程序有任何奇怪的行为? IE响应慢、主要网络延迟等

补充:
您是否也像循环一样快速创建多个连接?

I have run across this issue when using the Windows Azure SDK for PHP as well. Did you notice any strange behavior with your app at the time? I.E. slow response, major network latency, etc

Added:
also were you creating multiple connections quickly, like in a loop?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文