Java Examples for com.microsoft.azure.storage.blob.BlobContainerPermissions
The following java examples will help you to understand the usage of com.microsoft.azure.storage.blob.BlobContainerPermissions. These source code samples are taken from different open source projects.
Example 1
| Project: jframe-master File: TestBlobService.java View source code |
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) {
e.printStackTrace();
}
}Example 2
| Project: azure-storage-java-master File: BlobBasics.java View source code |
/**
* Executes the sample.
*
* @param args
* No input args are expected from users.
* @throws URISyntaxException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws InvalidKeyException, URISyntaxException {
Utility.printSampleStartInfo("BlobBasics");
// Setup the cloud storage account.
CloudStorageAccount account = CloudStorageAccount.parse(Utility.storageConnectionString);
// Create a blob service client
CloudBlobClient blobClient = account.createCloudBlobClient();
try {
// Get a reference to a container
// The container name must be lower case
// Append a random UUID to the end of the container name so that
// this sample can be run more than once in quick succession.
CloudBlobContainer container = blobClient.getContainerReference("blobbasicscontainer" + UUID.randomUUID().toString().replace("-", ""));
// Create the container if it does not exist
container.createIfNotExists();
// Make the container public
// 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
container.uploadPermissions(containerPermissions);
// Upload 3 blobs
// Get a reference to a blob in the container
CloudBlockBlob blob1 = container.getBlockBlobReference("blobbasicsblob1");
// Upload text to the blob
blob1.uploadText("Hello, World1");
// Get a reference to a blob in the container
CloudBlockBlob blob2 = container.getBlockBlobReference("blobbasicsblob2");
// Upload text to the blob
blob2.uploadText("Hello, World2");
// Get a reference to a blob in the container
CloudBlockBlob blob3 = container.getBlockBlobReference("blobbasicsblob3");
// Upload text to the blob
blob3.uploadText("Hello, World3");
// For each item in the container
for (ListBlobItem blobItem : container.listBlobs()) {
// If the item is a blob, not a virtual directory
if (blobItem instanceof CloudBlockBlob) {
// Download the text
CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem;
System.out.println(retrievedBlob.downloadText());
}
}
// output the URI of each of them
for (ListBlobItem blobItem : container.listBlobs()) {
System.out.println(blobItem.getUri());
}
// Delete the blobs
blob1.deleteIfExists();
blob2.deleteIfExists();
blob3.deleteIfExists();
// Delete the container
container.deleteIfExists();
} catch (Throwable t) {
Utility.printException(t);
}
Utility.printSampleCompleteInfo("BlobBasics");
}Example 3
| Project: azure-sdk-for-java-master File: ManageWebAppStorageAccountConnection.java View source code |
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 (StorageExceptionURISyntaxException | InvalidKeyException | e) {
throw new RuntimeException(e);
}
}Example 4
| Project: cloudbreak-master File: AzureClient.java View source code |
public void setPublicPermissionOnContainer(String resourceGroup, String storageName, String containerName) throws Exception {
CloudBlobContainer container = getBlobContainer(resourceGroup, storageName, containerName);
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
container.uploadPermissions(containerPermissions);
}Example 5
| Project: azure-slave-plugin-master File: StorageServiceDelegate.java View source code |
/**
* Generates SAS URL for blob in Azure storage account
* @param storageAccountName
* @param storageAccountKey
* @param containerName
* @param strBlobURL
* @return SAS URL
* @throws Exception
*/
public static String generateSASURL(String storageAccountName, String storageAccountKey, String containerName, String strBlobURL) throws Exception {
LOGGER.info("StorageServiceDelegate: generateSASURL: Generating SAS URL for blob " + strBlobURL);
StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(storageAccountName, storageAccountKey);
URL blobURL = new URL(strBlobURL);
String saBlobURI = new StringBuilder().append(blobURL.getProtocol()).append("://").append(blobURL.getHost()).append("/").toString();
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(credentials, new URI(saBlobURI), new URI(getCustomURI(storageAccountName, Constants.QUEUE, saBlobURI)), new URI(getCustomURI(storageAccountName, Constants.TABLE, saBlobURI)));
// Create the blob client.
CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(containerName);
// At this point need to throw an error back since container itself did not exist.
if (!container.exists()) {
throw new AzureCloudException("StorageServiceDelegate: generateSASURL: Container " + containerName + " does not exist in storage account " + storageAccountName);
}
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(new Date());
policy.setSharedAccessStartTime(calendar.getTime());
calendar.add(Calendar.HOUR, 1);
policy.setSharedAccessExpiryTime(calendar.getTime());
policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE));
// TODO: Test if old sas is valid after permissions are updated
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.getSharedAccessPolicies().put("jenkinssharedAccess", policy);
container.uploadPermissions(containerPermissions);
// Create a shared access signature for the container.
String sas = container.generateSharedAccessSignature(policy, null);
strBlobURL = strBlobURL.replace("http://", "https://");
LOGGER.info("StorageServiceDelegate: generateSASURL: Successfully generated SAS url " + strBlobURL + "?" + sas);
return strBlobURL + "?" + sas;
}Example 6
| Project: azure-chat-for-java-master File: AzureChatStorageUtils.java View source code |
/**
* This method will assign public access to the blob container.
*
* @param container
* @throws Exception
*/
public static void assignPublicAccessToContainer(CloudBlobContainer container) throws Exception {
LOGGER.info("[AzureChatStorageUtils][assignPublicAccessToContainer] start");
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
container.uploadPermissions(containerPermissions);
LOGGER.debug("PublicAccess Permissions uploaded Successfully.");
LOGGER.info("[AzureChatStorageUtils][assignPublicAccessToContainer] end");
}Example 7
| Project: hadoop-release-2.6.0-master File: AzureBlobStorageTestAccount.java View source code |
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 8
| Project: hadoop-master File: AzureBlobStorageTestAccount.java View source code |
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;
}