com.microsoft.azure.storage.blob.BlobContainerPublicAccessType Java Examples

The following examples show how to use com.microsoft.azure.storage.blob.BlobContainerPublicAccessType. 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: AzureConnectionManager.java    From sunbird-lms-service with MIT License 7 votes vote down vote up
/**
 * This method will provide Azure CloudBlobContainer object or in case of error it will provide
 * null;
 *
 * @param containerName String
 * @return CloudBlobContainer or null
 */
public static CloudBlobContainer getContainer(String containerName, boolean isPublicAccess) {

  try {
    CloudBlobClient cloudBlobClient = getBlobClient();
    // Get a reference to a container , The container name must be lower case
    CloudBlobContainer container =
        cloudBlobClient.getContainerReference(containerName.toLowerCase(Locale.ENGLISH));
    // Create the container if it does not exist.
    boolean response = container.createIfNotExists();
    ProjectLogger.log("container creation done if not exist==" + response);
    // Create a permissions object.
    if (isPublicAccess) {
      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 (Exception e) {
    ProjectLogger.log(e.getMessage(), e);
  }
  return null;
}
 
Example #2
Source File: AzureStorageUploader.java    From movie-db-java-on-azure with MIT License 6 votes vote down vote up
private CloudBlobContainer setupContainer(CloudBlobClient blobClient, String containerName) {
    try {
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        if (!container.exists()) {
            container.createIfNotExists();
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
            containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
            container.uploadPermissions(containerPermissions);
        }

        return container;
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Error setting up container: " + e.getMessage());
        return null;
    }
}
 
Example #3
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 #4
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 #5
Source File: AzureStorageContainerCreateRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunAtDriverContainerAllReadyCreated() {

    properties.container.setValue("container-name-ok");
    ValidationResult validationResult = containerCreate.initialize(runtimeContainer, properties);
    assertEquals(ValidationResult.OK.getStatus(), validationResult.getStatus());

    try {
        when(blobService.createContainerIfNotExist(anyString(),
                any(BlobContainerPublicAccessType.class))).thenReturn(false);
        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                return null;
            }
        }).when(blobService);

        containerCreate.blobService = blobService;
        containerCreate.runAtDriver(runtimeContainer);
    } catch (InvalidKeyException | StorageException | URISyntaxException e) {
        fail("should not throw this exception" + e.getMessage());
    }

}
 
Example #6
Source File: AzureStorageContainerCreateRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunAtDriverValid() {

    properties.container.setValue("container-name-ok");
    ValidationResult validationResult = containerCreate.initialize(runtimeContainer, properties);
    assertEquals(ValidationResult.OK.getStatus(), validationResult.getStatus());

    try {
        when(blobService.createContainerIfNotExist(anyString(),
                any(BlobContainerPublicAccessType.class))).thenReturn(true);
        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                return null;
            }
        }).when(blobService);

        containerCreate.blobService = blobService;
        containerCreate.runAtDriver(runtimeContainer);
    } catch (InvalidKeyException | StorageException | URISyntaxException e) {
        fail("should not throw this exception" + e.getMessage());
    }

}
 
Example #7
Source File: AzureStorageContainerCreateRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test(expected = ComponentException.class)
public void testRunAtDriverDieOnError() {

    properties.container.setValue("container-name-ok");
    properties.dieOnError.setValue(true);
    ValidationResult validationResult = containerCreate.initialize(runtimeContainer, properties);
    assertEquals(ValidationResult.OK.getStatus(), validationResult.getStatus());
    containerCreate.blobService = blobService;

    try {
        when(blobService.createContainerIfNotExist(anyString(), any(BlobContainerPublicAccessType.class))).thenThrow(
                new StorageException("errorCode", "storage exception message", new RuntimeException()));
        containerCreate.runAtDriver(runtimeContainer);

    } catch (StorageException | URISyntaxException | InvalidKeyException e) {
        fail("should not throw this exception" + e.getMessage());
    }

}
 
