admin管理员组

文章数量:1122832

I have a Java azure function that has a blob trigger binding name and I process the file and finally write the file to a different location using block client. The following behavior is observed occasionally. A blob by the input file name is created in the location where the output file is created using blob client. Does this behavior have to do with binding name?

public void run(@BlobTrigger( name = "file", path = "container/{name}", dataType = "binary") byte[] fileCont, 
@bindingName("name") String name,
final ExecutionContext context) { } 

I have a Java azure function that has a blob trigger binding name and I process the file and finally write the file to a different location using block client. The following behavior is observed occasionally. A blob by the input file name is created in the location where the output file is created using blob client. Does this behavior have to do with binding name?

public void run(@BlobTrigger( name = "file", path = "container/{name}", dataType = "binary") byte[] fileCont, 
@bindingName("name") String name,
final ExecutionContext context) { } 
Share Improve this question edited Nov 22, 2024 at 14:02 Dasari Kamali 3,4032 gold badges4 silver badges10 bronze badges asked Nov 22, 2024 at 3:22 RamarRamar 1,5643 gold badges18 silver badges31 bronze badges 4
  • Can you share your code in the question? – Dasari Kamali Commented Nov 22, 2024 at 3:30
  • 1 public void run(@BlobTrigger( name = "file", path = "container/{name}", dataType = "binary") byte[] fileCont, @bindingName("name") String name, final ExecutionContext context) { } – Ramar Commented Nov 22, 2024 at 4:56
  • Can you able to run the blob trigger function code which you have shared above? – Dasari Kamali Commented Nov 22, 2024 at 9:55
  • @Ramar kindly edit your question to update your code in the question instead of comment section – Arko Commented Nov 22, 2024 at 12:25
Add a comment  | 

1 Answer 1

Reset to default 0

The BlobTrigger uses the {name} placeholder to get the blob's name, and @BindingName("name") stores it in the name variable for use, like setting the output file path.

To prevent overwriting, change the output blob name by modifying the input file's name, avoiding conflicts.

BlobTriggerJava.java :

import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import com.azure.storage.blob.*;
import java.io.ByteArrayInputStream;

public class BlobTriggerJava {
    @FunctionName("BlobTriggerJava")
    @StorageAccount("AZURE_STORAGE")
    public void run(
            @BlobTrigger(name = "file", path = "kamcontainer/{name}", dataType = "binary") byte[] fileCont,
            @BindingName("name") String name,
            final ExecutionContext context
    ) {
        context.getLogger().info("Processing blob. Name: " + name + ", Size: " + fileCont.length + " bytes.");
        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
                .connectionString(System.getenv("AZURE_STORAGE")) 
                .buildClient();
        String outputBlobPath = "outputblob/demokam-" + name; 
        BlobClient blobClient = blobServiceClient.getBlobContainerClient("outputblob")
                .getBlobClient("demokam-" + name);
        byte[] data = fileCont; 
        blobClient.upload(new ByteArrayInputStream(data), data.length, true);
        context.getLogger().info("Content successfully copied to " + outputBlobPath);
    }
}

local.setting.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<Storeconnecstring>",
    "FUNCTIONS_WORKER_RUNTIME": "java",
    "AZURE_STORAGE": "<Storeconnecstring>"
  }
}

pom.xml :

<dependencies>
    <dependency>
        <groupId>com.microsoft.azure.functions</groupId>
        <artifactId>azure-functions-java-library</artifactId>
        <version>1.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-blob</artifactId>
        <version>12.10.0</version> 
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.32</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.32</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.4.2</version>
        <scope>test</scope>
     </dependency>
     <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-core</artifactId>
         <version>2.23.4</version>
         <scope>test</scope>
    </dependency>
</dependencies>

Output :

The following blob trigger function ran successfully as shown below.

Input Container blob data :

Output Container blob data :

本文标签: javaAzure functionbinding name and blob clientStack Overflow