如何在 Azure 上存储 pdf

Gre*_*Gum 6 azure azure-storage azure-web-app-service

我有一个 WordPress on Linux App,它在 Azure 上运行,带有 MySql 数据库。

我需要能够将 PDF 文件上传到 Azure,然后在网站中有一个链接,使用户可以单击该链接并查看 PDF。

更具体地说,该文档是在本地创建然后上传到 Azure 的月度发票。用户将登录,然后看到一个链接,允许他查看发票。

我不知道文档应该如何存储。它应该存储在MySql数据库中吗?或者在可以链接到的某种类型的存储中?当然,它需要是安全的。

VAT*_*VAT 0

可以使用 Azure Blob 存储来存储任何文件类型。

如下所示,获取任何文件的文件名、文件流、MimeType 和文件数据。

        var fileName = Path.GetFileName(@"C:\ConsoleApp1\Readme.pdf");
        var fileStream = new FileStream(fileName, FileMode.Create);
        string mimeType = MimeMapping.MimeUtility.GetMimeMapping(fileName);
        byte[] fileData = new byte[fileName.Length];

        objBlobService.UploadFileToBlobAsync(fileName, fileData, mimeType);
Run Code Online (Sandbox Code Playgroud)

下面是上传文件到Azure Blob的主要方法

    private async Task<string> UploadFileToBlobAsync(string strFileName, byte[] fileData, string fileMimeType)
    {
        // access key will be available from Azure blob - "DefaultEndpointsProtocol=https;AccountName=XXX;AccountKey=;EndpointSuffix=core.windows.net"
        CloudStorageAccount csa = CloudStorageAccount.Parse(accessKey);
        CloudBlobClient cloudBlobClient = csa.CreateCloudBlobClient();
        string containerName = "my-blob-container"; //Name of your Blob Container
        CloudBlobContainer cbContainer = cloudBlobClient.GetContainerReference(containerName);
        string fileName = this.GenerateFileName(strFileName);

        if (await cbContainer.CreateIfNotExistsAsync())
        {
            await cbContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        }

        if (fileName != null && fileData != null)
        {
            CloudBlockBlob cbb = cbContainer.GetBlockBlobReference(fileName);
            cbb.Properties.ContentType = fileMimeType;
            await cbb.UploadFromByteArrayAsync(fileData, 0, fileData.Length);
            return cbb.Uri.AbsoluteUri;
        }
        return "";
    }
Run Code Online (Sandbox Code Playgroud)

这是参考网址。确保安装这些 Nuget 软件包。

Install-Package WindowsAzure.Storage 
Install-Package MimeMapping
Run Code Online (Sandbox Code Playgroud)