7:Understanding and Creating Blob storage in Windows Azure
for Cloud services. Log into the Azure Management Portal. At the bottom of the navigation pane, click NEW. Click DAA !E"#$CE!, then !%"A&E, and then click 'U$C( C"EAE. In UL, t!pe a "ub domain name to u"e in the UI for the "torage account. #he entr! can contain from $%&' lo(erca"e letter" and number". #hi" value become" the ho"t name (ithin the UI that i" u"ed to addre"" )lob, *ueue, or #able re"ource" for the "ub"cription. Choo"e a egion+Affinit! ,roup in (hich to locate the "torage. If !ou (ill be u"ing "torage from !our Azure application, "elect the "ame region (here !ou (ill deplo! !our application. -ptionall!, !ou can enable geo%replication. Click create storage account. !etu) A !torage Connection !tring #he Azure .torage Client Librar! for ./0# "upport" u"ing a "torage connection "tring to configure endpoint" and credential" for acce""ing "torage "ervice". 1ou can put !our "torage Configuring *our connection string w+en using Cloud !ervices connection "tring in a configuration file, rather than hard%coding it in code2 3hen u"ing Azure Cloud .ervice", it i" recommended !ou "tore !our connection "tring u"ing the Azure "ervice configuration "!"tem 4*.csdef and *.cscfg file"5. 3hen u"ing Azure 3eb .ite", Azure 6irtual Machine", or building ./0# application" that are intended to run out"ide of Azure, it i" recommended !ou "tore !our connection "tring u"ing the ./0# configuration "!"tem 4e.g. web.config or app.config file5. #he "ervice configuration mechani"m i" uni7ue to Azure Cloud .ervice" pro8ect" and enable" !ou to d!namicall! change configuration "etting" from the Azure Management Portal (ithout redeplo!ing !our application. #o configure !our connection "tring in the Azure "ervice configuration2 9. 3ithin the .olution 0:plorer of 6i"ual .tudio, in the "oles folder of !our Azure ;eplo!ment Pro8ect, right%click !our (eb role or (orker role and click Pro)erties. &. Click the !ettings tab and pre"" the Add !etting button. A ne( !etting, entr! (ill then "ho( up in the "etting" grid. $. In the *)e drop%do(n of the ne( !etting, entr!, choo"e Connection !tring. '. Click the ... button at the right end of the !etting, entr!. #he !torage Account Connection !tring dialog (ill open. <. Choo"e (hether !ou (ant to target the "torage emulator 43indo(" Azure "torage "imulated on !our local machine5 or a "torage account in the cloud. #he code in thi" guide (ork" (ith either option. 0nter the Pri-ar* Access (e* value copied from the earlier "tep in thi" tutorial if !ou (i"h to "tore blob data in the "torage account (e created earlier on Azure. =. Change the entr! Na-e from !etting, to a name Configuring *our connection string using .NE configuration If !ou are (riting an application that i" not an Azure cloud "ervice, 4"ee previou" "ection5, it i" recommended !ou u"e the ./0# configuration "!"tem 4e.g. web.config or app.config5. #hi" include" Azure 3eb .ite" or Azure 6irtual Machine", a" (ell a" application" de"igned to run out"ide of Azure. 1ou "tore the connection "tring u"ing the <appSettings> element a" follo("2 <configuration> <appSettings> <add key="StorageConnectionString" value="DefaultEndpointsrotocol=!ttps"#ccount$a%e=&#ccount$a%e'"#ccount(ey=&# ccount(ey" )> <)appSettings> <)configuration> Na-es)ace declarations Add the follo(ing name"pace declaration" to the top of an! C> file in (hich !ou (i"h to programmaticall! acce"" Azure .torage2 using *icrosoft.+indows#,ure.Storage" using *icrosoft.+indows#,ure.Storage.#ut!" using *icrosoft.+indows#,ure.Storage.-lob" Make "ure !ou reference the *icrosoft.+indows#,ure.Storage.dll a""embl!. "etrieving *our connection string 1ou can u"e the Cloud!torageAccount t!pe to repre"ent !our .torage Account information. If !ou are u"ing a 3indo(" Azure pro8ect template and+or have a reference to Micro"oft.3indo("Azure.CloudConfigurationManager, !ou can !ou u"e the CloudConfiguration.anager t!pe to retrieve !our "torage connection "tring and "torage account information from the Azure "ervice configuration2 CloudStorage#ccount storage#ccount = CloudStorage#ccount.arse. CloudConfiguration*anager./etSetting."StorageConnectionString"00" If !ou are creating an application (ith no reference to Micro"oft.3indo("Azure.CloudConfigurationManager, and !our connection "tring i" located in the web.config or app.config a" "ho( above, then !ou can u"e Configuration.anager to retrieve the connection "tring. 1ou (ill need to add a reference to .!"tem.Configuration.dll to !our pro8ect, and add another name"pace declaration for it2 using Syste%.Configuration" CloudStorage#ccount storage#ccount = CloudStorage#ccount.arse.
Configuration*anager.ConnectionStrings&"StorageConnectionString"'.ConnectionS tring0" A CloudBlobClient t!pe allo(" !ou to retrieve ob8ect" that repre"ent container" and blob" "tored (ithin the )lob .torage .ervice. #he follo(ing code create" a CloudBlobClient ob8ect u"ing the "torage account ob8ect (e retrieved above2 Cloud-lobClient blobClient = storage#ccount.CreateCloud-lobClient.0" %Data/ib de)endencies -;ataLib dependencie" in the .torage Client Librar! for ./0# are re"olved through the -;ataLib 4ver"ion <.?.&5 package" available through /u,et and not 3C@ ;ata .ervice". #he -;ataLib librarie" can be do(nloaded directl! or referenced b! !our code pro8ect through /u,et. #he "pecific -;ataLib package" are -;ata, 0dm, and .patial. 0ow to: Create a container All "torage blob" re"ide in a container. 1ou can u"e a CloudBlobClient ob8ect to get a reference to the container !ou (ant to u"e. 1ou can create the container if it doe"nAt e:i"t2 CloudStorage#ccount storage#ccount = CloudStorage#ccount.arse. CloudConfiguration*anager./etSetting."StorageConnectionString"00" Cloud-lobClient blobClient = storage#ccount.CreateCloud-lobClient.0" Cloud-lobContainer container = blobClient./etContainer1eference."%ycontainer"0" container.Create2f$otE3ists.0" )! default, the ne( container i" private and !ou mu"t "pecif! !our "torage acce"" ke! to do(nload blob" from thi" container. If !ou (ant to make the file" (ithin the container available to ever!one, !ou can "et the container to be public u"ing the follo(ing code2 container.Seter%issions. new -lobContainerer%issions 4 ublic#ccess = -lobContainerublic#ccess5ype.-lob 60" An!one on the Internet can "ee blob" in a public container, but !ou can modif! or delete them onl! if !ou have the appropriate acce"" ke!. 0ow to U)load a blob into a container Azure )lob .torage "upport" block blob" and page blob". In the ma8orit! of ca"e", block blob i" the recommended t!pe to u"e. #o upload a file to a block blob, get a container reference and u"e it to get a block blob reference. -nce !ou have a blob reference, !ou can upload an! "tream of data to it b! calling the U)load1ro-!trea- method. #hi" operation (ill create the blob if it didnAt previou"l! e:i"t, or over(rite it if it doe" e:i"t. #he follo(ing e:ample "ho(" ho( to upload a blob into a container and a""ume" that the container (a" alread! created. CloudStorage#ccount storage#ccount = CloudStorage#ccount.arse. CloudConfiguration*anager./etSetting."StorageConnectionString"00" Cloud-lobClient blobClient = storage#ccount.CreateCloud-lobClient.0" Cloud-lobContainer container = blobClient./etContainer1eference."%ycontainer"0" Cloud-lock-lob block-lob = container./et-lock-lob1eference."%yblob"0" using .var fileStrea% = Syste%.27.8ile.7pen1ead.9"pat!:%yfile"00 4 block-lob.;pload8ro%Strea%.fileStrea%0" 6 0ow to: /ist t+e blobs in a container #o li"t the blob" in a container, fir"t get a container reference. 1ou can then u"e the containerA" /istBlobs method to retrieve the blob" and+or directorie" (ithin it. #o acce"" the rich "et of propertie" and method" for a returned $/istBlob$te-, !ou mu"t ca"t it to a CloudBloc2Blob, CloudPageBlob, or CloudBlobDirector* ob8ect. If the t!pe i" unkno(n, !ou can u"e a t!pe check to determine (hich to ca"t it to. #he follo(ing code demon"trate" ho( to retrieve and output the UI of each item in the p!otos container2 CloudStorage#ccount storage#ccount = CloudStorage#ccount.arse. CloudConfiguration*anager./etSetting."StorageConnectionString"00" Cloud-lobClient blobClient = storage#ccount.CreateCloud-lobClient.0" Cloud-lobContainer container = blobClient./etContainer1eference."p!otos"0" foreac! .2<ist-lob2te% ite% in container.<ist-lobs.null= false00 4 if .ite%./et5ype.0 == typeof.Cloud-lock-lob00 4 Cloud-lock-lob blob = .Cloud-lock-lob0ite%" Console.+rite<ine."-lock blob of lengt! 4>6? 4@6"= blob.roperties.<engt!= blob.;ri0" 6 else if .ite%./et5ype.0 == typeof.Cloudage-lob00 4 Cloudage-lob page-lob = .Cloudage-lob0ite%" Console.+rite<ine."age blob of lengt! 4>6? 4@6"= page-lob.roperties.<engt!= page-lob.;ri0" 6 else if .ite%./et5ype.0 == typeof.Cloud-lobDirectory00 4 Cloud-lobDirectory directory = .Cloud-lobDirectory0ite%" Console.+rite<ine."Directory? 4>6"= directory.;ri0" 6 6 A" "ho(n above, the blob "ervice ha" the concept of directorie" (ithin container", a" (ell. #hi" i" "o that !ou can organize !our blob" in a more folder%like "tructure. photo9.8pg A>@>)arc!itecture)description.t3t A>@>)arc!itecture)p!otoB.Cpg A>@>)arc!itecture)p!otoD.Cpg A>@@)arc!itecture)description.t3t A>@@)p!otoE.Cpg 3hen !ou call /istBlobs on the Aphoto"A container 4a" in the above "ample5, the collection returned (ill contain CloudBlobDirector* and CloudBloc2Blob ob8ect" repre"enting the directorie" and blob" contained at the top level. Bere (ould be the re"ulting output2 Practical:7 Understanding and Creating Blob storage in Windows Azure for Cloud services Step 1- Create a new project. Step 2- App.config <?xml version="1.0" encoing="!tf-"" ?# <config!ration# <start!p# <s!pporte$!ntime version="v%.0" s&!=".'()*ramewor&+,ersion=v%.-" .# <.start!p# <connectionStrings# <a name="A/!reStorageAcco!nt" connectionString="0efa!lt(npoints1rotocol=2ttps3Acco!nt'ame=4g2istorage3Acco!nt5 e6=1"
<.connectionStrings# <appSettings# <a &e6="so!rce*oler" val!e="c789ocal:lo4s".# <a &e6="estcontainer" val!e="emo4lo4s".# <.appSettings# <.config!ration# !sing ;icrosoft.<inowsA/!re.Storage3 !sing ;icrosoft.<inowsA/!re.Storage.:lo43 !sing S6stem3 !sing S6stem.Collections.=eneric3 !sing S6stem.Config!ration3 !sing S6stem.>?3 !sing S6stem.9in@3 !sing S6stem.)ext3 !sing S6stem.)2reaing.)as&s3 namespace project1 A class 1rogram A static voi ;ainBstringCD argsE A string connString = Config!ration;anager.ConnectionStringsC"A/!reStorageAcco!nt"D.ConnectionString3 string local*oler = Config!ration;anager.AppSettingsC"so!rce*oler"D3 string esContainer = Config!ration;anager.AppSettingsC"esContainer"D3 Console.<rite9ineB"connection to storage acco!nt"E3 Clo!StorageAcco!nt sa = Clo!StorageAcco!nt.1arseBconnStringE3 Clo!:lo4Client 4c = sa.CreateClo!:lo4ClientBE3 Console.<rite9ineB"=etting reference to container"E3 Clo!:lo4Container container = 4c.=etContainer$eferenceBesContainerE3 container.Create>f'ot(xistsBE3 stringCD file(ntries = 0irector6.=et*ilesBlocal*olerE3 foreac2 Bstring file1at2 in file(ntriesE A string &e6 = 0ate)ime.Ftc'ow.)oStringB"6666-;;--GG7mm7ss"E H "-" H 1at2.=et*ilenameBfile1at2E3 Fploa:lo4Bcontainer+ &e6+ file1at2+ tr!eE3 I Console.<rite9ineBJ"!ploa processing complete.1ress an6 &e6 to exist"E3 Console.$ea5e6BE3 I static voi Fploa:lo4BClo!:lo4Container container+ string &e6+ string file'ame+ 4ool eleteAfterE A Console.<rite9ineBJ"!ploaing file to container 7&e6 =" H &e6 H "so!rce file" H file'ameE3 Clo!:loc&:lo4 4 = container.=et:loc&:lo4$eferenceB&e6E3 !sing Bvar fs = S6stem.>?.*ile.?penBfile'ame+ *ile;oe.?pen+ *ileAccess.$ea+ *ileS2are.'oneEE A 4.Fploa*romStreamBfsE3 I if BeleteAfterE *ile.0eleteBfile'ameE3 I I