[英]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:调用以下函数来移动文件并创建 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;
}
Function to generate sas token:生成 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;
}
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 :如果我尝试从浏览器直接访问 URL,我会得到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.
我不是 Azure/处理云存储的专家,并且在这方面坚持了很长时间。 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://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://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 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.