简体   繁体   中英

How to read the content in a azure blob using azure function app

I want to trigger the function app when a file is uploaded into the container and read the content in the file. I'm able to trigger the function app. But when i use Openread to read the content in the file i'm getting reference not found error. Below is the code which I have used to read the file and the binding.

#r "System.IO";

using System;
using System.Collections.Generic;
using System.IO;
using System.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Xml;

using Microsoft.Win32;
using System.Net;
using System.Linq;

public static void Run(Stream myBlob, string name, TraceWriter log, string 
inputBlob)
{
    log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: 
    {myBlob.Length} Bytes");
    StreamReader reader = new StreamReader(inputBlob.OpenRead())
    {
        String oldContent = reader.ReadToEnd();
    }
}

Bindings

{
    "bindings": [{
            "name": "myBlob",
            "type": "blobTrigger",
            "direction": "in",
            "path": "sample/{name}",
            "connection": "testforfunctionapp_STORAGE"
        }, {
            "type": "blob",
            "name": "inputBlob",
            "path": "sample/{name}",
            "connection": "testforfunctionapp_STORAGE",
            "direction": "in"
        }
    ],
    "disabled": false
}

Could anyone please help on this?

Thanks

You could follow this document to create a blob trigger.

i'm getting reference not found error

In your case, the inputBlob is a string type, you can't use inputBlob.OpenRead() . And there is no need additional bind inputBlob

Please have a try to use the following code. It works correctly on my side.

#r "System.IO"

using System;
using System.Collections.Generic;
using System.IO;
public static void Run(Stream myBlob, string name, TraceWriter log)
{
    log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    StreamReader reader = new StreamReader(myBlob);
    string  oldContent = reader.ReadToEnd();
    log.Info($"oldContent:{oldContent}");

}

I didn't see the GetblobReference, which could be the cause of the issue:

 var blob = container.GetBlobReference("testblob.txt");

        blob.UploadText(new String('x', 5000000));

        var source = blob.OpenRead();

You can check the official doc for it here

I'd also recommend checking the example in this thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM