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.
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