简体   繁体   中英

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

Program Flow:

I have a set of high resolution image files a user uploads as part of his "Order". At the backend I'm moving those files to a permanent storage container (from a temporary container). This process is working fine.

New Requirement:

Create a thumbnail of the imge after moving it to the permanent storage container.

Following function is invoked to move the file and create the 300x300 image thumbnail:

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;
    }

Function to generate sas token:

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;
    }

Function that moves the file to the new container:

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();
    }

The Problem:

Issue is when I try to read the file from the path (Exception: Can't read input file):

调试

If I try to go directly to the URL from the browser, I get AuthenticationFailed :

浏览器

Does anybody have a solution for this? I'm not an expert with Azure/ handling cloud storages and stuck on this for quite a while now. Any help would be really appreciated.

Thanks!

There are a few things I would try here:


Resources:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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