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

The following examples show how to use com.microsoft.azure.storage.blob.BlobContainerPermissions. 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: 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 #6
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;
}
 
Example #7
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 #8
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 #9
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();
}