[英]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/处理云存储的专家,并且在这方面坚持了很长时间。 任何帮助将非常感激。
谢谢!
我会在这里尝试一些事情:
资源:
https://docs.microsoft.com/en-us/azure/storage/blobs/anonymous-read-access-configure?tabs=portal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.