Example #8
Source File: AzureStorageContainerCreateRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * The method {@link AzureStorageContainerCreateRuntime#runAtDriver(RuntimeContainer)} should not throw any
 * exception if the
 * dieOnError is not set to true.
 */
@Test
public void testRunAtDriverHandleURISyntaxException() {

    properties.container.setValue("container-name-ok");
    ValidationResult validationResult = containerCreate.initialize(runtimeContainer, properties);
    assertEquals(ValidationResult.OK.getStatus(), validationResult.getStatus());
    containerCreate.blobService = blobService;

    try {
        when(blobService.createContainerIfNotExist(anyString(), any(BlobContainerPublicAccessType.class))).thenThrow(
                new URISyntaxException("bad url", "some reason"));
        containerCreate.runAtDriver(runtimeContainer);

    } catch (StorageException | URISyntaxException | InvalidKeyException e) {
        fail("should handle this error correctly " + e.getMessage());
    }

}
 
Example #9
Source File: AzureStorageContainerCreateRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * The method {@link AzureStorageContainerCreateRuntime#runAtDriver(RuntimeContainer)} should not throw any
 * exception if the
 * dieOnError is not set to true.
 */
@Test
public void testRunAtDriverHandleStorageException() {

    properties.container.setValue("container-name-ok");
    ValidationResult validationResult = containerCreate.initialize(runtimeContainer, properties);
    assertEquals(ValidationResult.OK.getStatus(), validationResult.getStatus());
    containerCreate.blobService = blobService;

    try {

        when(blobService.createContainerIfNotExist(anyString(), any(BlobContainerPublicAccessType.class))).thenThrow(
                new StorageException("errorCode", "storage exception message", new RuntimeException()));
        containerCreate.runAtDriver(runtimeContainer);

    } catch (StorageException | URISyntaxException | InvalidKeyException e) {
        fail("should handle this error correctly " + e.getMessage());
    }

}
 
Example #10
Source File: AzureStorageContainerCreateRuntime.java    From components with Apache License 2.0 6 votes vote down vote up
private void createAzureStorageBlobContainer() {

        try {

            BlobContainerPublicAccessType accessType = BlobContainerPublicAccessType.OFF;
            if (TAzureStorageContainerCreateProperties.AccessControl.Public.equals(access)) {
                accessType = BlobContainerPublicAccessType.CONTAINER;
            }

            boolean containerCreated = blobService.createContainerIfNotExist(containerName, accessType);
            if (!containerCreated) {
                LOGGER.warn(messages.getMessage("warn.ContainerExists", containerName));
            }
        } catch (StorageException | URISyntaxException | InvalidKeyException e) {
            LOGGER.error(e.getLocalizedMessage());
            if (dieOnError) {
                throw new ComponentException(e);
            }
        }
    }
 
Example #11
Source File: TestBlobService.java    From jframe with Apache License 2.0 5 votes vote down vote up
public void testContainerPermission() {
    // Create a permissions object.
    BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

    // Include public access in the permissions object.
    containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

    // Set the permissions on the container.
    try {
        container.uploadPermissions(containerPermissions);
    } catch (StorageException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
Example #12
Source File: AzureStorageHelper.java    From azure-gradle-plugins with MIT License 5 votes vote down vote up
public static String uploadFileAsBlob(final File fileToUpload, final CloudStorageAccount storageAccount,
                                      final String containerName, final String blobName) throws Exception {
    final CloudBlobContainer blobContainer = getBlobContainer(storageAccount, containerName);
    blobContainer.createIfNotExists(BlobContainerPublicAccessType.BLOB, null, null);

    final CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);
    blob.upload(new FileInputStream(fileToUpload), fileToUpload.length());
    return blob.getUri().toString();
}
 
Example #13
Source File: AzureStorageBlobService.java    From components with Apache License 2.0 5 votes vote down vote up
/**
 * This method create an azure container if it doesn't exist and set it access policy
 *
 * @param containerName : the name of the container to be created
 * @return true if the container was created, false otherwise
 */
public boolean createContainerIfNotExist(final String containerName, final BlobContainerPublicAccessType accessType)
        throws StorageException, URISyntaxException, InvalidKeyException {
    CloudBlobClient cloudBlobClient = connection.getCloudStorageAccount().createCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName);

    boolean containerCreated;
    try {
        containerCreated = cloudBlobContainer
                .createIfNotExists(accessType, null, AzureStorageUtils.getTalendOperationContext());
    } catch (StorageException e) {
        if (!e.getErrorCode().equals(StorageErrorCodeStrings.CONTAINER_BEING_DELETED)) {
            throw e;
        }
        LOGGER.warn(messages.getMessage("error.CONTAINER_BEING_DELETED", containerName));
        // wait 40 seconds (min is 30s) before retrying.
        // See https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/delete-container
        try {
            Thread.sleep(40000);
        } catch (InterruptedException eint) {
            LOGGER.error(messages.getMessage("error.InterruptedException"));
            throw new ComponentException(eint);
        }
        containerCreated = cloudBlobContainer
                .createIfNotExists(accessType, null, AzureStorageUtils.getTalendOperationContext());
        LOGGER.debug(messages.getMessage("debug.ContainerCreated", containerName));
    }

    return containerCreated;
}
 
Example #14
Source File: AzureStorageRepository.java    From hawkbit-extensions with Eclipse Public License 1.0 5 votes vote down vote up
private CloudBlobContainer getContainer() throws URISyntaxException, StorageException {
    final CloudBlobContainer container = blobClient.getContainerReference(properties.getContainerName());
    container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER, new BlobRequestOptions(),
            new OperationContext());

    return container;
}
 
Example #15
Source File: AzureBlobStorageTestAccount.java    From big-c with Apache License 2.0 4 votes vote down vote up
public static void primePublicContainer(CloudBlobClient blobClient,
    String accountName, String containerName, String blobName, int fileSize)
    throws Exception {

  // Create a container if it does not exist. The container name
  // must be lower case.
  CloudBlobContainer container = blobClient
      .getContainerReference(containerName);

  container.createIfNotExists();

  // Create a new shared access policy.
  SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy();

  // Set READ and WRITE permissions.
  //
  sasPolicy.setPermissions(EnumSet.of(
      SharedAccessBlobPermissions.READ,
      SharedAccessBlobPermissions.WRITE,
      SharedAccessBlobPermissions.LIST,
      SharedAccessBlobPermissions.DELETE));

  // Create the container permissions.
  BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

  // Turn public access to the container off.
  containerPermissions
      .setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

  // Set the policy using the values set above.
  containerPermissions.getSharedAccessPolicies().put("testwasbpolicy",
      sasPolicy);
  container.uploadPermissions(containerPermissions);

  // Create a blob output stream.
  CloudBlockBlob blob = container.getBlockBlobReference(blobName);
  BlobOutputStream outputStream = blob.openOutputStream();

  outputStream.write(new byte[fileSize]);
  outputStream.close();
}
 
Example #16
Source File: AzureBlobStorageTestAccount.java    From big-c with Apache License 2.0 4 votes vote down vote up
private static String generateSAS(CloudBlobContainer container,
    boolean readonly) throws Exception {

  // Create a container if it does not exist.
  container.createIfNotExists();

  // Create a new shared access policy.
  SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy();

  // Create a UTC Gregorian calendar value.
  GregorianCalendar calendar = new GregorianCalendar(
      TimeZone.getTimeZone("UTC"));

  // Specify the current time as the start time for the shared access
  // signature.
  //
  calendar.setTime(new Date());
  sasPolicy.setSharedAccessStartTime(calendar.getTime());

  // Use the start time delta one hour as the end time for the shared
  // access signature.
  calendar.add(Calendar.HOUR, 10);
  sasPolicy.setSharedAccessExpiryTime(calendar.getTime());

  if (readonly) {
    // Set READ permissions
    sasPolicy.setPermissions(EnumSet.of(
        SharedAccessBlobPermissions.READ,
        SharedAccessBlobPermissions.LIST));
  } else {
    // Set READ and WRITE permissions.
    //
    sasPolicy.setPermissions(EnumSet.of(
        SharedAccessBlobPermissions.READ,
        SharedAccessBlobPermissions.WRITE,
        SharedAccessBlobPermissions.LIST));
  }

  // Create the container permissions.
  BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

  // Turn public access to the container off.
  containerPermissions.setPublicAccess(BlobContainerPublicAccessType.OFF);

  container.uploadPermissions(containerPermissions);

  // Create a shared access signature for the container.
  String sas = container.generateSharedAccessSignature(sasPolicy, null);
  // HACK: when the just generated SAS is used straight away, we get an
  // authorization error intermittently. Sleeping for 1.5 seconds fixes that
  // on my box.
  Thread.sleep(1500);

  // Return to caller with the shared access signature.
  return sas;
}
 
Example #17
Source File: AzureBlobStorageTestAccount.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public static void primePublicContainer(CloudBlobClient blobClient,
    String accountName, String containerName, String blobName, int fileSize)
    throws Exception {

  // Create a container if it does not exist. The container name
  // must be lower case.
  CloudBlobContainer container = blobClient
      .getContainerReference(containerName);

  container.createIfNotExists();

  // Create a new shared access policy.
  SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy();

  // Set READ and WRITE permissions.
  //
  sasPolicy.setPermissions(EnumSet.of(
      SharedAccessBlobPermissions.READ,
      SharedAccessBlobPermissions.WRITE,
      SharedAccessBlobPermissions.LIST,
      SharedAccessBlobPermissions.DELETE));

  // Create the container permissions.
  BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

  // Turn public access to the container off.
  containerPermissions
      .setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

  // Set the policy using the values set above.
  containerPermissions.getSharedAccessPolicies().put("testwasbpolicy",
      sasPolicy);
  container.uploadPermissions(containerPermissions);

  // Create a blob output stream.
  CloudBlockBlob blob = container.getBlockBlobReference(blobName);
  BlobOutputStream outputStream = blob.openOutputStream();

  outputStream.write(new byte[fileSize]);
  outputStream.close();
}
 
Example #18
Source File: AzureBlobStorageTestAccount.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private static String generateSAS(CloudBlobContainer container,
    boolean readonly) throws Exception {

  // Create a container if it does not exist.
  container.createIfNotExists();

  // Create a new shared access policy.
  SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy();

  // Create a UTC Gregorian calendar value.
  GregorianCalendar calendar = new GregorianCalendar(
      TimeZone.getTimeZone("UTC"));

  // Specify the current time as the start time for the shared access
  // signature.
  //
  calendar.setTime(new Date());
  sasPolicy.setSharedAccessStartTime(calendar.getTime());

  // Use the start time delta one hour as the end time for the shared
  // access signature.
  calendar.add(Calendar.HOUR, 10);
  sasPolicy.setSharedAccessExpiryTime(calendar.getTime());

  if (readonly) {
    // Set READ permissions
    sasPolicy.setPermissions(EnumSet.of(
        SharedAccessBlobPermissions.READ,
        SharedAccessBlobPermissions.LIST));
  } else {
    // Set READ and WRITE permissions.
    //
    sasPolicy.setPermissions(EnumSet.of(
        SharedAccessBlobPermissions.READ,
        SharedAccessBlobPermissions.WRITE,
        SharedAccessBlobPermissions.LIST));
  }

  // Create the container permissions.
  BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

  // Turn public access to the container off.
  containerPermissions.setPublicAccess(BlobContainerPublicAccessType.OFF);

  container.uploadPermissions(containerPermissions);

  // Create a shared access signature for the container.
  String sas = container.generateSharedAccessSignature(sasPolicy, null);
  // HACK: when the just generated SAS is used straight away, we get an
  // authorization error intermittently. Sleeping for 1.5 seconds fixes that
  // on my box.
  Thread.sleep(1500);

  // Return to caller with the shared access signature.
  return sas;
}