use of com.microsoft.azure.storage.blob.CloudBlobClient in project elasticsearch by elastic.
the class AzureStorageServiceImpl method blobExists.
@Override
public boolean blobExists(String account, LocationMode mode, String container, String blob) throws URISyntaxException, StorageException {
// Container name must be lower case.
CloudBlobClient client = this.getSelectedClient(account, mode);
CloudBlobContainer blobContainer = client.getContainerReference(container);
if (blobContainer.exists()) {
CloudBlockBlob azureBlob = blobContainer.getBlockBlobReference(blob);
return SocketAccess.doPrivilegedException(azureBlob::exists);
}
return false;
}
use of com.microsoft.azure.storage.blob.CloudBlobClient in project azure-sdk-for-java by Azure.
the class VirtualMachineScaleSetOperationsTests method canUpdateVirtualMachineScaleSetWithExtensionProtectedSettings.
@Test
public void canUpdateVirtualMachineScaleSetWithExtensionProtectedSettings() throws Exception {
final String vmssName = generateRandomResourceName("vmss", 10);
final String uname = "jvuser";
final String password = "123OData!@#123";
ResourceGroup resourceGroup = this.resourceManager.resourceGroups().define(RG_NAME).withRegion(REGION).create();
StorageAccount storageAccount = this.storageManager.storageAccounts().define(generateRandomResourceName("stg", 15)).withRegion(REGION).withExistingResourceGroup(resourceGroup).create();
List<StorageAccountKey> keys = storageAccount.getKeys();
Assert.assertNotNull(keys);
Assert.assertTrue(keys.size() > 0);
String storageAccountKey = keys.get(0).value();
final String storageConnectionString = String.format("DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s", storageAccount.name(), storageAccountKey);
// Get the script to upload
//
InputStream scriptFileAsStream = VirtualMachineScaleSetOperationsTests.class.getResourceAsStream("/install_apache.sh");
// Get the size of the stream
//
int fileSize;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
int bytesRead;
while ((bytesRead = scriptFileAsStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileSize = outputStream.size();
outputStream.close();
// Upload the script file as block blob
//
URI fileUri;
if (IS_MOCKED) {
fileUri = new URI("http://nonexisting.blob.core.windows.net/scripts/install_apache.sh");
} else {
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient cloudBlobClient = account.createCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.getContainerReference("scripts");
container.createIfNotExists();
CloudBlockBlob blob = container.getBlockBlobReference("install_apache.sh");
blob.upload(scriptFileAsStream, fileSize);
fileUri = blob.getUri();
}
List<String> fileUris = new ArrayList<>();
fileUris.add(fileUri.toString());
Network network = this.networkManager.networks().define(generateRandomResourceName("vmssvnet", 15)).withRegion(REGION).withExistingResourceGroup(resourceGroup).withAddressSpace("10.0.0.0/28").withSubnet("subnet1", "10.0.0.0/28").create();
VirtualMachineScaleSet virtualMachineScaleSet = this.computeManager.virtualMachineScaleSets().define(vmssName).withRegion(REGION).withExistingResourceGroup(resourceGroup).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0).withExistingPrimaryNetworkSubnet(network, "subnet1").withoutPrimaryInternetFacingLoadBalancer().withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(uname).withRootPassword(password).withUnmanagedDisks().withNewStorageAccount(generateRandomResourceName("stg", 15)).withExistingStorageAccount(storageAccount).defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", fileUris).withProtectedSetting("commandToExecute", "bash install_apache.sh").withProtectedSetting("storageAccountName", storageAccount.name()).withProtectedSetting("storageAccountKey", storageAccountKey).attach().create();
// Validate extensions after create
//
Map<String, VirtualMachineScaleSetExtension> extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
VirtualMachineScaleSetExtension extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
// Retrieve scale set
VirtualMachineScaleSet scaleSet = this.computeManager.virtualMachineScaleSets().getById(virtualMachineScaleSet.id());
// Validate extensions after get
//
extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
// Update VMSS capacity
//
int newCapacity = (int) (scaleSet.capacity() + 1);
virtualMachineScaleSet.update().withCapacity(newCapacity).apply();
// Validate extensions after update
//
extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
}
use of com.microsoft.azure.storage.blob.CloudBlobClient in project azure-sdk-for-java by Azure.
the class ManageWebAppStorageAccountConnection method setUpStorageAccount.
private static CloudBlobContainer setUpStorageAccount(String connectionString, String containerName) {
try {
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
// Create a blob service client
CloudBlobClient blobClient = account.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(containerName);
container.createIfNotExists();
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
// Include public access in the permissions object
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
// Set the permissions on the container
container.uploadPermissions(containerPermissions);
return container;
} catch (StorageException | URISyntaxException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
use of com.microsoft.azure.storage.blob.CloudBlobClient in project azure-sdk-for-java by Azure.
the class ManageLinuxWebAppStorageAccountConnection method setUpStorageAccount.
private static CloudBlobContainer setUpStorageAccount(String connectionString, String containerName) {
try {
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
// Create a blob service client
CloudBlobClient blobClient = account.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(containerName);
container.createIfNotExists();
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
// Include public access in the permissions object
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
// Set the permissions on the container
container.uploadPermissions(containerPermissions);
return container;
} catch (StorageException | URISyntaxException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
use of com.microsoft.azure.storage.blob.CloudBlobClient in project hadoop by apache.
the class LocalSASKeyGeneratorImpl method getRelativeBlobSASUri.
/**
* Implementation for generation of Relative Path Blob SAS Uri.
*/
@Override
public URI getRelativeBlobSASUri(String accountName, String container, String relativePath) throws SASKeyGenerationException {
CloudBlobContainer sc = null;
CloudBlobClient client = null;
try {
CloudStorageAccount account = getSASKeyBasedStorageAccountInstance(accountName);
client = account.createCloudBlobClient();
sc = client.getContainerReference(container);
} catch (URISyntaxException uriSyntaxEx) {
throw new SASKeyGenerationException("Encountered URISyntaxException " + "while getting container references for container " + container + " inside storage account : " + accountName, uriSyntaxEx);
} catch (StorageException stoEx) {
throw new SASKeyGenerationException("Encountered StorageException while " + "getting container references for container " + container + " inside storage account : " + accountName, stoEx);
}
CloudBlockBlob blob = null;
try {
blob = sc.getBlockBlobReference(relativePath);
} catch (URISyntaxException uriSyntaxEx) {
throw new SASKeyGenerationException("Encountered URISyntaxException while " + "getting Block Blob references for container " + container + " inside storage account : " + accountName, uriSyntaxEx);
} catch (StorageException stoEx) {
throw new SASKeyGenerationException("Encountered StorageException while " + "getting Block Blob references for container " + container + " inside storage account : " + accountName, stoEx);
}
try {
return client.getCredentials().transformUri(blob.getUri());
} catch (StorageException stoEx) {
throw new SASKeyGenerationException("Encountered StorageException while " + "generating SAS key for Blob: " + relativePath + " inside " + "container : " + container + " in Storage Account : " + accountName, stoEx);
} catch (URISyntaxException uriSyntaxEx) {
throw new SASKeyGenerationException("Encountered URISyntaxException " + "while generating SAS key for Blob: " + relativePath + " inside " + "container: " + container + " in Storage Account : " + accountName, uriSyntaxEx);
}
}
Aggregations