Reputation: 181
I followed a solution to my problem from this below thread
https://stackoverflow.com/questions/62596741/azure-function-not-working-cannot-declare-namespace-in-script-code
I created a code locally and published it online - it was working on both - local & Azure function.
Below code snippet isworking on both - local & Azure function. Notice that filename is hardcoded.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Azure.Storage.Blobs;
namespace FunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
string filename = "success.png";
string storageconnstring = "**********";
BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("demo");
BlobClient blobClient = containerClient.GetBlobClient(filename);
var blobUri = blobClient.Uri;
BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient("demo-copy");//This is the container where we want to copy the blob
BlobClient targetBlobClient = targetContainerClient.GetBlobClient(filename);
await targetBlobClient.StartCopyFromUriAsync(blobUri);
return new OkObjectResult(responseMessage);
}
}
}
Now this code is working only locally - the ONLY change I did was that now you can pass the filename. What I am doing wrong?
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Azure.Storage.Blobs;
namespace FunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string filename = req.Query["filename"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
filename = filename ?? data?.filename;
string responseMessage = string.IsNullOrEmpty(filename)
? "This HTTP triggered function executed successfully. Pass a file name in the query string or in the request body for initiating a temporary copy"
: $"This HTTP triggered function executed successfully.";
string storageconnstring = "DefaultEndpointsProtocol=https;AccountName=mainices;AccountKey=lrlWVXi9tWDfkjv6XMcMgylPG2fU78nOcK3AwJkRJTBKDQ4FdxJkieYiGBhfFTYULl+IHey0OJASpkHlg25Eaw==;EndpointSuffix=core.windows.net";
BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("demo");
BlobClient blobClient = containerClient.GetBlobClient(filename);
var blobUri = blobClient.Uri;
BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient("demo-copy");//This is the container where we want to copy the blob
BlobClient targetBlobClient = targetContainerClient.GetBlobClient(filename);
await targetBlobClient.StartCopyFromUriAsync(blobUri);
return new OkObjectResult(targetBlobClient.Uri);
}
}
}
Upvotes: 0
Views: 458
Reputation: 18387
As you defined the autorization level as functions, it is expecting a function key:
[HttpTrigger(AuthorizationLevel.Function
you'll need to add it to your querystring:
https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>?code=<API_KEY>
PS: do not forget to pass the filename parameter too
Upvotes: 2