Java Code Examples for com.microsoft.azure.storage.CloudStorageAccount#parse()

The following examples show how to use com.microsoft.azure.storage.CloudStorageAccount#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AzureStorageDriver.java    From dcos-cassandra-service with Apache License 2.0 7 votes vote down vote up
private CloudBlobContainer getCloudBlobContainer(String accountName, String accountKey, String containerName) {
  CloudBlobContainer container = null;

  if (StringUtils.isNotBlank(containerName)) {
    final String storageConnectionString = "DefaultEndpointsProtocol=https"
      + ";AccountName=" + accountName
      + ";AccountKey=" + accountKey;

    try {
      final CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
      CloudBlobClient serviceClient = account.createCloudBlobClient();

      container = serviceClient.getContainerReference(containerName);
      container.createIfNotExists();
    } catch (StorageException | URISyntaxException | InvalidKeyException e) {
      logger.error("Error connecting to container for account {} and container name {}", accountName, containerName, e);
    }
  }

  return container;
}
 
Example 2
Source File: AzureNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void init(ZeppelinConfiguration conf) throws IOException {
  this.conf = conf;
  user = conf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_AZURE_USER);
  shareName = conf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_AZURE_SHARE);

  try {
    CloudStorageAccount account = CloudStorageAccount.parse(
        conf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_AZURE_CONNECTION_STRING));
    CloudFileClient client = account.createCloudFileClient();
    CloudFileShare share = client.getShareReference(shareName);
    share.createIfNotExists();

    CloudFileDirectory userDir = StringUtils.isBlank(user) ?
        share.getRootDirectoryReference() :
        share.getRootDirectoryReference().getDirectoryReference(user);
    userDir.createIfNotExists();

    rootDir = userDir.getDirectoryReference("notebook");
    rootDir.createIfNotExists();
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example 3
Source File: AzureSetup.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void validateWasbFileSystem(SpiFileSystem fileSystem) throws URISyntaxException, InvalidKeyException, StorageException {
    CloudWasbView cloudFileSystem = (CloudWasbView) fileSystem.getCloudFileSystems().get(0);
    String accountName = cloudFileSystem.getAccountName();
    String accountKey = cloudFileSystem.getAccountKey();
    String connectionString = "DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accountKey;
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(connectionString);
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
    CloudBlobContainer containerReference = blobClient.getContainerReference(TEST_CONTAINER + System.nanoTime());
    try {
        containerReference.createIfNotExists();
        containerReference.delete();
    } catch (StorageException e) {
        if (e.getCause() instanceof UnknownHostException) {
            throw new CloudConnectorException("The provided account does not belong to a valid storage account");
        }
    }
}
 
Example 4
Source File: AzureSetup.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
private void validateAdlsGen2FileSystem(SpiFileSystem fileSystem) throws URISyntaxException, InvalidKeyException, StorageException {
    CloudAdlsGen2View cloudFileSystem = (CloudAdlsGen2View) fileSystem.getCloudFileSystems().get(0);
    String accountName = cloudFileSystem.getAccountName();
    String accountKey = cloudFileSystem.getAccountKey();
    String connectionString = "DefaultEndpointsProtocol=https;AccountName="
            + accountName + ";AccountKey=" + accountKey + ";EndpointSuffix=core.windows.net";
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(connectionString);
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
    CloudBlobContainer containerReference = blobClient.getContainerReference(TEST_CONTAINER + System.nanoTime());
    try {
        containerReference.createIfNotExists();
        containerReference.delete();
    } catch (StorageException e) {
        if (e.getCause() instanceof UnknownHostException) {
            throw new CloudConnectorException("The provided account does not belong to a valid storage account");
        }
    }
}
 
Example 5
Source File: TestBlobService.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Before
public void testCreateContainer() {
    try {
        // Retrieve storage account from connection-string.
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        // Get a reference to a container.
        // The container name must be lower case
        container = blobClient.getContainerReference(containerName);

        // Create the container if it does not exist.
        container.createIfNotExists();
    } catch (Exception e) {
        // Output the stack trace.
        e.printStackTrace();
    }
}
 
