簡體   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