How to store pdf's on Azure
Asked Answered
E

3

8

I have a WordPress on Linux App running on Azure with a MySql database.

I need to be able to upload PDF files to Azure, and then have a link in the web site that will enable users to then click the link and view the PDF.

To be more specific, the document is a monthly invoice that is created on premise and then uploaded to Azure. The user will log-in and then see a link that will allow him to view the invoice.

What I don't know is how the document should be stored. Should it be stored in the MySql database? Or in some type of storage which can be linked to? Of course, it needs to be secure.

Erratic answered 30/11, 2017 at 15:24 Comment(0)
P
1

You can use Azure Blob Storage to store any file type.

As shown below, get the File-Name, File-Stream, MimeType and File Data for any file.

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

Here's the main method to upload files to 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 "";
    }

Here's the reference URL. Make sure you install these Nuget packages.

Install-Package WindowsAzure.Storage 
Install-Package MimeMapping
Pepsinate answered 20/8, 2019 at 11:34 Comment(0)
B
2

Greg, Blob storage would be your best option within Azure, here's what it can do:

1 -Serving images or documents directly to a browser
2 -Storing files for distributed access
3- Streaming video and audio
4- Storing data for backup and restore, disaster recovery, and archiving
5- Storing data for analysis by an on-premises or Azure-hosted service

Any file stored within Azure Blobs would be able to be accessed through a link, ex: https://storagesample.blob.core.windows.net/mycontainer/blob1.txt or use an alias such as http://files.mycompany.com/somecontainer/bolbs.txt

Full details can be accessed here: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-dotnet-how-to-use-blobs

Blois answered 30/11, 2017 at 17:1 Comment(0)
T
1

You can use Azure blob storage to upload/store your PDF documents. Each document stored will have a link that can be then shown in your website. And also you can protect these resources and use SAS, shared key authentication mechanisms to access the resources.

Telemachus answered 30/11, 2017 at 16:48 Comment(0)
P
1

You can use Azure Blob Storage to store any file type.

As shown below, get the File-Name, File-Stream, MimeType and File Data for any file.

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

Here's the main method to upload files to 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 "";
    }

Here's the reference URL. Make sure you install these Nuget packages.

Install-Package WindowsAzure.Storage 
Install-Package MimeMapping
Pepsinate answered 20/8, 2019 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.