Skip to content

Commit 7395cc4

Browse files
authored
Create the blob container if it doesn't exist when uploading (#267)
1 parent da425af commit 7395cc4

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ application-local.yml
6464
/application.yml
6565
/application-*.yml
6666

67-
/local_debug
67+
/local_debug
68+
.env

azure-pipelines-ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ stages:
3636
command: 'custom'
3737
workingDir: 'react'
3838
customCommand: 'run pub'
39+
- task: PowerShell@2
40+
displayName: Generate Env File
41+
inputs:
42+
targetType: 'inline'
43+
script: |
44+
New-Item -Path common/src/test/resources -Name ".env" -ItemType "file" -Value "BLOB_CONNECTION_STRING = $(BLOB_CONNECTION_STRING)"
45+
workingDirectory: '$(Build.Repository.LocalPath)'
3946
- task: PowerShell@2
4047
displayName: Set center/agent version
4148
inputs:
@@ -65,6 +72,13 @@ stages:
6572
jdkVersionOption: '1.11'
6673
sonarQubeRunAnalysis: false
6774
spotBugsAnalysis: false
75+
- task: PowerShell@2
76+
displayName: Delete Env File
77+
inputs:
78+
targetType: 'inline'
79+
script: |
80+
Remove-Item -Path common/src/test/resources/.env -Force
81+
workingDirectory: '$(Build.Repository.LocalPath)'
6882
- task: PublishCodeCoverageResults@1
6983
displayName: Publich Code Coverage
7084
inputs:

common/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
compile 'com.android.tools.ddms:ddmlib:27.0.2'
2424
compile group: 'junit', name: 'junit', version: '4.12'
2525
testCompile 'com.github.stefanbirkner:system-rules:1.19.0'
26+
testCompile 'io.github.cdimascio:java-dotenv:5.1.3'
2627

2728
compile project(":sdk")
2829
compile project(":taps_to_cases:runner")

common/src/main/java/com/microsoft/hydralab/common/util/blob/BlobStorageClient.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.azure.storage.common.sas.AccountSasSignatureValues;
1919
import com.google.common.net.MediaType;
2020
import com.microsoft.hydralab.common.entity.center.BlobProperty;
21-
import com.microsoft.hydralab.common.entity.common.EntityFileRelation.EntityType;
2221
import com.microsoft.hydralab.common.entity.common.SASData;
2322
import org.slf4j.Logger;
2423
import 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
}

common/src/test/java/com/microsoft/hydralab/common/util/blob/BlobStorageClientTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.microsoft.hydralab.common.entity.common.SASData;
66
import com.microsoft.hydralab.common.test.BaseTest;
77
import com.microsoft.hydralab.common.util.ThreadUtils;
8+
import io.github.cdimascio.dotenv.Dotenv;
89
import org.junit.jupiter.api.*;
910
import org.junit.platform.commons.util.StringUtils;
1011

@@ -13,13 +14,21 @@
1314
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1415
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1516
class BlobStorageClientTest extends BaseTest {
16-
String connectionString = "";
1717
BlobStorageClient blobStorageClient;
1818
File sampleFile = new File("src/test/resources/uitestsample.ipa");
1919
BlobProperty property = new BlobProperty();
2020

2121
@BeforeAll
2222
void initBlob() {
23+
String connectionString = null;
24+
try {
25+
Dotenv dotenv = Dotenv.load();
26+
connectionString = dotenv.get("BLOB_CONNECTION_STRING");
27+
logger.info("Get connectionString from env file successfully!");
28+
} catch (Exception e) {
29+
logger.error("Get connectionString from env file failed!", e);
30+
}
31+
2332
property.setConnection(connectionString);
2433
property.setFileLimitDay(6);
2534
property.setSASExpiryTimeAgent(30);
@@ -50,9 +59,8 @@ void downloadFileFromBlob() {
5059
BlobProperties properties = blobStorageClient.downloadFileFromBlob(sampleFile_copy, DeviceNetworkBlobConstants.PKG_BLOB_NAME, "test/unit/" + sampleFile.getName());
5160
logger.info("Download sample file finished, properties: " + properties);
5261
Assertions.assertNotNull(properties, "Download File Failed!");
53-
if (sampleFile_copy.exists()) {
54-
sampleFile_copy.delete();
55-
}
62+
Assertions.assertTrue(sampleFile_copy.exists(), "Download File Failed!");
63+
sampleFile_copy.delete();
5664
}
5765
}
5866

0 commit comments

Comments
 (0)