[英]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.