Example 6
Source File: OldAzureNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void init(ZeppelinConfiguration conf) throws IOException {
  this.conf = conf;
  user = conf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_AZURE_USER);
  shareName = conf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_AZURE_SHARE);

  try {
    CloudStorageAccount account = CloudStorageAccount.parse(
        conf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_NOTEBOOK_AZURE_CONNECTION_STRING));
    CloudFileClient client = account.createCloudFileClient();
    CloudFileShare share = client.getShareReference(shareName);
    share.createIfNotExists();

    CloudFileDirectory userDir = StringUtils.isBlank(user) ?
        share.getRootDirectoryReference() :
        share.getRootDirectoryReference().getDirectoryReference(user);
    userDir.createIfNotExists();

    rootDir = userDir.getDirectoryReference("notebook");
    rootDir.createIfNotExists();
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example 7
Source File: AzsValidator.java    From halyard with Apache License 2.0 6 votes vote down vote up
@Override
public void validate(ConfigProblemSetBuilder ps, AzsPersistentStore n) {
  String connectionString =
      "DefaultEndpointsProtocol=https;AccountName="
          + n.getStorageAccountName()
          + ";AccountKey="
          + secretSessionManager.decrypt(n.getStorageAccountKey());

  try {
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(connectionString);

    CloudBlobContainer container =
        storageAccount.createCloudBlobClient().getContainerReference(n.getStorageContainerName());
    container.exists();
  } catch (Exception e) {
    ps.addProblem(
        Problem.Severity.ERROR,
        "Failed to connect to the Azure storage account \""
            + n.getStorageAccountName()
            + "\": "
            + e.getMessage());
    return;
  }
}
 
Example 8
Source File: ManageWebAppStorageAccountConnection.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
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);
    }

}
 
Example 9
Source File: ManageLinuxWebAppStorageAccountConnection.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
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);
    }

}
 
Example 10
Source File: AzureTableStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
public void init() {
  try {
    String endpoint = String.format(ENDPOINT_TEMPLATE, configuration.getAccountName());

    CloudStorageAccount cosmosAccount =
        CloudStorageAccount.parse(
            String.format(
                COSMOS_CONNECTION_TEMPLATE,
                configuration.getAccountName(),
                configuration.getAccountKey(),
                endpoint));
    tableClient = cosmosAccount.createCloudTableClient();

    // Create the tables if the do not exist
    tableClient.getTableReference(JOB_TABLE).createIfNotExists();
    tableClient.getTableReference(JOB_DATA_TABLE).createIfNotExists();

    CloudStorageAccount blobAccount =
        CloudStorageAccount.parse(
            String.format(
                BLOB_CONNECTION_TEMPLATE,
                configuration.getAccountName(),
                configuration.getBlobKey()));
    blobClient = blobAccount.createCloudBlobClient();

    blobClient.getContainerReference(BLOB_CONTAINER).createIfNotExists();
  } catch (StorageException | URISyntaxException | InvalidKeyException e) {
    throw new MicrosoftStorageException(e);
  }
}
 
Example 11
Source File: AzureConnectionWithKeyService.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public CloudStorageAccount getCloudStorageAccount() throws InvalidKeyException, URISyntaxException {

    StringBuilder connectionString = new StringBuilder();
    connectionString.append("DefaultEndpointsProtocol=").append(protocol) //
            .append(";AccountName=").append(accountName) //
            .append(";AccountKey=").append(accountKey);

    return CloudStorageAccount.parse(connectionString.toString());
}
 
Example 12
Source File: AzureUploadManager.java    From secor with Apache License 2.0 5 votes vote down vote up
public AzureUploadManager(SecorConfig config) throws Exception {
    super(config);

    final String storageConnectionString =
            "DefaultEndpointsProtocol=" + mConfig.getAzureEndpointsProtocol() + ";" +
            "AccountName=" + mConfig.getAzureAccountName() + ";" +
            "AccountKey=" + mConfig.getAzureAccountKey() + ";";

    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
    blobClient = storageAccount.createCloudBlobClient();
}
 
Example 13
Source File: AzureStorageService.java    From front50 with Apache License 2.0 5 votes vote down vote up
public AzureStorageService(String connectionString, String containerName) {
  this.containerName = containerName;
  try {
    this.storageAccount = CloudStorageAccount.parse(connectionString);
  } catch (Exception e) {
    // Log the exception
    this.storageAccount = null;
  }
}
 
Example 14
Source File: MSDeployArtifactHandlerImpl.java    From azure-gradle-plugins with MIT License 5 votes vote down vote up
protected CloudStorageAccount getCloudStorageAccount(final FunctionApp app) throws Exception {
    final AppSetting internalStorageSetting = app.getAppSettings().get(INTERNAL_STORAGE_KEY);
    if (internalStorageSetting == null || StringUtils.isEmpty(internalStorageSetting.value())) {
        logError(INTERNAL_STORAGE_NOT_FOUND);
        throw new Exception(INTERNAL_STORAGE_NOT_FOUND);
    }
    logDebug(INTERNAL_STORAGE_CONNECTION_STRING + internalStorageSetting.value());
    return CloudStorageAccount.parse(internalStorageSetting.value());
}
 
Example 15
Source File: AzureStorageRepositoryAutoConfiguration.java    From hawkbit-extensions with Eclipse Public License 1.0 4 votes vote down vote up
@Bean
CloudStorageAccount cloudStorageAccount(final AzureStorageRepositoryProperties properties)
        throws InvalidKeyException, URISyntaxException {
    return CloudStorageAccount.parse(properties.getConnectionString());
}
 
Example 16
Source File: QueryMetricsAndActivityLogs.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
private static void addBlobTransactions(String storageConnectionString) throws IOException, URISyntaxException, InvalidKeyException, StorageException {
    // Get the script to upload
    //
    InputStream scriptFileAsStream = QueryMetricsAndActivityLogs
            .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
    //
    CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
    CloudBlobClient cloudBlobClient = account.createCloudBlobClient();
    CloudBlobContainer container = cloudBlobClient.getContainerReference("scripts");
    container.createIfNotExists();

    ServiceProperties serviceProps = cloudBlobClient.downloadServiceProperties();

    // configure Storage logging and metrics
    LoggingProperties logProps = new LoggingProperties();
    logProps.setLogOperationTypes(EnumSet.of(LoggingOperations.READ, LoggingOperations.WRITE));
    logProps.setRetentionIntervalInDays(2);
    logProps.setVersion("1.0");
    serviceProps.setLogging(logProps);

    MetricsProperties metricProps = new MetricsProperties();
    metricProps.setMetricsLevel(MetricsLevel.SERVICE_AND_API);
    metricProps.setRetentionIntervalInDays(2);
    metricProps.setVersion("1.0");
    serviceProps.setHourMetrics(metricProps);
    serviceProps.setMinuteMetrics(metricProps);

    // Set the default service version to be used for anonymous requests.
    serviceProps.setDefaultServiceVersion("2015-04-05");

    // Set the service properties.
    cloudBlobClient.uploadServiceProperties(serviceProps);

    CloudBlockBlob blob = container.getBlockBlobReference("install_apache.sh");
    blob.upload(scriptFileAsStream, fileSize);

    // give sometime for the infrastructure to process the records and fit into time grain.
    SdkContext.sleep(6 * 60000);
}
 
Example 17
Source File: AzureClientImpl.java    From exhibitor with Apache License 2.0 4 votes vote down vote up
@Override
public CloudBlobClient getClient() throws Exception {
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(this.storageConnectionString);
    return storageAccount.createCloudBlobClient();
}
 
