尝试将生成的 pdf 文件上传到 Azure Blob 存储时出现 404 错误

404 Error when trying to upload a generated pdf file to Azure Blob Storage

这是我收到的错误:Microsoft.WindowsAzure.Storage.StorageException:“远程服务器返回错误:(404) 未找到。

关于以下代码:

使用 PDFSharp 生成 PDF 的第一种方法:

[Route("cpd-services/generate-generic-sla/{cpd_services_id}/{userid}")]
    public ActionResult GenerateGenericClientSLA(int cpd_services_id, int userId)
    {
        var genericSLA = m_cpdServicesRepository.GetCPDServicesGenericSubscriptionDetail(cpd_services_id, userId);

        string SLAContent = m_cpdServicesRepository.GetSLATemplateByType(CPDServicesSLAHelpers.GenericClientDraftSLA);

        SLAContent = InsertGenericSLAData(SLAContent, genericSLA);

        var SLATitle = "GenericSLA" + "-" + userId;

        PdfDocument document = PdfGenerator.GeneratePdf(SLAContent, PdfSharp.PageSize.A4);
        PdfGenerateConfig config = new PdfGenerateConfig();
        config.PageSize = PdfSharp.PageSize.A4;
        var file = File(PDF.PDFDocumentToBytes(document), "application/pdf");
        file.FileDownloadName = SLATitle.ToLower() + ".pdf";

        return UploadGenericSLA(file, userId, cpd_services_id, SLATitle);
    }

UploadGenericSLA 方法:

public JsonResult UploadGenericSLA(FileContentResult file, int userId, int CPDServicesId, string sla)
    {
        Storage storage = new Storage(Settings);

        string filename = storage.UploadPDFDocument(file, "documents/cpd-services-service-level-agreement/generic/cpd-" + CPDServicesId + "/" + sla.Trim().ToLower() + ".?");

        int result = m_cpdServicesRepository.AddCPDServicesGenericSLA(file.FileDownloadName.Trim().ToLower(), CPDServicesSLAHelpers.GenericClientDraftSLA, userId, CPDServicesId);

        if (result > 0)
        {
            TempData[CRUDResult.CRUDMessage] = $"{CRUDResult.Success}|SLA has been successfully generated";
            new TelemetryHelper { }.TrackTrace($"SLA Generation - {CPDServicesId}", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
            return Json(result);
        }
        else
        {
            TempData[CRUDResult.CRUDMessage] = $"{CRUDResult.Failed}|SLA Generation Failed";
            return Json(result);
        }
    }

这又会在我的 Storage.cs class:

上触发此方法
public string UploadPDFDocument(FileContentResult file, string filename)
    {
        return UploadPDFFile($"{Settings.StoragePath}/{Settings.Environment}", file, filename);
    }

protected string UploadPDFFile(string container, FileContentResult file, string filename)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Settings.AzureStorageConnectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(container.ToLower());


        if (filename.EndsWith(".?"))
        {
            int pos = file.FileDownloadName.LastIndexOf(".");
            filename = (filename.Substring(0, filename.Length - 1) + file.FileDownloadName.Substring(pos + 1)).ToLower();
        }

        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename.ToLower());

        blob.Properties.ContentType = "application/pdf";

        blob.SetProperties(); //This is where the request to the blob storage fails.

        blob.Metadata.Add("ContentType", "application/pdf");
        blob.Metadata.Add("Size", file.FileContents.Length.ToString());
        blob.Metadata.Add("ContentLength", file.FileContents.Length.ToString());
        blob.Metadata.Add("Filename", filename);

        if (FileExists(container, filename))
        {
            blob.CreateSnapshot();
        }

        blob.UploadFromByteArray(file.FileContents, 0, file.FileContents.Length);

        return filename;
    }

这是 FileExists 方法的代码:

protected bool FileExists(string container, string filename)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Settings.AzureStorageConnectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(container);

        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename);

        return blob.Exists();
    }

我们目前正在使用 WindowsAzure.Storage - 公司还不想升级...

如有任何帮助,我们将不胜感激

您可以排除 blob.SetProperties() 方法。

尝试一下,如果不起作用,请尝试为内容类型设置 Blobhttpheaders。 请参考 this 线程以获取@Gaurav Mantri 建议的可能解决方案。

也尝试将属性分成两步(reference)

BlobProperties blobProperties = blockblob.Properties;

blobProperties.ContentType = "application/pdf";