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

The following examples show how to use com.microsoft.azure.storage.CloudStorageAccount#createCloudBlobClient() . 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: AzureStorageUploader.java    From movie-db-java-on-azure with MIT License 8 votes vote down vote up
/**
 * Upload image file to Azure storage with specified name.
 *
 * @param file     image file object
 * @param fileName specified file name
 * @return relative path of the created image blob
 */
public String uploadToAzureStorage(ApplicationContext applicationContext, MultipartFile file, String fileName) {
    String uri = null;

    try {
        CloudStorageAccount storageAccount =
                (CloudStorageAccount) applicationContext.getBean("cloudStorageAccount");
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        setupContainer(blobClient, this.thumbnailImageContainer);
        CloudBlobContainer originalImageContainer = setupContainer(blobClient, this.originalImageContainer);

        if (originalImageContainer != null) {
            CloudBlockBlob blob = originalImageContainer.getBlockBlobReference(fileName);
            blob.upload(file.getInputStream(), file.getSize());

            uri = blob.getUri().getPath();
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Error uploading image: " + e.getMessage());
    }

    return uri;
}
 
Example 2
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 3
Source File: PreviewStorageAzure.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
public PreviewStorageAzure(List<StorageCredentials> accounts, String container, ExecutorService executor) {
	this.containers = new ArrayList<>();
	this.accounts = new HashMap<>();
	this.executor = executor;

	for (StorageCredentials account : accounts) {
		try {
			CloudStorageAccount storage = new CloudStorageAccount(account, true);
			this.accounts.put(account.getAccountName(), storage.createCloudBlobClient());

			CloudBlobClient client = storage.createCloudBlobClient();
			this.containers.add(getStorageContainer(client, container));
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	log.info("configured azure storage with {} accounts", accounts.size());
}
 
Example 4
Source File: VideoStorageAzure.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
public VideoStorageAzure(List<StorageCredentials> accounts, String container, long accessDurationInMs, boolean instrumentOs, int bufferOsSize, int fetchIsSize) {
   this.containers = new ArrayList<>();
   this.accounts = new HashMap<>();
   this.accessDurationInMs = accessDurationInMs;
   this.instrumentOs = instrumentOs;
   this.bufferOsSize = bufferOsSize;
   this.fetchIsSize = fetchIsSize;

   for (StorageCredentials account : accounts) {
      try {
         CloudStorageAccount storage = new CloudStorageAccount(account,true);
         this.accounts.put(account.getAccountName(), storage.createCloudBlobClient());

         CloudBlobClient client = storage.createCloudBlobClient();
         this.containers.add(getStorageContainer(client,container));
      } catch (Exception ex) {
         throw new RuntimeException(ex);
      }
   }

   log.info("configured azure storage with {} accounts", accounts.size());
}
 
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: 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 7
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 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: AzureStorageSourceOrSink.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public List<NamedThing> getSchemaNames(RuntimeContainer container) throws IOException {
    List<NamedThing> result = new ArrayList<>();
    try {
        CloudStorageAccount storageAccount = getAzureConnection(container).getCloudStorageAccount();
        CloudBlobClient client = storageAccount.createCloudBlobClient();
        for (CloudBlobContainer c : client.listContainers()) {
            result.add(new SimpleNamedThing(c.getName(), c.getName()));
        }
    } catch (InvalidKeyException | URISyntaxException e) {
        throw new ComponentException(e);
    }
    return result;
}
 
Example 10
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 11
Source File: AzureStorageUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Create CloudBlobClient instance.
 * @param flowFile An incoming FlowFile can be used for NiFi Expression Language evaluation to derive
 *                 Account Name, Account Key or SAS Token. This can be null if not available.
 */
public static CloudBlobClient createCloudBlobClient(ProcessContext context, ComponentLog logger, FlowFile flowFile) throws URISyntaxException {
    final AzureStorageCredentialsDetails storageCredentialsDetails = getStorageCredentialsDetails(context, flowFile);
    final CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(
        storageCredentialsDetails.getStorageCredentials(),
        true,
        storageCredentialsDetails.getStorageSuffix(),
        storageCredentialsDetails.getStorageAccountName());
    final CloudBlobClient cloudBlobClient = cloudStorageAccount.createCloudBlobClient();

    return cloudBlobClient;
}
 
Example 12
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 13
Source File: StorageInterfaceImpl.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public void createBlobClient(CloudStorageAccount account) {
  serviceClient = account.createCloudBlobClient();
}
 
Example 14
Source File: AzureBlobStorageTestAccount.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public static AzureBlobStorageTestAccount createAnonymous(
    final String blobName, final int fileSize) throws Exception {

  NativeAzureFileSystem fs = null;
  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration(), noTestAccountConf = new Configuration();

  // Set up a session with the cloud blob client to generate SAS and check the
  // existence of a container and capture the container object.
  CloudStorageAccount account = createTestAccount(conf);
  if (account == null) {
    return null;
  }
  CloudBlobClient blobClient = account.createCloudBlobClient();

  // Capture the account URL and the account name.
  String accountName = conf.get(TEST_ACCOUNT_NAME_PROPERTY_NAME);

  // Generate a container name and create a shared access signature string for
  // it.
  //
  String containerName = generateContainerName();

  // Set up public container with the specified blob name.
  primePublicContainer(blobClient, accountName, containerName, blobName,
      fileSize);

  // Capture the blob container object. It should exist after generating the
  // shared access signature.
  container = blobClient.getContainerReference(containerName);
  if (null == container || !container.exists()) {
    final String errMsg = String
        .format("Container '%s' expected but not found while creating SAS account.");
    throw new Exception(errMsg);
  }

  // Set the account URI.
  URI accountUri = createAccountUri(accountName, containerName);

  // Initialize the Native Azure file system with anonymous credentials.
  fs = new NativeAzureFileSystem();
  fs.initialize(accountUri, noTestAccountConf);

  // Create test account initializing the appropriate member variables.
  AzureBlobStorageTestAccount testAcct = new AzureBlobStorageTestAccount(fs,
      account, container);

  // Return to caller with test account.
  return testAcct;
}
 
Example 15
Source File: AzureBlobStorageTestAccount.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public static AzureBlobStorageTestAccount createRoot(final String blobName,
    final int fileSize) throws Exception {

  NativeAzureFileSystem fs = null;
  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration();

  // Set up a session with the cloud blob client to generate SAS and check the
  // existence of a container and capture the container object.
  CloudStorageAccount account = createTestAccount(conf);
  if (account == null) {
    return null;
  }
  CloudBlobClient blobClient = account.createCloudBlobClient();

  // Capture the account URL and the account name.
  String accountName = conf.get(TEST_ACCOUNT_NAME_PROPERTY_NAME);

  // Set up public container with the specified blob name.
  CloudBlockBlob blobRoot = primeRootContainer(blobClient, accountName,
      blobName, fileSize);

  // Capture the blob container object. It should exist after generating the
  // shared access signature.
  container = blobClient.getContainerReference(AZURE_ROOT_CONTAINER);
  if (null == container || !container.exists()) {
    final String errMsg = String
        .format("Container '%s' expected but not found while creating SAS account.");
    throw new Exception(errMsg);
  }

  // Set the account URI without a container name.
  URI accountUri = createAccountUri(accountName);

  // Initialize the Native Azure file system with anonymous credentials.
  fs = new NativeAzureFileSystem();
  fs.initialize(accountUri, conf);

  // Create test account initializing the appropriate member variables.
  // Set the container value to null for the default root container.
  //
  AzureBlobStorageTestAccount testAcct = new AzureBlobStorageTestAccount(
      fs, account, blobRoot);

  // Return to caller with test account.
  return testAcct;
}
 
Example 16
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;
}
 
Example 17
Source File: AzureBlobStorageTestAccount.java    From big-c with Apache License 2.0 4 votes vote down vote up
public static AzureBlobStorageTestAccount createRoot(final String blobName,
    final int fileSize) throws Exception {

  NativeAzureFileSystem fs = null;
  CloudBlobContainer container = null;
  Configuration conf = createTestConfiguration();

  // Set up a session with the cloud blob client to generate SAS and check the
  // existence of a container and capture the container object.
  CloudStorageAccount account = createTestAccount(conf);
  if (account == null) {
    return null;
  }
  CloudBlobClient blobClient = account.createCloudBlobClient();

  // Capture the account URL and the account name.
  String accountName = conf.get(TEST_ACCOUNT_NAME_PROPERTY_NAME);

  // Set up public container with the specified blob name.
  CloudBlockBlob blobRoot = primeRootContainer(blobClient, accountName,
      blobName, fileSize);

  // Capture the blob container object. It should exist after generating the
  // shared access signature.
  container = blobClient.getContainerReference(AZURE_ROOT_CONTAINER);
  if (null == container || !container.exists()) {
    final String errMsg = String
        .format("Container '%s' expected but not found while creating SAS account.");
    throw new Exception(errMsg);
  }

  // Set the account URI without a container name.
  URI accountUri = createAccountUri(accountName);

  // Initialize the Native Azure file system with anonymous credentials.
  fs = new NativeAzureFileSystem();
  fs.initialize(accountUri, conf);

  // Create test account initializing the appropriate member variables.
  // Set the container value to null for the default root container.
  //
  AzureBlobStorageTestAccount testAcct = new AzureBlobStorageTestAccount(
      fs, account, blobRoot);

  // Return to caller with test account.
  return testAcct;
}
 
Example 18
Source File: AzureStorageRepository.java    From hawkbit-extensions with Eclipse Public License 1.0 4 votes vote down vote up
public AzureStorageRepository(final CloudStorageAccount storageAccount,
        final AzureStorageRepositoryProperties properties) {
    this.blobClient = storageAccount.createCloudBlobClient();
    this.properties = properties;
}
 
Example 19
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 20
Source File: AzureResourceFactory.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
/**
 * @throws InvalidKeyException 
 * @throws URISyntaxException 
 * 
 */
public AzureResourceFactory(String credentialsString, Boolean isHttps) throws InvalidKeyException, URISyntaxException {
	StorageCredentials storageCredentials = StorageCredentialsAccountAndKey.tryParseCredentials(credentialsString);
   CloudStorageAccount csa = new CloudStorageAccount(storageCredentials, isHttps);
   this.blobClient = csa.createCloudBlobClient();
}