Example 18
Source File: TableGettingStartedTask.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Override
protected Void doInBackground(String... arg0) {

    act.printSampleStartInfo("TableBasics");

    try {
        // Setup the cloud storage account.
        CloudStorageAccount account = CloudStorageAccount
                .parse(MainActivity.storageConnectionString);

        // Create a table service client.
        tableClient = account.createCloudTableClient();

        // Retrieve a reference to a table.
        // Append a random UUID to the end of the table name so that this
        // sample can be run more than once in quick succession.
        table = tableClient.getTableReference(tableName
                + UUID.randomUUID().toString().replace("-", ""));

        // Create the table if it doesn't already exist.
        table.createIfNotExists();

        // Illustrates how to list the tables.
        this.BasicListing();

        // Illustrates how to form and execute a single insert operation.
        this.BasicInsertEntity();

        // Illustrates how to form and execute a batch operation.
        this.BasicBatch();

        // Illustrates how to form and execute a query operation.
        this.BasicQuery();

        // Illustrates how to form and execute an upsert operation.
        this.BasicUpsert();

        // Illustrates how to form and execute an entity delete operation.
        this.BasicDeleteEntity();

        // Delete the table.
        table.deleteIfExists();

    } catch (Throwable t) {
        act.printException(t);
    }

    act.printSampleCompleteInfo("TableBasics");

    return null;
}
 
Example 19
Source File: QueueGettingStartedTask.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Override
protected Void doInBackground(String... arg0) {

    act.printSampleStartInfo("QueueBasics");

    try {
        // Setup the cloud storage account.
        CloudStorageAccount account = CloudStorageAccount
                .parse(MainActivity.storageConnectionString);

        // Create a queue service client
        CloudQueueClient queueClient = account.createCloudQueueClient();

        // Retrieve a reference to a queue
        // Append a random UUID to the end of the queue name so that this
        // sample can be run more than once in quick succession.
        CloudQueue queue = queueClient.getQueueReference("queuebasics"
                + UUID.randomUUID().toString().replace("-", ""));

        // Create the queue if it doesn't already exist
        queue.createIfNotExists();

        // Create messages and add it to the queue
        CloudQueueMessage message1 = new CloudQueueMessage("Hello, World1");
        queue.addMessage(message1);

        CloudQueueMessage message2 = new CloudQueueMessage("Hello, World2");
        queue.addMessage(message2);

        CloudQueueMessage message3 = new CloudQueueMessage("Hello, World3");
        queue.addMessage(message3);

        CloudQueueMessage message4 = new CloudQueueMessage("Hello, World4");
        queue.addMessage(message4);

        CloudQueueMessage message5 = new CloudQueueMessage("Hello, World5");
        queue.addMessage(message5);

        // Peek at the next message
        CloudQueueMessage peekedMessage = queue.peekMessage();
        act.outputText(
                view,
                String.format("Peeked Message : %s",
                        peekedMessage.getMessageContentAsString()));

        // Modify the message content and set it to be visible immediately
        // Retrieve the first visible message in the queue
        CloudQueueMessage updateMessage = queue.retrieveMessage();
        updateMessage.setMessageContent("Updated contents.");
        EnumSet<MessageUpdateFields> updateFields = EnumSet
                .of(MessageUpdateFields.CONTENT,
                        MessageUpdateFields.VISIBILITY);
        queue.updateMessage(updateMessage, 0, updateFields, null, null);

        // Retrieve 3 messages from the queue with a visibility timeout of 1
        // second
        queue.retrieveMessages(3, 1, null, null);

        // Sleep for 2 seconds so the messages become visible and can be
        // processed/deleted
        Thread.sleep(2000);

        // Retrieve the messages in the queue with a visibility timeout of
        // 30 seconds and delete them
        CloudQueueMessage retrievedMessage;
        while ((retrievedMessage = queue.retrieveMessage(30,
                null /* options */, null /* opContext */)) != null) {
            // Process the message in less than 30 seconds, and then delete
            // the message.
            act.outputText(view,
                    retrievedMessage.getMessageContentAsString());
            queue.deleteMessage(retrievedMessage);
        }

        // Delete a queue
        queue.deleteIfExists();

    } catch (Throwable t) {
        act.printException(t);
    }

    act.printSampleCompleteInfo("QueueBasics");

    return null;
}
 
Example 20
Source File: TestBatchAI.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public BatchAIWorkspace createResource(BatchAIWorkspaces workspaces) throws Exception {
    final String groupName = SdkContext.randomResourceName("rg", 10);
    final String workspaceName = SdkContext.randomResourceName("ws", 10);
    final String vnetName = SdkContext.randomResourceName("vnet", 15);
    final String saName = SdkContext.randomResourceName("cluster", 15);
    final String shareName = "myfileshare";
    final String shareMountPath = "azurefileshare";
    final String blobFileSystemPath = "myblobsystem";
    final String containerName = "mycontainer";
    final String userName = "tirekicker";
    final String subnetName = "MySubnet";
    String storageAccountKey;
    String fileShareUri;

    BatchAIWorkspace workspace = workspaces.define(workspaceName)
            .withRegion(region)
            .withNewResourceGroup(groupName)
            .create();

    if (isPlaybackMode()) {
        storageAccountKey = "dummy_key";
        fileShareUri = "dummy_uri";
    } else {
        storageAccountKey = ensureStorageAccount(storageAccounts, saName, groupName, shareName);
        String connectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s", saName, storageAccountKey);

        CloudFileShare cloudFileShare = CloudStorageAccount.parse(String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;EndpointSuffix=core.windows.net",
                saName, storageAccountKey))
                .createCloudFileClient()
                .getShareReference(shareName);
        cloudFileShare.create();

        CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
        CloudBlobClient cloudBlobClient = account.createCloudBlobClient();
        CloudBlobContainer container = cloudBlobClient.getContainerReference(containerName);
        container.createIfNotExists();
        fileShareUri = cloudFileShare.getStorageUri().getPrimaryUri().toString();
    }

    Network network = networks.define(vnetName)
        .withRegion(region)
        .withExistingResourceGroup(groupName)
        .withAddressSpace("192.168.0.0/16")
        .withSubnet(subnetName, "192.168.200.0/24")
        .create();

    BatchAICluster cluster = workspace.clusters().define(clusterName)
            .withVMSize(VirtualMachineSizeTypes.STANDARD_D1_V2.toString())
            .withUserName(userName)
            .withPassword("MyPassword")
            .withAutoScale(1, 1)
            .withLowPriority()
            .defineSetupTask()
                .withCommandLine("echo Hello World!")
                .withStdOutErrPath("./outputpath")
            .attach()
            .defineAzureFileShare()
                .withStorageAccountName(saName)
                .withAzureFileUrl(fileShareUri)
                .withRelativeMountPath(shareMountPath)
                .withAccountKey(storageAccountKey)
                .attach()
            .defineAzureBlobFileSystem()
                .withStorageAccountName(saName)
                .withContainerName(containerName)
                .withRelativeMountPath(blobFileSystemPath)
                .withAccountKey(storageAccountKey)
                .attach()
            .withVirtualMachineImage("microsoft-ads", "linux-data-science-vm-ubuntu", "linuxdsvmubuntu")
            .withSubnet(network.id(), subnetName)
            .withAppInsightsComponentId("appinsightsId")
            .withInstrumentationKey("appInsightsKey")
            .create();
    printBatchAICluster(cluster);
    Assert.assertEquals("resizing", cluster.allocationState().toString());
    Assert.assertEquals(userName, cluster.adminUserName());
    Assert.assertEquals(VmPriority.LOWPRIORITY, cluster.vmPriority());
    Assert.assertEquals(1, cluster.nodeSetup().mountVolumes().azureFileShares().size());
    Assert.assertEquals(shareMountPath, cluster.nodeSetup().mountVolumes().azureFileShares().get(0).relativeMountPath());
    Assert.assertEquals(1, cluster.nodeSetup().mountVolumes().azureBlobFileSystems().size());
    Assert.assertEquals(blobFileSystemPath, cluster.nodeSetup().mountVolumes().azureBlobFileSystems().get(0).relativeMountPath());
    Assert.assertEquals(network.id() + "/subnets/" + subnetName, cluster.subnet().id());
    Assert.assertEquals("appinsightsId", cluster.nodeSetup().performanceCountersSettings().appInsightsReference().component().id());
    Assert.assertEquals("linux-data-science-vm-ubuntu", cluster.virtualMachineConfiguration().imageReference().offer());
    return workspace;
}