Azure Storage Blob sdk V8과 V12
Blob 작업을 처리한 후 관련해서 업데이트 된 내용이 있는지 확인해 보았는데 Azure SDK V12가 지원되는 것을 알게되었다.
V12 업그레이드 된 이유는 링크를 달아 두었으니 확인해 보면 될 것 같다.
나는 V8을 사용하고 있어 V12 내용을 확인하며 기존에 사용하던 방법과 다른 점에 대해서 간단히 정리하려고한다.
dependency
v8 | v12 |
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>azure-storage</artifactId> <version>8.4.0</version> </dependency> |
<dependency> <groupId>com.azure</groupId> <artifactId>azure-storage-blob</artifactId> <version>12.0.0</version> </dependency> |
package
v8 | v12 |
com.microsoft.azure.storage | com.azure.storage.blob |
변경된 핵심 클래스
기존 동기 클래스가 변경 되었으며, 비동기 클래스가 추가됨.
연결문자열 구성
V8
@Configuration
public class BlobConfig {
@Value("${blob.connection.string}")
private String credential;
@Bean
CloudBlobClient cloudBlobClient() throws URISyntaxException, InvalidKeyException {
CloudStorageAccount parse = CloudStorageAccount.parse(credential);
return parse.createCloudBlobClient();
}
}
V12
BlobServiceClient : Azure Storage 서비스 리소스 및 Blob 컨테이너를 조작할 수 있습니다. 스토리지 계정은 최상위 네임스페이스를 제공합니다.
BlobContainerClient : Azure Storage 컨테이너와 해당 Blob을 조작할 수 있습니다.
@Configuration
public class BlobConfig {
@Value("${blob.connection.string}")
private String credential;
@Bean
BlobServiceClient blobServiceClient() {
BlobServiceClientBuilder builder = new BlobServiceClientBuilder();
return builder.connectionString(credential).buildClient();
}
@Bean
BlobContainerClient blobContainerClient() {
BlobContainerClientBuilder builder = new BlobContainerClientBuilder();
builder.connectionString(credential)
.containerName("web-json");
return builder.buildClient();
}
}
업로드
V8
@Service
public class BlobUpLoadService {
@Autowired
private CloudBlobClient client;
public void upload(File file) throws URISyntaxException, StorageException, IOException {
CloudBlobContainer containerReference = client.getContainerReference("containerName");
CloudBlockBlob blockBlobReference = containerReference.getBlockBlobReference("blob path name");
try (FileInputStream fileInputStream = new FileInputStream(file)) {
blockBlobReference.upload(fileInputStream, file.length());
}
}
}
V12
@Service
public class BlobService {
@Autowired
private BlobServiceClient blobServiceClient;
public void upload() {
BlobClient blobClient = blobServiceClient.getBlobContainerClient("containerName").getBlobClient("blob path name");
BlobHttpHeaders headers = new BlobHttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_VALUE);
blobClient.setHttpHeaders(headers);
blobClient.uploadFromFile(file1.getAbsolutePath(),true);
}
}
+컨테이너 이름 규칙
문자 또는 숫자로 시작해야 하며 문자, 숫자 및 대시(-) 문자를 포함할 수 있습니다.
모든 hyphen 문자는 문자 또는 숫자 바로 앞뒤에 와야 하며 연속해서 사용할 수 없습니다.
문자는 소문자여야 합니다.
길이는 3자 이상, 63자 이하여야 합니다.
버전별 상이한 내용은 아래를 참고하면 좋을 것 같다.
참고 :
https://github.com/Azure/azure-storage-java/blob/master/V12%20Upgrade%20Story.md
https://stackoverflow.com/questions/64020966/azure-blobclient-vs-cloudblobclient
https://elcamino.cloud/articles/2020-03-30-azure-storage-blobs-net-sdk-v12-upgrade-guide-and-tips.html