繁体   English   中英

Springboot:将文件移动到存储容​​器(Azure)后无法读取文件

[英]Springboot: Unable to read the file after moving it to a storage container (Azure)

程序流程:

我有一组用户上传的高分辨率图像文件作为他的“订单”的一部分。 在后端,我将这些文件移动到永久存储容器(从临时容器)。 这个过程运行良好。

新要求:

将图像移动到永久存储容器后创建图像的缩略图。

调用以下函数来移动文件并创建 300x300 图像缩略图:

public void moveFilesToNewContainer(OrderFile of, Order savedOrder, OrderFileRepository orderFileRepository) {
        int timestampLength = String.valueOf(System.currentTimeMillis()).length()+1;
        try {
            String filePath = of.getFilePath()
                    .replace(String.format("https://%s.blob.core.windows.net/%s/",
                            asAccountName, asTemporaryContainerName), "");

            String extension = CommonUtils.getFileExtension(filePath);
            String fileName = filePath.substring(timestampLength);

            String sasToken = fileService.generateSasToken(asTemporaryContainerName, filePath);
            String newFilePath = fileService.moveFileToNewPlace(asTemporaryContainerName, asFilesContainerName,
                    filePath, String.format("%s/%s", savedOrder.getOrderNo(), fileName), sasToken);

            // If file is of image type, generate a 300x300 thumbnail with _preview appended to its name
            if (CommonUtils.isImageType(extension)) {
                String pathWithoutExt = CommonUtils.removeFileExtension(newFilePath);
                Image thumbnailImage = generateImageThumbnail(newFilePath + '?' + sasToken);
                File destFileName = new File(pathWithoutExt + "_preview" + extension);
                ImageIO.write((RenderedImage) thumbnailImage, extension, destFileName);
            }

            of.setFilePath(URLDecoder.decode(newFilePath, StandardCharsets.UTF_8.name()));
            orderFileRepository.save(of);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
    }

public Image generateImageThumbnail(String filePath) {
        try {
            File file = new File(filePath);
            BufferedImage image = ImageIO.read(file);
            Image newImage = image.getScaledInstance(300, 300, Image.SCALE_DEFAULT);
            return newImage;
        } catch(IOException e){
            System.out.println("Error: " + e);
        }
        return null;
    }

生成 sas 令牌的函数:

public String generateSasToken(String srcContainerStr, String srcBlobName) {
        BlobServiceClient blobServiceClient = getTmpBlobServiceClient();

        BlobContainerClient srcContainerClient = blobServiceClient.getBlobContainerClient(srcContainerStr);

        BlobClient srcBlobClient = srcContainerClient.getBlobClient(srcBlobName);
        BlobServiceSasSignatureValues sas = new BlobServiceSasSignatureValues(OffsetDateTime.now().plusMinutes(10),
                BlobContainerSasPermission.parse("r")
                        .setDeletePermission(true).setReadPermission(true).setWritePermission(true));
        String sasToken = srcBlobClient.generateSas(sas);
        return sasToken;
    }

将文件移动到新容器的函数:

public String moveFileToNewPlace(String srcContainerStr, String destContainerStr,
                                     String srcBlobName, String destBlobName, String sasToken) {
        // Create a BlobServiceClient object which will be used to create a container client
        BlobServiceClient blobServiceClient = getTmpBlobServiceClient();

        BlobContainerClient srcContainerClient = blobServiceClient.getBlobContainerClient(srcContainerStr);
        BlobContainerClient destContainer = blobServiceClient.getBlobContainerClient(destContainerStr);

        BlobClient srcBlobClient = srcContainerClient.getBlobClient(srcBlobName);

        BlobClient destBlobClient = destContainer.getBlobClient(destBlobName);

        // Copy to new location
        destBlobClient.beginCopy(String.format("%s?%s", srcBlobClient.getBlobUrl(), sasToken), null);
        // Delete old file
        srcBlobClient.delete();

        return destBlobClient.getBlobUrl();
    }

问题:

问题是当我尝试从路径读取文件时(例外:无法读取输入文件):

调试

如果我尝试从浏览器直接访问 URL,我会得到AuthenticationFailed

浏览器

有人对此有解决方案吗? 我不是 Azure/处理云存储的专家,并且在这方面坚持了很长时间。 任何帮助将非常感激。

谢谢!

我会在这里尝试一些事情:

  • 如果您想通过 URL 访问云存储文件而不是在同一个专用网络上,请授予“公共”权限。 您还应该能够在 YAML 文件或 Azure GUI 中进行配置
  • 至于路径,请尝试在 newFilePath 中使用反斜杠而不是正斜杠; 另外,尝试使用“虚拟目录”前缀
  • 请务必遵循本指南: https ://docs.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-storage

资源:

https://docs.microsoft.com/en-us/azure/storage/blobs/anonymous-read-access-configure?tabs=portal

https://azure.microsoft.com/en-us/updates/choose-to-allow-or-disallow-blob-public-access-on-azure-storage-accounts/

https://docs.microsoft.com/en-us/azure/storage/files/storage-troubleshoot-windows-file-connection-problems?tabs=azure-portal

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM