Skip to content

Commit

Permalink
Create the blob container if it doesn't exist when uploading (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
hydraxman committed Feb 14, 2023
1 parent da425af commit 7395cc4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ application-local.yml
/application.yml
/application-*.yml

/local_debug
/local_debug
.env
14 changes: 14 additions & 0 deletions azure-pipelines-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ stages:
command: 'custom'
workingDir: 'react'
customCommand: 'run pub'
- task: PowerShell@2
displayName: Generate Env File
inputs:
targetType: 'inline'
script: |
New-Item -Path common/src/test/resources -Name ".env" -ItemType "file" -Value "BLOB_CONNECTION_STRING = $(BLOB_CONNECTION_STRING)"
workingDirectory: '$(Build.Repository.LocalPath)'
- task: PowerShell@2
displayName: Set center/agent version
inputs:
Expand Down Expand Up @@ -65,6 +72,13 @@ stages:
jdkVersionOption: '1.11'
sonarQubeRunAnalysis: false
spotBugsAnalysis: false
- task: PowerShell@2
displayName: Delete Env File
inputs:
targetType: 'inline'
script: |
Remove-Item -Path common/src/test/resources/.env -Force
workingDirectory: '$(Build.Repository.LocalPath)'
- task: PublishCodeCoverageResults@1
displayName: Publich Code Coverage
inputs:
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
compile 'com.android.tools.ddms:ddmlib:27.0.2'
compile group: 'junit', name: 'junit', version: '4.12'
testCompile 'com.github.stefanbirkner:system-rules:1.19.0'
testCompile 'io.github.cdimascio:java-dotenv:5.1.3'

compile project(":sdk")
compile project(":taps_to_cases:runner")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.azure.storage.common.sas.AccountSasSignatureValues;
import com.google.common.net.MediaType;
import com.microsoft.hydralab.common.entity.center.BlobProperty;
import com.microsoft.hydralab.common.entity.common.EntityFileRelation.EntityType;
import com.microsoft.hydralab.common.entity.common.SASData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -52,7 +51,6 @@ public BlobStorageClient(BlobProperty blobProperty) {
blobServiceClient = new BlobServiceClientBuilder().connectionString(blobProperty.getConnection()).buildClient();
fileLimitDay = blobProperty.getFileLimitDay();
cdnUrl = blobProperty.getCDNUrl();
initContainer();
isAuthedBySAS = false;
isConnected = true;
}
Expand All @@ -70,7 +68,6 @@ private void buildClientBySAS(SASData sasData) {
blobServiceClient = new BlobServiceClientBuilder().endpoint(sasData.getEndpoint()).credential(azureSasCredential).buildClient();
fileLimitDay = sasData.getFileLimitDay();
cdnUrl = sasData.getCdnUrl();
initContainer();
isConnected = true;
sasDataInUse = sasData;
}
Expand All @@ -82,18 +79,20 @@ private void checkBlobStorageClientUpdate() {
}
}

private void initContainer() {
EntityType[] entityTypes = EntityType.values();
for (EntityType entityType : entityTypes) {
String containerName = entityType.blobConstant;
try {
blobServiceClient.getBlobContainerClient(containerName);
classLogger.info("Get a BlobContainerClient for container {}", containerName);
} catch (BlobStorageException e) {
classLogger.info("Can't connect to container for {}. Try to create one!", containerName);
blobServiceClient.createBlobContainerWithResponse(containerName, null, PublicAccessType.CONTAINER, Context.NONE);
private BlobContainerClient getContainer(String containerName) {
BlobContainerClient blobContainerClient;
try {
blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);
classLogger.info("Get a BlobContainerClient for container {}", containerName);
if (!blobContainerClient.exists()) {
classLogger.info("Container {} doesn't exist, will try to create it.", containerName);
blobContainerClient.create();
}
} catch (BlobStorageException e) {
classLogger.info("Can't connect to container for {}. Try to create one!", containerName);
blobContainerClient = blobServiceClient.createBlobContainerWithResponse(containerName, null, PublicAccessType.CONTAINER, Context.NONE).getValue();
}
return blobContainerClient;
}

public SASData generateSAS(SASData.SASPermission sasPermission) {
Expand Down Expand Up @@ -139,10 +138,8 @@ public String uploadBlobFromFile(File uploadFile, String containerName, String b
if (logger == null) {
logger = classLogger;
}
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);
logger.info("Get a BlobContainerClient for container {} for file {}", containerName, uploadFile.getAbsoluteFile());

BlobClient blobClient = blobContainerClient.getBlobClient(blobFilePath);
BlobClient blobClient = getContainer(containerName).getBlobClient(blobFilePath);
if (uploadFile.getName().endsWith(MediaType.MP4_VIDEO.subtype())) {
BlobHttpHeaders headers = new BlobHttpHeaders();
headers.setContentType(MediaType.MP4_VIDEO.toString());
Expand Down Expand Up @@ -172,8 +169,7 @@ public BlobProperties downloadFileFromBlob(File downloadToFile, String container
if (!saveDir.exists()) {
cn.hutool.core.lang.Assert.isTrue(saveDir.mkdirs(), "mkdirs fail in downloadFileFromUrl");
}
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);
BlobClient blobClient = blobContainerClient.getBlobClient(blobFilePath);
BlobClient blobClient = getContainer(containerName).getBlobClient(blobFilePath);
return blobClient.downloadToFile(downloadToFile.getAbsolutePath(), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.microsoft.hydralab.common.entity.common.SASData;
import com.microsoft.hydralab.common.test.BaseTest;
import com.microsoft.hydralab.common.util.ThreadUtils;
import io.github.cdimascio.dotenv.Dotenv;
import org.junit.jupiter.api.*;
import org.junit.platform.commons.util.StringUtils;

Expand All @@ -13,13 +14,21 @@
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class BlobStorageClientTest extends BaseTest {
String connectionString = "";
BlobStorageClient blobStorageClient;
File sampleFile = new File("src/test/resources/uitestsample.ipa");
BlobProperty property = new BlobProperty();

@BeforeAll
void initBlob() {
String connectionString = null;
try {
Dotenv dotenv = Dotenv.load();
connectionString = dotenv.get("BLOB_CONNECTION_STRING");
logger.info("Get connectionString from env file successfully!");
} catch (Exception e) {
logger.error("Get connectionString from env file failed!", e);
}

property.setConnection(connectionString);
property.setFileLimitDay(6);
property.setSASExpiryTimeAgent(30);
Expand Down Expand Up @@ -50,9 +59,8 @@ void downloadFileFromBlob() {
BlobProperties properties = blobStorageClient.downloadFileFromBlob(sampleFile_copy, DeviceNetworkBlobConstants.PKG_BLOB_NAME, "test/unit/" + sampleFile.getName());
logger.info("Download sample file finished, properties: " + properties);
Assertions.assertNotNull(properties, "Download File Failed!");
if (sampleFile_copy.exists()) {
sampleFile_copy.delete();
}
Assertions.assertTrue(sampleFile_copy.exists(), "Download File Failed!");
sampleFile_copy.delete();
}
}

Expand Down

0 comments on commit 7395cc4

Please sign in to comment.