Upload and download file from Blob Storage using x++.
By using below code we connect to Blob storage. Below operations also we can do it.
- File List
- Read the file content
- Move the file between folder.
- Upload File
- Delete File
using Microsoft.WindowsAzure.Storage;
Using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
class TestAzureBlob
{
public static void main(Args _args)
{
TestAzureBlob testAzureBlob = new TestAzureBlob();
CloudBlobContainer blobContainer;
// Connecting container
blobContainer = testAzureBlob.connectToAzureBlob();
// Get's the file List
testAzureBlob.getFilesList(blobContainer);// Way 1
testAzureBlob.readTheFiles(blobContainer);// Way 2
// Read the data in file
testAzureBlob.readFileValue(blobContainer);
// upload the file
testAzureBlob.UploadFileToBlob(blobContainer);
// move the file
testAzureBlob.moveTheFileFromOneFolderToAnotherFolder(blobContainer);
// delete the file
testAzureBlob.deleteFileFromFolder(blobContainer);
}
}
Connect to Blob:
public CloudBlobContainer connectToAzureBlob()
{
CloudBlobClient cloudBlobClient;
CloudBlobContainer cloudBlobContainer;
CloudStorageAccount cloudStorageAccount;
cloudStorageAccount = CloudStorageAccount::Parse("Azure Blob Connection String");
// Example :
// ("DefaultEndpointsProtocol = https;
// AccountName = 'AccountName';AccountKey = 'AccountKey';EndpointSuffix=core.windows.net");
cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
cloudBlobContainer = cloudBlobClient.GetContainerReference("mycontainer");
Info(cloudBlobContainer.Name);
return cloudBlobContainer;
}
Get the files List:
Way 1:
public void getFilesList(CloudBlobContainer _cloudBlobContainer)
{
// Directory of blob container
CloudBlobDirectory cloudBlobDirectory;
container fileCon;
// Folder Path
cloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference("My Folder");
System.Collections.IEnumerable lstEnumarable = cloudBlobDirectory.ListBlobs(false, 0, null, null);
System.Collections.IEnumerator lstEnumarator = lstEnumarable.GetEnumerator();
List filenames = new List(Types::String);
while(lstEnumarator.MoveNext())
{
IListBlobItem item = lstEnumarator.Current;
if (item is CloudBlockBlob)
{
CloudBlockBlob blob = item;
blob.FetchAttributes(null, null, null);
fileCon = str2con(blob.name, "/");
filenames.addStart(conPeek(filecon, conLen(filecon)));
info(strFmt("File : %1", conPeek(filecon, conLen(filecon))));
}
}
}
Way 2:
public void readTheFiles(CloudBlobContainer _cloudBlobContainer)
{
System.Collections.IEnumerable lstEnumarable = _cloudBlobContainer.ListBlobs(null, false, 0, null, null);
System.Collections.IEnumerator lstEnumarator = lstEnumarable.GetEnumerator();
List filenames = new List(Types::String);
while (lstEnumarator.MoveNext())
{
IListBlobItem item = lstEnumarator.Current;
if (item is CloudBlockBlob)
{
CloudBlockBlob blob = item;
System.IO.StreamReader reader = new System.IO.StreamReader(blob.OpenRead(null, null, null));
Info(reader.ReadToEnd());
}
}
}
Read the file content:
public void readFileValue(CloudBlobContainer _cloudBlobContainer)
{
CloudBlobDirectory cloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference("My Folder");
CloudBlockBlob blob = cloudBlobDirectory.GetBlockBlobReference("File");
System.IO.Stream memory = blob.openRead(null, null, null);
// Read the content
System.IO.StreamReader streamReader = new System.IO.StreamReader(memory);
// Read each line
str strRecord = streamReader.ReadLine();
// while (!System.String::IsNullOrEmpty(strRecord))
{
try
{
container conRecord = str2con_RU(strRecord, ',');
info(conPeek(conRecord, 1));
info(conPeek(conRecord, 2));
strRecord = streamReader.ReadLine();
}
catch
{
throw error("Message");
}
}
}
Upload the file:
public void UploadFileToBlob(CloudBlobContainer _cloudBlobContainer)
{
CloudBlobDirectory cloudBlobDirectory;
CloudBlockBlob CloudBlockBlob;
System.Byte[] reportBytes = new System.Byte[0]();
// File path
cloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference("My Folder");
CloudBlockBlob = cloudBlobDirectory.GetBlockBlobReference("File.Txt");
// ---------------- or ----------------
CloudBlockBlob = _cloudBlobContainer.GetBlockBlobReference("File.Txt");
// Encode
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
reportBytes = enc.GetBytes("YOUR XML STRING/TEXT or Data in file");
System.IO.MemoryStream stream = new System.IO.MemoryStream(reportBytes);
// upload stream
CloudBlockBlob.UploadFromStream(stream, null, null, null);
}
Move the file:
public void moveTheFileFromOneFolderToAnotherFolder(CloudBlobContainer _cloudBlobContainer)
{
CloudBlobDirectory sourceCloudBlobDirectory;
CloudBlobDirectory destinationCloudBlobDirectory;
CloudBlockBlob sourceCloudBlockBlob;
CloudBlockBlob destinationCloudBlockBlob;
sourceCloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference("My Folder");
destinationCloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference("My Folder 2");
sourceCloudBlockBlob = sourceCloudBlobDirectory.GetBlockBlobReference("File1.Txt");
destinationCloudBlockBlob = destinationCloudBlobDirectory.GetBlockBlobReference("File1.Txt");
// Upload the file to destination
destinationCloudBlockBlob.UploadFromStream(sourceCloudBlockBlob.OpenRead(null, null, null), null, null, null);
}
Delete the file:
public void deleteFileFromFolder(CloudBlobContainer _cloudBlobContainer)
{
CloudBlobDirectory sourceCloudBlobDirectory;
CloudBlockBlob sourceCloudBlockBlob;
sourceCloudBlobDirectory = _cloudBlobContainer.GetDirectoryReference("My Folder");
sourceCloudBlockBlob = sourceCloudBlobDirectory.GetBlockBlobReference("File2.Txt");
// Delete file
sourceCloudBlockBlob.delete(0, null, null, null);
}
Keep Daxing!!