Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function - writing and reading a blob file

This is my first post, pleas excuse me if i have made an errors.

I am very new to Azure.

I have created a function that is triggered by ASA using Service bus Queue.

My data is passed into the function.

I would like to know how i can create a blob file , the file name must be specific to a certain string.

I would then like to write to the file or even read from the file.

Are there any samples of writing and reading to files using Azure functions? I am correct i must use a binding process.

Thank You

DJ

like image 777
user8400863 Avatar asked Aug 01 '17 16:08

user8400863


1 Answers

Are there any samples of writing and reading to files using Azure functions? I am correct i must use a binding process.

To bind Blob to your function, you need to add following configure section to function.json. The direction must be set to 'inout' if you want to read and write data to the blob.

{
  "type": "blob",
  "name": "myBlob",
  "path": "mycontainer/myblob.txt",
  "connection": "your_azurestorage_connection_name",
  "direction": "inout"
}

After that, you could add a parameter to your function named myBlob.

public static async Task Run(CloudBlockBlob myBlob, TraceWriter log)
{
}

Since the type of CloudBlockBlob is not default imported to Azure function, we need to add following code to import it.

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Blob;

At the last, we can read or write data to the blob using the myBlob reference. Sample of writing data to the blob.

byte[] buffer = System.Text.Encoding.UTF8.GetBytes("hello world!");
myBlob.UploadFromByteArray(buffer, 0, buffer.Length);
like image 179
Amor Avatar answered Oct 29 '22 00:10

Amor