Search code examples
javaazureazure-functionsazure-blob-trigger

Azure Function BlobTrigger: Microsoft.WindowsAzure.Storage: No connection could be made because the target machine actively refused it


I am not able to run my azure function locally written in java which should work based on BlobTrigger.

I am facing the following error:

 A host error has occurred
 Microsoft.WindowsAzure.Storage: No connection could be made because the target machine actively refused it. System.Net.Http: No connection could be made because the target machine actively refused it. System.Private.CoreLib: No connection could be made because the target machine actively refused it.

Here is my code:

public class Function {

    @FunctionName("BlobTrigger")
    @StorageAccount("reseaudiag")
    public void blobTrigger(
            @BlobTrigger(name = "content", path = "filer/{fileName}", dataType = "binary",
            connection = "AzureWebJobsDashboard"
                    ) byte[] content,
            @BindingName("fileName") String fileName,
            final ExecutionContext context
            ) {
        context.getLogger().info("Java Blob trigger function processed a blob.\n Name: " + fileName + "\n Size: " + content.length + " Bytes");

    }
}

Based on initial run only, I can start implementing the logic but I am blocked to run the basic step itself.

Here is my local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=XXX;AccountKey=confidential;EndpointSuffix=core.windows.net",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "DataConnectionString": "UseDevelopmentStorage=true",
    "ContainerName": "filer"
  },
  "ConnectionStrings": {
    "PlantaoEntities": {
      "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=confidential;EndpointSuffix=core.windows.net",
      "ProviderName": "System.Data.EntityClient"
    }
  }
}

Thank you.


Solution

  • Your code is almost correct. You need to specify a correct connection in your blob trigger.

    Here is my successful sample:

    package com.function;
    
    import com.microsoft.azure.functions.annotation.*;
    import com.microsoft.azure.functions.*;
    
    public class Function {
    
        @FunctionName("BlobTrigger")
        public void run(
                @BlobTrigger(name = "trigger", path = "test/{fileName}", dataType = "binary", connection = "AzureWebJobsStorage") byte[] content,
                @BindingName("fileName") String fileName,
                final ExecutionContext context) {
                    context.getLogger().info("Blob: " + fileName + " -> Length: " + content.length);
        }
    }
    
    

    I use the "AzureWebJobsStorage" connection in my code, so I need to set a connection string in the local.settings.json:

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=storagetest789;AccountKey=*******w==;EndpointSuffix=core.windows.net",
        "FUNCTIONS_WORKER_RUNTIME": "java"
      }
    }
    
    

    Then, run the function locally and upload a file to the storage, I will get outputs as following:

    enter image description here


    Note:

    1. When you publish your app to Azure Function App, the settings in your local setting file will not be updated to the cloud. You need to manually update them.

    2. Please ensure that you have made your storage account accessible. If you enable firewall for your storage account, you need to add your client IP for local test, and allow trusted Microsoft Services to access your storage.

    enter image description here


    Update

    1. If you develop with VS Code, you can upload local settings from the command palette: F1 -> "Upload Local Settings"

    enter image description here

    1. You can also set application settings from Azure Portal enter image description here

    And then you can modify settings here:

    enter image description here