当前位置: 首页 > 知识库问答 >
问题:

Azure CloudBlobContainer。CreateIfNotExists返回403禁止

慕容宇
2023-03-14

我正在调用CloudBlobContainer。CreateIfNotExist(请参阅下面的FindOrCreatePrivate ateBlobContainer方法)间接来自Web API服务,但它返回以下403禁止错误消息:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The remote server returned an error: (403) Forbidden.
</ExceptionMessage>
<ExceptionType>Microsoft.WindowsAzure.Storage.StorageException</ExceptionType>
<StackTrace>
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.Exists(Boolean primaryOnly, BlobRequestOptions requestOptions, OperationContext operationContext) at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType accessType, BlobRequestOptions requestOptions, OperationContext operationContext) at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobRequestOptions requestOptions, OperationContext operationContext) at [Obfuscated].DocumentManagement.BlobStorage.BlobHelper.FindOrCreatePrivateBlobContainer(String ContainerName, String AccountConnectionString) in c:\Users\[Obfuscated]\Desktop\[ProjectNameObfuscated]Online\[Obfuscated].DocumentManagement.BlobStorage\BlobHelper.cs:line 25 at [Obfuscated].DocumentManagement.BlobStorage.BlobFileItemHandler.GetStream(Int64 FileItemId) in c:\Users\[Obfuscated]\Desktop\[ProjectNameObfuscated]Online\[Obfuscated].DocumentManagement.BlobStorage\BlobFileItemHandler.cs:line 114 at [Obfuscated].DocumentManagement.Service.Controllers.FileItemController.Get(String ServiceAuthKey, Int64 FileItemId) in c:\Users\[Obfuscated]\Desktop\[ProjectNameObfuscated]Online\[Obfuscated].DocumentManagement.Service\Controllers\FileItemController.cs:line 148 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The remote server returned an error: (403) Forbidden.
</ExceptionMessage>
<ExceptionType>System.Net.WebException</ExceptionType>
<StackTrace>
at System.Net.HttpWebRequest.GetResponse() at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)
</StackTrace>
</InnerException>
</Error>

以下是生成错误的代码

     public HttpResponseMessage Get(string ServiceAuthKey, Int64 FileItemId)
        {
            if (!CheckServiceAuthKey(ServiceAuthKey).IsSuccessStatusCode)
                return new HttpResponseMessage(HttpStatusCode.Unauthorized);

            HttpRequest request = HttpContext.Current.Request;

            FileItem fi = null;
            using (DocumentDbContext db = new DocumentDbContext())
            {
                fi = db.FileItems.Find(FileItemId);
            }


            BlobFileItemHandler fih = new BlobFileItemHandler();
            Stream s = fih.GetStream(FileItemId);


            // -------- DOWNLOAD FILE TO CLIENT -------- 

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StreamContent(s);
            //a text file is actually an octet-stream (pdf, etc)
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            //we used attachment to force download
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = fi.PublicFileName;


            return response;
        }

 public Stream GetStream(Int64 FileItemId)
        {
            CloudBlobContainer c = BlobHelper.FindOrCreatePrivateBlobContainer("[Obfuscated]-dms", AccountConnectionString);

            using (DocumentDbContext db = new DocumentDbContext())
            {
                FileItem fi = db.FileItems.Find(FileItemId);

                CloudBlockBlob blob = c.GetDirectoryReference(fi.FilePathOnServer).GetBlockBlobReference(fi.PrivateFileName);
                bool blobExists = blob.Exists();

                if (!blobExists)
                    throw new System.IO.FileNotFoundException();

                Stream stream = new MemoryStream();
                blob.DownloadToStream(stream);
                long streamlen = stream.Length;
                stream.Position = 0;
                return stream;
            }

        }

public static CloudBlobContainer FindOrCreatePrivateBlobContainer(string ContainerName, string AccountConnectionString)
        {
            Trace.TraceInformation("FindOrCreatePrivateBlobContainer '" + ContainerName + "' with connectionstring '" + AccountConnectionString + "'");
            CloudStorageAccount account = CloudStorageAccount.Parse(AccountConnectionString);
            CloudBlobClient blobClient = account.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);
            container.CreateIfNotExists();
            return container;
        }

我需要一些帮助来解决此错误的原因。我尝试了以下方法:

  1. 已检查要创建的容器的名称是否有效,并且在这种特定情况下,仅由小写字母组成(无特殊或大写字符)

请帮帮忙!

这是我启用跟踪后的错误输出:应用程序:2014-07-13T19:08:03 PID[6888]错误
Microsoft。WindowsAzure。存储StorageException:远程服务器返回错误:(403)禁止---

共有3个答案

阎辰钊
2023-03-14

如果您最近升级了WindowsAzure。存储而不是使用已弃用的WindowsAzure。StorageClient dll,那么您可能会遇到此问题,因为尚未设置Blob Request estOptions和操作上下文

使用CloudBbloContainer创建MSDN链接(如果不存在)方法

http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblobcontainer.createifnotexists.aspx

於永寿
2023-03-14

正如我们在上面的评论中所讨论的,当您开始从服务中获取403状态代码时,请确保您的密钥有效。如果密钥是通过门户或使用服务管理API重新生成的,则可以更改

芮明知
2023-03-14

这可能不是这个特定问题的解决方案,但它可能会帮助其他人。

我得到了一个403错误,很难找到解决方案。我终于发现我的开发机器的时间偏离了2个小时。当我正确设置时间时,403消失了。

Azure要求UTC时间戳在请求时间的15分钟内。

 类似资料:
  • 问题内容: 我正在尝试制作Sitecraper。我是在本地计算机上制作的,在那儿工作得很好。当我在服务器上执行相同操作时,它显示403禁止错误。我正在使用PHP简单HTML DOM解析器 。我在服务器上收到的错误是这样的: 警告:file_get_contents(http://example.com/viewProperty.html?id=7715888)[function.file- get

  • 我使用http://localhost:8080/auth/realms/{realm_name}/protocol/openid-connect/token endpoint创建令牌。 grant_type=client_credentials 客户端-id:------------- 客户端-secret:78296D38-CC82-4010-A817-65C283484E51 现在我想获得r

  • 对localhost的Ajax请求返回403错误。然而,当我更改controller requestMethod以获取并在浏览器上打开请求“url”时,它会显示从服务器返回的json数据。我希望使用这些数据填充一个下拉列表。请帮帮我。我正在使用spring security 4.0和spring MVC框架。CSRF未禁用。此外,该url在Spring得到保护。 我在这里查看了相关问题,但没有找到

  • 问题内容: 我编写了一个测试UsersController的单元测试。UsersControllerTest.findUser工作正常,但不能正常运行UsersControllerTest.insertGetModifyDelete。 在测试日志中,我可以看到POST请求与UsersController的任何方法都不匹配,但是我不明白为什么。您能帮我这个吗? 这是我其余的Java类: 我有2种方法

  • 问题内容: 我希望我的控制器在用户没有权限查看特定页面时返回正确的HTTP响应代码。 问题答案: 匆匆 如果您使用的是普通JSP视图(最常见),则只需添加 视图文件中的某处。在顶部是一个不错的地方。 详情 在MVC中,我将始终在视图中进行设置,在大多数情况下,对于Spring- MVC,请使用来呈现正确的视图以响应抛出的运行时Exception。 例如:在控制器或服务层中创建一个并将其抛出,并使异

  • 共享Api实现类,添加preauthorize-admin,查看所有用户 这是我的JUnit测试,我发送get请求并返回403错误。