원인
- Azure 클라우드 이용 인프라 구축 동안 Spring 서버를 이용하여 Blob Storage에 저장할 경우 저장된 파일에 접근시 다운로드 되는 현상이 발생함.
- Spring 서버 통해서 업로드된 파일의 Content Type 이 application/octet-stream 으로 저장되는 것을 확인, Azure Web을 이용해서 업로드시 정상적으로 업로드 및 접근이 가능함
문제
- Azure 에서 Spring에 제공해주는 SDK를 통해서 파일 업로드를 할 경우 Content Type을 명시해주지 않으면 Default Type이 application/octet-stream 으로 업로드 되는 것을 확인함.
- 브라우저 혹은 Azure Storage에서 지원하지 않는 파일형식에 접근할 경우 해당 파일이 '다운로드' 되는 것을 확인함
해결
- Spring 에서 Azure Storage로 파일 업로드시 파라미터로 넘어온 파일의 Content Type 을 명시해줌 -> BlobHttpHeaders 클래스 이용하여 명시해줌
- 기존 BlobClient.upload()를 이용하여 업로드 하였으나, Content Type이 명시된 BlobHttpHeaders 객체를 함께 보내기 위해 BlobClient.uploadWithResponse() 를 이용하여 업로드 진행함
public String uploadToAzureStorage (MultipartFile file, String directoryPath, String containerName) {
String fileName = createFileName(file.getOriginalFilename());
BlobContainerClient containerClient = new BlobContainerClientBuilder()
.connectionString(CONNECTION_STRING)
.containerName(containerName)
.buildClient();
// Azure Storage Default Content Type 은 application/octet-stream 이기때문에 넘어온 파일의 Content Type 로 바인딩해줘야함
BlobHttpHeaders blobHeader = new BlobHttpHeaders().setContentType(file.getContentType());
BlobClient blob = containerClient.getBlobClient(directoryPath + fileName);
try {
blob.uploadWithResponse(file.getInputStream(), file.getSize(), null, blobHeader, null, null, null, null, null);
} catch (IOException e) {
e.printStackTrace();
}
return fileName;