从科尔多瓦应用程序将映像上传到 Azure Blob 时出错

本文关键字:Azure Blob 出错 映像 应用程序 | 更新日期: 2023-09-27 18:31:10

我正在尝试从Cordova应用程序上传一个blob,但得到404。但是,SAS URL 有效,并且可以在 C# 应用程序中正常工作。请在下面找到代码:

var uriWithAccess = URL;
var xhr = new XMLHttpRequest();
xhr.onerror = fail;
xhr.onloadend = uploadCompleted;
xhr.open("POST", uriWithAccess);
xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob');
xhr.setRequestHeader('x-ms-blob-content-type','image/jpeg');
xhr.send(requestData);

任何帮助将不胜感激。我也尝试使用 $.ajax,但它也给出了 404 错误。

PS:代码运行良好,但从最近几天开始导致问题。

谢谢

莫希特·查布拉

从科尔多瓦应用程序将映像上传到 Azure Blob 时出错

是否已配置 CORS ? 也许 JS 请求失败,因为 Azure 存储中不允许执行域。

<Cors>    
      <CorsRule>
            <AllowedOrigins>http://www.contoso.com, http://www.fabrikam.com</AllowedOrigins>
            <AllowedMethods>PUT,GET</AllowedMethods>
            <AllowedHeaders>x-ms-meta-data*,x-ms-meta-target*,x-ms-meta-abc</AllowedHeaders>
            <ExposedHeaders>x-ms-meta-*</ExposedHeaders>
            <MaxAgeInSeconds>200</MaxAgeInSeconds>
    </CorsRule>
<Cors>

若要设置该配置,可以使用 azure 存储 REST API,或者更轻松地运行如下所示的简短 C# 程序:

private static void InitializeCors()
{
     // CORS should be enabled once at service startup
     // Given a BlobClient, download the current Service Properties 
     ServiceProperties blobServiceProperties = BlobClient.GetServiceProperties();
     ServiceProperties tableServiceProperties = TableClient.GetServiceProperties();
     // Enable and Configure CORS
     ConfigureCors(blobServiceProperties);
     ConfigureCors(tableServiceProperties);
            
     // Commit the CORS changes into the Service Properties
     BlobClient.SetServiceProperties(blobServiceProperties);
     TableClient.SetServiceProperties(tableServiceProperties);
}
private static void ConfigureCors(ServiceProperties serviceProperties)
{
    serviceProperties.Cors = new CorsProperties();
    serviceProperties.Cors.CorsRules.Add(new CorsRule()
    {
        AllowedHeaders = new List<string>() { "*" },
        AllowedMethods = CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Post,
        AllowedOrigins = new List<string>() { "*" },
        ExposedHeaders = new List<string>() { "*" },
        MaxAgeInSeconds = 1800 // 30 minutes
     });
}

我不确定您应该使用什么主机来启用对移动应用程序的访问,但首先您应该尝试使用所有主机。

Access-Control-Allow-Origin: *
AllowedOrigins = new List<string>() { "*" },

您可以在此处遵循详细指南:

Windows Azure Storage: Introducing CORS