1818import com .azure .storage .common .sas .AccountSasSignatureValues ;
1919import com .google .common .net .MediaType ;
2020import com .microsoft .hydralab .common .entity .center .BlobProperty ;
21- import com .microsoft .hydralab .common .entity .common .EntityFileRelation .EntityType ;
2221import com .microsoft .hydralab .common .entity .common .SASData ;
2322import org .slf4j .Logger ;
2423import org .slf4j .LoggerFactory ;
@@ -52,7 +51,6 @@ public BlobStorageClient(BlobProperty blobProperty) {
5251 blobServiceClient = new BlobServiceClientBuilder ().connectionString (blobProperty .getConnection ()).buildClient ();
5352 fileLimitDay = blobProperty .getFileLimitDay ();
5453 cdnUrl = blobProperty .getCDNUrl ();
55- initContainer ();
5654 isAuthedBySAS = false ;
5755 isConnected = true ;
5856 }
@@ -70,7 +68,6 @@ private void buildClientBySAS(SASData sasData) {
7068 blobServiceClient = new BlobServiceClientBuilder ().endpoint (sasData .getEndpoint ()).credential (azureSasCredential ).buildClient ();
7169 fileLimitDay = sasData .getFileLimitDay ();
7270 cdnUrl = sasData .getCdnUrl ();
73- initContainer ();
7471 isConnected = true ;
7572 sasDataInUse = sasData ;
7673 }
@@ -82,18 +79,20 @@ private void checkBlobStorageClientUpdate() {
8279 }
8380 }
8481
85- private void initContainer () {
86- EntityType [] entityTypes = EntityType .values ();
87- for (EntityType entityType : entityTypes ) {
88- String containerName = entityType .blobConstant ;
89- try {
90- blobServiceClient .getBlobContainerClient (containerName );
91- classLogger .info ("Get a BlobContainerClient for container {}" , containerName );
92- } catch (BlobStorageException e ) {
93- classLogger .info ("Can't connect to container for {}. Try to create one!" , containerName );
94- blobServiceClient .createBlobContainerWithResponse (containerName , null , PublicAccessType .CONTAINER , Context .NONE );
82+ private BlobContainerClient getContainer (String containerName ) {
83+ BlobContainerClient blobContainerClient ;
84+ try {
85+ blobContainerClient = blobServiceClient .getBlobContainerClient (containerName );
86+ classLogger .info ("Get a BlobContainerClient for container {}" , containerName );
87+ if (!blobContainerClient .exists ()) {
88+ classLogger .info ("Container {} doesn't exist, will try to create it." , containerName );
89+ blobContainerClient .create ();
9590 }
91+ } catch (BlobStorageException e ) {
92+ classLogger .info ("Can't connect to container for {}. Try to create one!" , containerName );
93+ blobContainerClient = blobServiceClient .createBlobContainerWithResponse (containerName , null , PublicAccessType .CONTAINER , Context .NONE ).getValue ();
9694 }
95+ return blobContainerClient ;
9796 }
9897
9998 public SASData generateSAS (SASData .SASPermission sasPermission ) {
@@ -139,10 +138,8 @@ public String uploadBlobFromFile(File uploadFile, String containerName, String b
139138 if (logger == null ) {
140139 logger = classLogger ;
141140 }
142- BlobContainerClient blobContainerClient = blobServiceClient .getBlobContainerClient (containerName );
143- logger .info ("Get a BlobContainerClient for container {} for file {}" , containerName , uploadFile .getAbsoluteFile ());
144141
145- BlobClient blobClient = blobContainerClient .getBlobClient (blobFilePath );
142+ BlobClient blobClient = getContainer ( containerName ) .getBlobClient (blobFilePath );
146143 if (uploadFile .getName ().endsWith (MediaType .MP4_VIDEO .subtype ())) {
147144 BlobHttpHeaders headers = new BlobHttpHeaders ();
148145 headers .setContentType (MediaType .MP4_VIDEO .toString ());
@@ -172,8 +169,7 @@ public BlobProperties downloadFileFromBlob(File downloadToFile, String container
172169 if (!saveDir .exists ()) {
173170 cn .hutool .core .lang .Assert .isTrue (saveDir .mkdirs (), "mkdirs fail in downloadFileFromUrl" );
174171 }
175- BlobContainerClient blobContainerClient = blobServiceClient .getBlobContainerClient (containerName );
176- BlobClient blobClient = blobContainerClient .getBlobClient (blobFilePath );
172+ BlobClient blobClient = getContainer (containerName ).getBlobClient (blobFilePath );
177173 return blobClient .downloadToFile (downloadToFile .getAbsolutePath (), true );
178174 }
179175}
0 commit comments