I'm trying to create an Azure function that connects to a container that is not hard-coded, much like my connection.
public static void Run(
[BlobTrigger("Container-Name/ftp/{name}", Connection = "AzureWebJobsStorage")]Stream blobStream,
string name,
IDictionary<string, string> metaData,
TraceWriter log)
The connection property is able to get the connection value directly from local.settings.json. It does not appear that capability is an option for "container-name", or if so, not with the appended /ftp/{name}.
So, is there a way to set the container name based on settings for an Azure Function Blob Trigger?
You can define your function like this:
public static void Run(
[BlobTrigger("%mycontainername%/ftp/{name}", Connection = "AzureWebJobsStorage")]
Stream blobStream,
string name,
IDictionary<string, string> metaData,
TraceWriter log)
and then define an application setting called mycontainername to contain the actual container name.
Related
I need to add files to an Azure Storage Account using an Azure Function App with a Queuetrigger. But the container needs to be added dynamically. How is that possible?
public static async Task Run(
[QueueTrigger("activitylogevents", Connection = "StorageConnectionAppSetting")] Log activitylogevents,
Dynamic ==> [Blob("{dynamicc container}/dev/bronze/logs.json", FileAccess.Read)] Stream streamIn,
ILogger log)
{ ... Code doing stuff ... }
Thanks
You can make use of IBinder to dynamically define your BlobAttribute:
public static void MyFunction1(
[QueueTrigger("activitylogevents", Connection = "StorageConnectionAppSetting")] Log activitylogevents,
IBinder binderIn,
ILogger log)
{
var blobInAttribute = new BlobAttribute(myUrl, FileAccess.Read) { Connection = "StorageConnectionAppSetting" };
var streamIn = binderIn.Bind<Stream>(blobInAttribute);
//other code
}
Azure Service Bus trigger is used for Azure Functions for Topic and subscription, I wonder if Azure Service Bus trigger input and output support shared access signature (SAS)?
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-sas
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus
Output
[FunctionName(nameof(Order))]
public async Task<IActionResult> Order(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]
HttpRequest request,
[ServiceBus("%TopicName%", EntityType.Topic, Connection = "AzureServiceBus_ConnectionString")]
IAsyncCollector<Message> collector)
{
await collector.AddAsync(message);
}
Inupt
[FunctionName("ServiceBusQueueTriggerCSharp")]
public static void Run(
[ServiceBusTrigger("myqueue", AccessRights.Manage, Connection = "ServiceBusConnection")]
string myQueueItem,
Int32 deliveryCount,
DateTime enqueuedTimeUtc,
string messageId,
ILogger log)
{
}
Any idea?
If not supported, any workaround
I have an Azure Function which is using a ServiceBus trigger with topics. I recently added correlationId to the list of bindable inputs however the correlationId is not binding. I have confirmed that the correlationId is set corectly on the inbound message. All tooling/NuGets are on the latest bits (as of today).
The function also happens to be a durable function as is obvious in the following code snippet
[FunctionName("TopicHandler")]
public static void Run([ServiceBusTrigger("%TopicName%", "SubscriptionName", Connection = "ServiceBusConnection")]string messageBody,
string label,
string messageId,
string correlationId,
IDictionary<string, object> userProperties,
[OrchestrationClient] DurableOrchestrationClient starter,
ExecutionContext executionContext,
ILogger logger)
{
....
Documetnation seems to imply this should work:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus#trigger---message-metadata
However this is not completely explict as to whether it only works with queues or not.
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messages-payloads#message-routing-and-correlation
Any guidance would be appreciated before I create a custom property.
I'm following this documentation to create a trigger for a service bus queue.
I'd like to be able to access the message properties. I thought I could simply add Dictionary<string, object> properties to the parameter list like so:
public static void Run(
[ServiceBusTrigger(QueueName, Connection = "connectionSetting")]
// Message message,
string myQueueItem,
Int32 deliveryCount,
DateTime enqueuedTimeUtc,
string messageId,
string ContentType,
Dictionary<string,object> properties,
TraceWriter log)
But that throws:
Error indexing method 'Program.Run'. Microsoft.Azure.WebJobs.Host:
Cannot bind parameter 'properties' to type Dictionary`2. Make sure the
parameter Type is supported by the binding.
Here is a list of the possible parameter bindings. What am I getting wrong?
Update:
I tried changing the singature to
public static void Run(
[ServiceBusTrigger(QueueName, Connection = "connectionSetting")]
// Message message,
string myQueueItem,
Int32 deliveryCount,
DateTime enqueuedTimeUtc,
string messageId,
string ContentType,
IDictionary<string, object> properties,
TraceWriter log)
It produces the same error:
Error indexing method 'Program.Run'. Microsoft.Azure.WebJobs.Host:
Cannot bind parameter 'properties' to type IDictionary
For function v2 runtime, the name of the parameter has changed to UserProperties
To fix the error, update the parameter to the following:
IDictionary<string, object> UserProperties
Here is the related code from Service Bus extension.
https://github.com/Azure/azure-webjobs-sdk/blob/42a711763ddecca9df4caae9c7dc5fe16178880c/src/Microsoft.Azure.WebJobs.ServiceBus/Triggers/ServiceBusTriggerBinding.cs#L127
IDictionary<string, object> properties
Update:
for version 2 use the following bindings:
public static void Run(
[ServiceBusTrigger(QueueName, Connection = "connectionSetting")]
Message message,
string label,
Int32 deliveryCount,
DateTime enqueuedTimeUtc,
string messageId,
string ContentType,
ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {Encoding.UTF8.GetString(message.Body)}");
var userProperties = message.UserProperties;
}
Given the following topic trigger:
[FunctionName("eventhandler")]
public static Task Run([ServiceBusTrigger("domain.event", "domain.subscription", Connection = "QueueConnection")]
BrokeredMessage mySbMsg,
IBinder binder,
TraceWriter traceWriter,
CancellationToken cancellationToken)
How can i retrieve the full endpoint of the Service Bus Trigger without needing to add additional custom settings in application config? Is there any environment variables i can retrieve within the run function?
To get an environment variable or an app setting value, use System.Environment.GetEnvironmentVariable.
public static string GetEnvironmentVariable(string name)
{
return name + ": " +
System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}
https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#environment-variables