无法使用java azure函数从邮递员表单数据获取图像来存储Blob存储

问题描述 投票:0回答:1

enter image description here我正在使用java azure函数,因为我想从邮递员表单数据获取图像,但无法以正确的格式获取图像,并且图像大小增加,但它可以在邮递员二进制文件中以正确的格式和实际大小完成,但在我的工作我想通过邮递员表单数据获取图像

@FunctionName("CreateItemImage")
    public HttpResponseMessage createItemImage(@HttpTrigger(name = "request", methods = {
            HttpMethod.POST }, route = "menuimage", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<byte[]> request,
            ExecutionContext context) {

        String connectionString = "keys";
        String containerName = "menuitemimage";
        String blobName = UUID.randomUUID().toString();
        String contentType = "image/jpeg";
        String localPath = "D:/Photo/three/"+blobName+".jpeg"; 
        
           
        try {
            byte[] imageBytes= request.getBody();

            if (imageBytes == null|| imageBytes.length == 0) {
                return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
                        .body("Image content is missing in the request.").build();
            }
            
             Path localFilePath = Paths.get(localPath);
                Files.write(localFilePath, imageBytes);

            BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString)
                    .buildClient();
            BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.getBlobClient(blobName);

            ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);

            byte[] md5Hash = calculateMD5Hash(imageBytes);

            BlobHttpHeaders headers = new BlobHttpHeaders().setContentType(contentType).setContentMd5(md5Hash);

            blobClient.upload(inputStream, imageBytes.length);
            blobClient.setHttpHeaders(headers);

            context.getLogger().info("Image uploaded successfully with Content-Type: " + contentType);

            return request.createResponseBuilder(HttpStatus.OK)
                    .body("Image uploaded successfully " +blobClient.getBlobUrl())
                    [](https://i.stack.imgur.com/ZcCZL.png)
                    .build();
        } catch (Exception e) {
            context.getLogger().severe("Error: " + e.getMessage());
            return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + e.getMessage())
                    .build();
        }
        
        
    }
    private byte[] calculateMD5Hash(byte[] data) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data);
        return md.digest();

    }

我想以正确的大小和格式以邮递员表单数据上传图像,不仅将图像存储在blob存储中,我还将来自邮递员的即将发布的图像存储在本地,它的格式也不正确,但使用二进制文件

java azure-functions postman azure-blob-storage form-data
1个回答
0
投票

我对上面的代码做了一些小更改,Azure Function 配置为接受

multipart/form-data
内容。您可以通过在
consumes
注释中指定正确的
@HttpTrigger
值来完成此操作。

BlobTriggerJava:

public HttpResponseMessage createItemImage(
    @HttpTrigger(
        name = "request",
        methods = { HttpMethod.POST },
        route = "menuimage",
        authLevel = AuthorizationLevel.ANONYMOUS,
        consumes = "multipart/form-data"
    ) HttpRequestMessage<FormData> request, ExecutionContext context) {

    String connectionString = "your_connection_string";
    String containerName = "menuitemimage";
    String blobName = UUID.randomUUID().toString();
    String contentType = "image/jpeg";

    try {
        FormData formData = request.getBody();
        
        if (!formData.containsKey("image")) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
                .body("Image is missing in the form-data request.").build();
        }

        // Get the image data from the form-data
        byte[] imageBytes = formData.get("image").get();

        // Your code to store the image in localPath, blob storage, or any other processing

        return request.createResponseBuilder(HttpStatus.OK)
            .body("Image uploaded successfully " + blobClient.getBlobUrl())
            .build();
    } catch (Exception e) {
        context.getLogger().severe("Error: " + e.getMessage());
        return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
            .body("Error: " + e.getMessage())
            .build();
    }
}
  • get("image").get()
    方法使用键“image”从表单数据中检索图像数据。

enter image description here

在 Postman 中,当您使用“multipart/form-data”内容类型发出 POST 请求时,请包含一个名为“test”的键并将图像文件附加到其中,测试图像将由您的 Azure 函数正确处理。

enter image description here

在容器中: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.