MaT
MaT

Reputation: 1606

Windows Azure Local Blob Storage Access?

I don't understand why when I call :

LocalResource ls = RoleEnvironment.GetLocalResource("mystorage");

ls.RootPath;

The local folder is not created, I don't understant how it works. For me, it's a local path that is create. So I should have.

C:\Resources\directory\xxx.HelloAzureWeb.mystorage\

But there is not such file.

Edit: What I am trying to do is to store some xml scene save files. In a Unity application, I need to post (save them) et get (use them) those files.

Upvotes: 2

Views: 3857

Answers (4)

jocull
jocull

Reputation: 21155

For me, the files I store to the storage emulator blobs go into C:\Users\[username]\AppData\Local\DevelopmentStorage\LDB\BlockBlobRoot. Unfortunately, they're not very useful. It's just a bunch of GUIDs, but the filesizes look correct.

Upvotes: 2

AvkashChauhan
AvkashChauhan

Reputation: 20576

Here is how you access Local Storage in your ASP.NET Web Role:

Step 1: Created a very simple ASP.NET Web Role Project

Step 2: Included the following in servicedefinition.csdef:

<LocalResources>
 <LocalStorage name="mystorage" cleanOnRoleRecycle="false" sizeInMB="2096" />
</LocalResources>

Step 3: Add the following code in any aspx (in this case about.aspx)

<div>
  Local Storage file content: <b><asp:Label ID="fileContent" runat="server" /></b>
</div>

Step 4: Add the following code in any aspx.cs (in this case about.aspx.cs):

protected void Page_Load(object sender, EventArgs e)
{
        LocalResource myStorage = RoleEnvironment.GetLocalResource("mystorage");
        string filePath = Path.Combine(myStorage.RootPath, "Lesson.txt");
        File.WriteAllText(filePath, "First Lesson");
        fileContent.Text = File.ReadAllText(filePath);

}

That's it. I have tested this code in compute emulator and on Cloud and it does work.

Upvotes: 1

AvkashChauhan
AvkashChauhan

Reputation: 20576

There is some misunderstanding you have about creating the local storage. When you call below code, it does not create Local Storage for you, instead it returns back to you an instance of Local Storage object which is created during Application setup:

LocalResource ls = RoleEnvironment.GetLocalResource("mystorage");

To define local storage in your application you either directly add the following in CSDEF (below settings will create a local storage name mystorage, size 2GB and if any case VM is rebooted or role restarts, the local storage will not be clean and content still there:

<LocalResources>
 <LocalStorage cleanOnRoleRecycle="false" name="mystorage" sizeInMB="2048" />
</LocalResources>

To add local storage you can also choose ->> [ Visual studio > Role Properties > Local Storage > Add Local Storage ] option as well.

When you define Local Storage in your Windows Azure Application, a new folder will be added as below in your drive C: on Azure VM and this is done when you role is being provisioned in the VM during VM start time:

[In Azure VM]
C:\Resources\directory\[Your_deploymentID].[your_webrolename]_[Role_Instance_Count]\

[In Compute Emulator]
// Lunch your application in Compute Emulator and then open "Compute Emulator UI" to see the Local Storage Path in the command window related to your instance:
C:\Users\avkashc\AppData\Local\dftmp\Resources\3503509c-2112-45ea-8d63-377fe9f78491\directory\mystorage\

Once you will add above local storage specific settings in ServiceDefinition.CSDEF, your local storage will be created and then the following code will work:

LocalResource ls = RoleEnvironment.GetLocalResource("mystorage");
ls.Root // returns the correct local storage path
// Now you can use ls to read/write your data.

Upvotes: 2

David Makogon
David Makogon

Reputation: 71130

When using the storage emulator, Local Resource allocation is just a file directory. The root path looks a bit different from what you describe, but you should be able to navigate to that directory on your local machine, which will initially be empty. You should be seeing it under your \users\you\AppData (which is a hidden directory).

Oh, and local resources have nothing to do with Blob storage. In Windows Azure, it's just a locally-attached disk resource with a specific size quota. It's non-durable storage, unlike Blob storage. Perfect for temporary file writes, caching, etc. and faster than Blob Storage since it's a local disk, but for anything that needs to persist, you'll want to consider Blobs.

Upvotes: 0

Related Questions