In connection with migrating an old CMS system to Windows Azure, I have been playing around with Windows Azure Blob Storage. The CMS system was 10 years old and written in classic ASP, using a lot of local resources mainly the file system. I’ll try go write a blob post later on the few tweaks you have to make to enable Windows Azure to execute classis ASP.
One feature of the old CMS system was, that you could use FTP to move files to be displayed on the site. Getting files into Blob storage is relatively easy; you can even mount a Blob container using it as a (network) drive.
A colleague asked me, if it was possible to FTP files out of Blob storage (something about moving very large movie files, and trigger the process on the Azure side).
It turned out to be relatively easy to do this.
We first set up the CloudStorageAccount and the CloudBlobClient
var blobStorage = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
var blobClient = blobStorage.CreateCloudBlobClient();
blobClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);
Next we get a reference to the blob entry. For the sake of simplicity we hardcode the name, but you could of course loop through all entries like this
IEnumerable<IListBlobItem> blobs = container.ListBlobs();
if (blobs != null)
{
foreach(var blobItem in blobs)
{
and then handling each blobItem.
var containerName = "karstens";
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
var fileName = "Windows Azure Platform.pdf";
var uniqueBlobName = string.Format("{0}/{1}", containerName, fileName);
CloudBlockBlob blob = blobClient.GetBlockBlobReference(uniqueBlobName);
Next we need to setup the FTP server. First some housekeeping; again, you would properly not hardcode this
var ftpServerIP = "<FTPSERVER>";
var ftpUserID = "<USERID>";
var ftpPassword = "<PASSWORD>";
var ftpFilename = "ftp://" + ftpServerIP + "//" + fileName;
To to the actual FTP transfer we use the FtpWebRequest class
FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(ftpFilename);
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
If the files you with to transfer, remember to set the UseBinary property to true
ftpReq.UseBinary = true;
You may also have to turn off the Proxy for the FTP request. This is done by setting the Proxy property to null
ftpReq.Proxy = null;
Next step is to download the blob entry into a bytearray, set the length on the FTP request and write the file to the (ftp)stream
Byte[] b = blob.DownloadByteArray();
ftpReq.ContentLength = b.Length;
using (Stream s = ftpReq.GetRequestStream())
{
s.Write(b, 0, b.Length);
}
And we are done.
If you wish to automate this, you could have the WorkerRole monitor the Blob container and FTP any new items.
I am actually looking for a reference point whether FTP out from azure blobs. This article gave me confidence that i could move in this direction. Thanks for posting this.