I recently created an HTTP Triggered Java Azure function to handle some of my backend tasks. This Azure-Functions main purpose was to accept/read in image files wrapped in a javascript FormData Object and upload the files to my azure blob storage.
I've been searching online but wasn't able to find a working example of how to read in a File from a FormData Object in java? I've seen some examples of this being done with azure functions in C# and node.js.
Here is the link --> Azure functions - How to read form data
So there has to be means of doing this in Java, does anyone have or can anyone provide a working example ?
public class Function {
@FunctionName("HttpTrigger-Java")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods =
{HttpMethod.POST}) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
String postRequest = request.getBody();
//HELP: I need to get the FormData object from the postRequest some how...
FormData formDataObj = postRequest.getFormData();
//HELP: Then I need to get my image file from the FormData Object...
File sourceFile = formDataObj.getImageFile();
try {
// Parse the connection string
storageAccount = CloudStorageAccount.parse(storageConnectionString);
// get blob client
blobClient = storageAccount.createCloudBlobClient();
// get container name
container = blobClient.getContainerReference(containerName);
//Getting a blob reference
blob = container.getBlockBlobReference(sourceFile.getName());
//Creating blob and uploading file to it
blob.uploadFromFile(sourceFile.getAbsolutePath());
} catch (StorageException ex) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("There was an error with Storage Account.").build();
}
return request.createResponseBuilder(HttpStatus.OK).body("Images Uploaded Successfully!, ").build();
}
}