<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Strobaek Blog &#187; Code</title>
	<atom:link href="http://blog.strobaek.org/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.strobaek.org</link>
	<description>Karsten Strøbæk&#039;s blog about current projects, code and life in general</description>
	<lastBuildDate>Fri, 30 Dec 2011 18:33:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>More on Grooming IIS Logs</title>
		<link>http://blog.strobaek.org/2011/09/09/more-on-grooming-iis-logs/</link>
		<comments>http://blog.strobaek.org/2011/09/09/more-on-grooming-iis-logs/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 08:25:21 +0000</pubDate>
		<dc:creator>strobaek</dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://blog.strobaek.org/?p=613</guid>
		<description><![CDATA[I previously blogged about how to delete blob entries older than a given date. I have extended the script slightly to first download entries, zip then and upload them again to another storage container, before actually doing the removal. Before the deletion is carried out a simple check of file size is performed to ensure [...]]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://blog.strobaek.org/2011/08/17/grooming-windows-azure-diagnostics-storage-and-iis-logs/" target="_blank">previously</a> blogged about how to delete blob entries older than a given date.</p>
<p>I have extended the script slightly to first download entries, zip then and upload them again to another storage container, before actually doing the removal. Before the deletion is carried out a simple check of file size is performed to ensure the upload succeced.</p>
<p>The sequence is illustrated below:</p>
<p><a href="http://blog.strobaek.org/wp-content/uploads/groom-iis-logs.jpg"><img class="aligncenter size-medium wp-image-614" title="groom iis logs" src="http://blog.strobaek.org/wp-content/uploads/groom-iis-logs-300x202.jpg" alt="" width="420" height="291" /></a></p>
<p>And the script:</p>
<table width="520" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="518"><span style="font-family: courier new;"><strong><span style="font-size: x-small;">function FileIsLocked( [string] $filePath )<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">{<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    $script:locked = $false<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    $fileInfo = New-Object System.IO.FileInfo $filePath<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    trap<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    {<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">        # if we are in here, the file is locked<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">        $script:locked = $true<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">        continue<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    }<br />
</span></strong></span><span><strong><span style="font-family: Courier New;"><span style="font-size: x-small;"><span>    $fileStream = $fileInfo.Open<br />
[System.IO.FileMode]::OpenOrCreate,<br />
[System.IO.FileAccess]::ReadWrite,<br />
[System.IO.FileShare]::None )</span> if ($fileStream)<br />
{<br />
$fileStream.Close()<br />
}<br />
</span></span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    $script:locked<br />
</span></strong></span><strong><strong><span style="font-family: Courier New;"><span style="font-size: x-small;"><span>}</span></span></span></strong></strong># Name of your account</p>
<p><strong><strong><span style="font-family: Courier New;"><span style="font-size: x-small;"><span style="font-family: courier new;">$accountName = &lt;Account Name&gt;</span></span></span></strong></strong>&nbsp;</p>
<p><strong><span style="font-size: x-small;"><span style="font-family: courier new;"># Account key<br />
</span><span style="font-family: courier new;">$accountKey = &lt;Account Key&gt;<br />
</span></span></strong>&nbsp;</p>
<p><strong><span style="font-size: x-small;"><span style="font-family: courier new;"># Get current date on format YYYYMMDD, e.g. 20110906<br />
</span><span style="font-family: courier new;">$datePart = Get-Date -f &#8220;yyyyMMdd&#8221;</span></span></strong></p>
<p><strong><span style="font-size: x-small;"><span style="font-family: courier new;">#Location of where blob entries should be downloaded to<br />
</span><span style="font-family: courier new;">$downloadLocation = &#8220;C:Temp&#8221; + $datePart</span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;"># Name of source and target storage container<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">$containerName = &#8220;wad-iis-logfiles&#8221;<br />
</span><span style="font-family: courier new;">$targetContainerName = &#8220;backup&#8221;</span></span></strong></p>
<p><strong><span style="font-size: x-small;"><span style="font-family: courier new;"># Download and removed blob entries older than 90 days<br />
</span><span style="font-family: courier new;">$endTime = (get-date).adddays(-90)</span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;"># Download blob entries<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">Write-Host &#8220;Export-BlobContainer”<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">Export-BlobContainer<br />
-Name $containerName<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    -DownloadLocation $downloadLocation<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    -MaximumBlobLastModifiedDateTime $endTime<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">    -AccountName $accountName<br />
</span><span style="font-family: courier new;">    -AccountKey $accountKey</span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;"># Zip files<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">Write-Host &#8220;Zip logfiles&#8221;<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">$zipFileName = $downloadLocation + &#8220;IISLog&#8221; + $datePart + &#8220;.zip&#8221;<br />
</span><span style="font-family: courier new;">set-content $zipFileName (&#8220;PK&#8221; + [char]5 + [char]6 + (&#8220;$([char]0)&#8221; * 18))<br />
</span></span></strong><strong><span style="font-size: x-small;"><span style="font-family: courier new;">(dir $zipFileName).IsReadOnly = $false<br />
</span><span style="font-family: courier new;">$zipFile = (new-object -com shell.application).NameSpace($zipFileName) </span></span></strong><span style="font-family: courier new;"><br />
<strong><span style="font-size: x-small;">$zipFile.CopyHere($downloadLocation)</span></strong></span></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;">Write-Host &#8220;Check if File is locked&#8221;<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">Start-Sleep -s 30<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">$fileIsLocked = FileIsLocked $zipFileName<br />
</span><span style="font-family: courier new;">Write-Host $fileIsLocked</span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;">while ($fileIsLocked)<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">{<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    Write-Host &#8220;File Locked&#8221;<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    Start-Sleep -s 30<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">    $fileIsLocked = FileIsLocked $zipFileName<br />
</span><span style="font-family: courier new;">}</span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;"># Upload zip-file<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">Write-Host &#8220;Import-File&#8221;<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">Import-File<br />
-File $zipFileName<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    -BlobContainerName $targetContainerName<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    -CompressBlob<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    -LoggingLevel &#8220;Detailed&#8221;<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">    -AccountName $accountName<br />
</span><span style="font-family: courier new;">    -AccountKey $accountKey</span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;"># Check if upload went well by comparing file size<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">$ext = &#8220;zip&#8221;<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">$timeTo = (Get-Date -f &#8220;yyyy-MM-dd&#8221;).ToString() + &#8221; 23:59:59&#8243;<br />
</span><span style="font-family: courier new;">$timeFrom = (Get-Date -f &#8220;yyyy-MM-dd&#8221;).ToString() + &#8221; 00:00:00&#8243;</span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;">$bfc = New-Object Cerebrata.AzureUtilities.ManagementClient.StorageManagementEntities.BlobsFilterCriteria<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">$bfc.BlobNameEndsWith = $ext<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">$bfc.LastModifiedDateTimeTo = $timeTo<br />
</span><span style="font-family: courier new;">$bfc.LastModifiedDateTimeFrom = $timeFrom </span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;">$blobInfo = Get-Blob<br />
-BlobContainerName $targetContainerName<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    -IncludeMetadata<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">    -BlobsFilterCriteria $bfc<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">    -AccountName $accountName<br />
</span><span style="font-family: courier new;">    -AccountKey $accountKey</span></span></strong></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;">$fileInfo = (New-Object IO.FileInfo &#8220;$zipFileName&#8221;)</span></strong></span></p>
<p><span style="font-family: courier new;"><strong><span style="font-size: x-small;">if ($blobInfo.Size -eq $fileInfo.Length)<br />
</span></strong></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;">{<br />
</span></strong></span><strong><span style="font-size: x-small;"><span style="font-family: courier new;">    # Delete downloaded files (and zip-file)<br />
</span><span style="font-family: courier new;">    Remove-Item -Recurse -Force $downloadLocation</span></span></strong></p>
<p><span style="font-family: courier new;"><span><strong><span style="font-size: x-small;">    # Remove downloaded blob entries<br />
</span></strong></span></span><span style="font-family: courier new;"><span><strong><span style="font-size: x-small;">    Remove-Blob<br />
-BlobContainerName $containerName<br />
</span></strong></span></span><span style="font-family: courier new;"><span><strong><span style="font-size: x-small;">        -BlobsFilterCriteria $bfc<br />
</span></strong></span></span><span style="font-family: courier new;"><span><strong><span style="font-size: x-small;">        -AccountName $accountName<br />
</span></strong></span></span><span style="font-family: courier new;"><strong><span style="font-size: x-small;"><span>        -AccountKey $accountKey<br />
</span><span>}</span></span></strong></span></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://blog.strobaek.org/2011/09/09/more-on-grooming-iis-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grooming Windows Azure Diagnostics Storage and IIS Logs</title>
		<link>http://blog.strobaek.org/2011/08/17/grooming-windows-azure-diagnostics-storage-and-iis-logs/</link>
		<comments>http://blog.strobaek.org/2011/08/17/grooming-windows-azure-diagnostics-storage-and-iis-logs/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 10:06:30 +0000</pubDate>
		<dc:creator>strobaek</dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://blog.strobaek.org/?p=600</guid>
		<description><![CDATA[People working with Windows Azure are aware that the storage used for diagnostics will continue to grow perpetually if nothing is done about it. With the introduction of the Windows Azure Management Pack – I call it WAMP; don’t know if it has an official acronym yet &#8211; System Center Operations Manager (SCOM) is able [...]]]></description>
			<content:encoded><![CDATA[<p>People working with Windows Azure are aware that the storage used for diagnostics will continue to grow perpetually if nothing is done about it.</p>
<p>With the introduction of the <a href="http://www.microsoft.com/download/en/details.aspx?id=11324" target="_blank">Windows Azure Management Pack</a> – I call it WAMP; don’t know if it has an official acronym yet &#8211; System Center Operations Manager (SCOM) is able to groom the tables.</p>
<p>By default the following three rules are disabled in WAMP:</p>
<ul>
<li>Windows Azure Role Performance Counter Grooming</li>
<li>Windows Azure Role .NET Trace Grooming</li>
<li>Windows Azure Role NET Event Log Grooming</li>
</ul>
<ul>Once they have been enabled the rule will run on a periodic basis and will delete data from the relevant table older than T hours.</ul>
<p>An online guide to WAMP is available <a href="http://technet.microsoft.com/en-us/library/gg276367.aspx" target="_blank">here</a>.</p>
<p>Unfortunately WAMP/SCOM does not come with a similar functionality to groom the IIS logs located in blob storage. By default these logs are written to the blob storage once every hour, so after a few months in production there are quite a number of them. And remember, that it is one log entry per instance.</p>
<p>The cost of storage is not very big, so it would be difficult to argue for an automated solution to groom the logs if price is the only parameter considered. However, as the number of entries grow it will take longer and longer to actually identify the relevant one.</p>
<p>To overcome this challenge one can write a small PowerShell script using e.g. the <a href="https://www.cerebrata.com/Products/AzureManagementCmdlets/Default.aspx" target="_blank">Azure Management Cmdlets</a> developed by <a href="https://www.cerebrata.com/" target="_blank">Cerebrata</a>.</p>
<p>The script could look like the following:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="586"><strong><span style="font-family: Courier New;"># Name of your account</span></strong><br />
<strong><span style="font-family: Courier New;">$accountName = &lt;account name&gt;</span></strong><br />
<strong><span style="font-family: Courier New;"># Account key </span></strong><br />
<strong><span style="font-family: Courier New;">$accountKey = &lt;acount key&gt;</span></strong><strong><span style="font-family: Courier New;"># Name of container, e.g. wad-iis-logfiles</span></strong><br />
<strong><span style="font-family: Courier New;">$containerName = &lt;Container Name&gt;</span></strong><br />
<strong><span style="font-family: Courier New;">$lastModified = &lt;UTC Date&gt;</span></strong></p>
<p><strong><span style="font-family: Courier New;">$bfc = New-Object Cerebrata.AzureUtilities.ManagementClient.StorageManagementEntities.BlobsFilterCriteria</span></strong></p>
<p><strong><span style="font-family: Courier New;">$bfc.LastModifiedDateTimeTo = $lastModified</span></strong></p>
<p><strong><span style="font-family: Courier New;">Remove-Blob -BlobContainerName $containerName </span></strong><br />
<strong><span style="font-family: Courier New;">-BlobsFilterCriteria $bfc </span></strong><br />
<strong><span style="font-family: Courier New;">-AccountName $accountName </span></strong><br />
<span style="font-family: Courier New;"><strong>-AccountKey $accountKey</strong><strong></strong></span></td>
</tr>
</tbody>
</table>
<p>The script will remove all blob entries older than the date given.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.strobaek.org/2011/08/17/grooming-windows-azure-diagnostics-storage-and-iis-logs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FTP from Windows Azure Blob Storage</title>
		<link>http://blog.strobaek.org/2011/04/11/ftp-from-windows-azure-blob-storage/</link>
		<comments>http://blog.strobaek.org/2011/04/11/ftp-from-windows-azure-blob-storage/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 12:36:48 +0000</pubDate>
		<dc:creator>strobaek</dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://blog.strobaek.org/?p=570</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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&#160; have to make to enable Windows Azure to execute classis ASP.</p>
<p>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.</p>
<p>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).</p>
<p>It turned out to be relatively easy to do this.</p>
<p>We first set up the CloudStorageAccount and the CloudBlobClient</p>
<div style="font-family: ; background: #f8f8f8; color: ">
<div style="font-family: ; background: #f8f8f8; color: ">
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"><font color="#000000"> blobStorage = </font><span style="color: "><font color="#2b91af">CloudStorageAccount</font></span><font color="#000000">.FromConfigurationSetting(</font><span style="color: "><font color="#a31515">&quot;DataConnectionString&quot;</font></span><font color="#000000">);</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt" color="#000000"> blobClient = blobStorage.CreateCloudBlobClient();</font></font></p>
<p style="margin: 0px"><font face="Consolas"><font color="#000000"><font style="font-size: 10pt">blobClient.RetryPolicy = </font></font><font style="font-size: 10pt"><span style="color: "><font color="#2b91af">RetryPolicies</font></span><font color="#000000">.Retry(4, </font><span style="color: "><font color="#2b91af">TimeSpan</font></span><font color="#000000">.Zero);</font></font></font></p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">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</p>
<p style="margin: 0px">&#160;</p>
<div style="font-family: ; background: #f8f8f8; color: ">
<div style="font-family: ; background: white; color: ">
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#2b91af"><font style="font-size: 10pt">IEnumerable</font></font></span><font style="font-size: 10pt"><font color="#000000">&lt;</font><span style="color: "><font color="#2b91af">IListBlobItem</font></span><font color="#000000">&gt; blobs = container.ListBlobs();</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">if</font></font></span><font style="font-size: 10pt"><font color="#000000"> (blobs != </font><span style="color: "><font color="#0000ff">null</font></span><font color="#000000">)</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">{</font></font></p>
<p style="margin: 0px"><font face="Consolas"><font color="#000000"><font style="font-size: 10pt">&#160;&#160;&#160; </font></font><font style="font-size: 10pt"><span style="color: "><font color="#0000ff">foreach</font></span><font color="#000000">(</font><span style="color: "><font color="#0000ff">var</font></span><font color="#000000"> blobItem </font><span style="color: "><font color="#0000ff">in</font></span><font color="#000000"> blobs)</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">&#160;&#160;&#160; {&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">&#160;</font></font></p>
</p></div>
<p style="margin: 0px">and then handling each blobItem.</p>
<p style="margin: 0px">&#160;</p>
</p></div>
</p></div>
</p></div>
<div style="font-family: ; background: #f8f8f8; color: ">
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"><font color="#000000"> containerName = </font><span style="color: "><font color="#a31515">&quot;karstens&quot;</font></span><font color="#000000">;</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#2b91af"><font style="font-size: 10pt">CloudBlobContainer</font></font></span><font style="font-size: 10pt" color="#000000"> container = blobClient.GetContainerReference(containerName);</font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"><font color="#000000"> fileName = </font><span style="color: "><font color="#a31515">&quot;Windows Azure Platform.pdf&quot;</font></span><font color="#000000">;</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"><font color="#000000"> uniqueBlobName = </font><span style="color: "><font color="#0000ff">string</font></span><font color="#000000">.Format(</font><span style="color: "><font color="#a31515">&quot;{0}/{1}&quot;</font></span><font color="#000000">, containerName, fileName);</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#2b91af"><font style="font-size: 10pt">CloudBlockBlob</font></font></span><font style="font-size: 10pt" color="#000000"> blob = blobClient.GetBlockBlobReference(uniqueBlobName);</font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000"></font></font></p>
</p></div>
<p>Next we need to setup the FTP server. First some housekeeping; again, you would properly not hardcode this</p>
<div style="font-family: ; background: #f8f8f8; color: ">
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"><font color="#000000"> ftpServerIP = </font><span style="color: "><font color="#a31515">&quot;&lt;FTPSERVER&gt;&quot;</font></span><font color="#000000">;</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"><font color="#000000"> ftpUserID = </font><span style="color: "><font color="#a31515">&quot;&lt;USERID&gt;&quot;</font></span><font color="#000000">;</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"><font color="#000000"> ftpPassword = </font><span style="color: "><font color="#a31515">&quot;&lt;PASSWORD&gt;&quot;</font></span><font color="#000000">;</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">var</font></font></span><font style="font-size: 10pt"><font color="#000000"> ftpFilename = </font><span style="color: "><font color="#a31515">&quot;ftp://&quot;</font></span><font color="#000000"> + ftpServerIP + </font><span style="color: "><font color="#a31515">&quot;//&quot;</font></span><font color="#000000"> + fileName;</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">&#160;</font></font></p>
</p></div>
<p>To to the actual FTP transfer we use the FtpWebRequest class</p>
<div style="font-family: ; background: #f8f8f8; color: ">
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#2b91af"><font style="font-size: 10pt">FtpWebRequest</font></font></span><font style="font-size: 10pt"><font color="#000000"> ftpReq = (</font><span style="color: "><font color="#2b91af">FtpWebRequest</font></span><font color="#000000">)</font><span style="color: "><font color="#2b91af">WebRequest</font></span><font color="#000000">.Create(ftpFilename);</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font color="#000000"><font style="font-size: 10pt">ftpReq.Method = </font></font><font style="font-size: 10pt"><span style="color: "><font color="#2b91af">WebRequestMethods</font></span><font color="#000000">.</font><span style="color: "><font color="#2b91af">Ftp</font></span><font color="#000000">.UploadFile;</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font color="#000000"><font style="font-size: 10pt">ftpReq.Credentials = </font></font><font style="font-size: 10pt"><span style="color: "><font color="#0000ff">new</font></span><font color="#000000"> </font><span style="color: "><font color="#2b91af">NetworkCredential</font></span><font color="#000000">(ftpUserID, ftpPassword);</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000"></font></font></p>
</p></div>
<p>If the files you with to transfer, remember to set the UseBinary property to true</p>
<div style="font-family: ; background: #f8f8f8; color: ">
<p style="margin: 0px"><font face="Consolas"><font color="#000000"><font style="font-size: 10pt">ftpReq.UseBinary = </font></font><font style="font-size: 10pt"><span style="color: "><font color="#0000ff">true</font></span><font color="#000000">;</font></font></font></p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">You may also have to turn off the Proxy for the FTP request. This is done by setting the Proxy property to null</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<div style="font-family: ; background: #f8f8f8; color: ">
<p style="margin: 0px"><font face="Consolas"><font color="#000000"><font style="font-size: 10pt">ftpReq.Proxy = </font></font><font style="font-size: 10pt"><span style="color: "><font color="#0000ff">null</font></span><font color="#000000">;</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">&#160;</font></font></p>
</p></div>
<p>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</p>
<div style="font-family: ; background: #f8f8f8; color: ">
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#2b91af"><font style="font-size: 10pt">Byte</font></font></span><font style="font-size: 10pt" color="#000000">[] b = blob.DownloadByteArray();</font></font></p>
<p style="margin: 0px"><font color="#000000" size="2" face="Consolas"></font></p>
<div style="font-family: ; background: white; color: ">
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000"></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">ftpReq.ContentLength = b.Length;</font></font></p>
<p style="margin: 0px"><font color="#000000" size="2" face="Consolas"></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000"></font></font></p>
<div style="font-family: ; background: white; color: ">
<div style="font-family: ; background: white; color: ">
<p style="margin: 0px"><font face="Consolas"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">using</font></font></span><font style="font-size: 10pt"><font color="#000000"> (</font><span style="color: "><font color="#2b91af">Stream</font></span><font color="#000000"> s = ftpReq.GetRequestStream())</font></font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">{</font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">&#160;&#160;&#160; s.Write(b, 0, b.Length);</font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></p>
<p style="margin: 0px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">&#160;</font></font></p>
</p></div>
</p></div>
</p></div>
</p></div>
<p>And we are done.</p>
<p>If you wish to automate this, you could have the WorkerRole monitor the Blob container and FTP any new items.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.strobaek.org/2011/04/11/ftp-from-windows-azure-blob-storage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Formatting code</title>
		<link>http://blog.strobaek.org/2009/08/29/formatting-code/</link>
		<comments>http://blog.strobaek.org/2009/08/29/formatting-code/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 06:05:58 +0000</pubDate>
		<dc:creator>strobaek</dc:creator>
				<category><![CDATA[Admin]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[formatting]]></category>

		<guid isPermaLink="false">http://blog.strobaek.org/?p=22</guid>
		<description><![CDATA[I&#8217;m trying to find the best plugin for formatting the code.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m trying to find the best plugin for formatting the code. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.strobaek.org/2009/08/29/formatting-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

