@Override public CloudStorageAccount getCloudStorageAccount() throws InvalidKeyException, URISyntaxException { StorageCredentials credentials = new StorageCredentialsSharedAccessSignature(sasToken); return new CloudStorageAccount(credentials, true, null, accountName); }
@Override public CloudStorageAccount getCloudStorageAccount() throws InvalidKeyException, URISyntaxException { StorageCredentials credentials = new StorageCredentialsSharedAccessSignature(sasToken); return new CloudStorageAccount(credentials, true, null, accountName); }
static void AddCorsRuleStorageClientLibrary() { //Add a new rule. var corsRule = new CorsRule() { AllowedHeaders = new List<string> { "*" }, AllowedMethods = CorsHttpMethods.Get AllowedOrigins = new List<string> { "http://somedomain.com" },//This is the URL of your application. MaxAgeInSeconds = 1 * 60 * 60,//Let the browser cache it for an hour }; //First get the service properties from storage to ensure we're not adding the same CORS rule again. var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true); var client = storageAccount.CreateCloudBlobClient(); var serviceProperties = client.GetServiceProperties(); var corsSettings = serviceProperties.Cors; corsSettings.CorsRules.Add(corsRule); //Save the rule client.SetServiceProperties(serviceProperties); }
/** * Helper method that creates CloudStorageAccount Instance using the * storage account key. * @param accountName Name of the storage account * @param accountKey Storage Account key * @return CloudStorageAccount instance for the storage account. * @throws SASKeyGenerationException */ private CloudStorageAccount getStorageAccountInstance(String accountName, String accountKey) throws SASKeyGenerationException { if (!storageAccountMap.containsKey(accountName)) { CloudStorageAccount account = null; try { account = new CloudStorageAccount(new StorageCredentialsAccountAndKey( accountName, accountKey)); } catch (URISyntaxException uriSyntaxEx) { throw new SASKeyGenerationException("Encountered URISyntaxException " + "for account " + accountName, uriSyntaxEx); } storageAccountMap.put(accountName, account); } return storageAccountMap.get(accountName); }
static CloudStorageAccount createStorageAccount(String accountName, Configuration conf, boolean allowAnonymous) throws URISyntaxException, KeyProviderException { String accountKey = AzureNativeFileSystemStore .getAccountKeyFromConfiguration(accountName, conf); final StorageCredentials credentials; if (accountKey == null) { if (allowAnonymous) { credentials = StorageCredentialsAnonymous.ANONYMOUS; } else { LOG.warn("Skipping live Azure test because of missing key for" + " account '" + accountName + "'."); return null; } } else { credentials = new StorageCredentialsAccountAndKey( accountName.split("\\.")[0], accountKey); } return new CloudStorageAccount(credentials); }
static CloudStorageAccount createStorageAccount(String accountName, Configuration conf, boolean allowAnonymous) throws URISyntaxException, KeyProviderException { String accountKey = AzureNativeFileSystemStore .getAccountKeyFromConfiguration(accountName, conf); StorageCredentials credentials; if (accountKey == null && allowAnonymous) { credentials = StorageCredentialsAnonymous.ANONYMOUS; } else { credentials = new StorageCredentialsAccountAndKey( accountName.split("\\.")[0], accountKey); } if (credentials == null) { return null; } else { return new CloudStorageAccount(credentials); } }
public TableClient create() { try { final CloudStorageAccount storageAccount = new CloudStorageAccount(azureTableConfiguration.getStorageCredentialsAccountAndKey(), true); final CloudTableClient cloudTableClient = storageAccount.createCloudTableClient(); final TableRequestOptions defaultOptions = new TableRequestOptions(); defaultOptions.setRetryPolicyFactory(new RetryLinearRetry( Ints.checkedCast(azureTableConfiguration.getRetryInterval().toMilliseconds()), azureTableConfiguration.getRetryAttempts())); defaultOptions.setTimeoutIntervalInMs(Ints.checkedCast(azureTableConfiguration.getTimeout().toMilliseconds())); cloudTableClient.setDefaultRequestOptions(defaultOptions); return new TableClient(cloudTableClient); } catch (URISyntaxException err) { LOGGER.error("Failed to create a TableClient", err); throw new IllegalArgumentException(err); } } }
StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(accountName, accountKey); if(blobEndpoint == null || queueEndpoint == null || tableEndpoint == null){ account = new CloudStorageAccount(credentials); } else { account = new CloudStorageAccount(credentials,blobEndpoint, queueEndpoint, tableEndpoint);
public static CloudQueueClient createCloudQueueClient(SharedAccessAccountPolicy policy, boolean useHttps) throws StorageException, InvalidKeyException, URISyntaxException { CloudStorageAccount sasAccount = getAccount(); final String token = sasAccount.generateSharedAccessSignature(policy); final StorageCredentials creds = new StorageCredentialsSharedAccessSignature(token); final URI queueUri = new URI(useHttps ? "https" : "http", sasAccount.getQueueEndpoint().getAuthority(), sasAccount.getQueueEndpoint().getPath(), sasAccount.getQueueEndpoint().getQuery(), null); sasAccount = new CloudStorageAccount(creds, sasAccount.getBlobEndpoint(), queueUri, sasAccount.getTableEndpoint(), sasAccount.getFileEndpoint()); return sasAccount.createCloudQueueClient(); }
public static CloudTableClient createCloudTableClient(SharedAccessAccountPolicy policy, boolean useHttps) throws StorageException, InvalidKeyException, URISyntaxException { CloudStorageAccount sasAccount = getAccount(); final String token = sasAccount.generateSharedAccessSignature(policy); final StorageCredentials creds = new StorageCredentialsSharedAccessSignature(token); final URI tableUri = new URI(useHttps ? "https" : "http", sasAccount.getTableEndpoint().getAuthority(), sasAccount.getTableEndpoint().getPath(), sasAccount.getTableEndpoint().getQuery(), null); sasAccount = new CloudStorageAccount(creds, sasAccount.getBlobEndpoint(), sasAccount.getQueueEndpoint(), tableUri, sasAccount.getFileEndpoint()); return sasAccount.createCloudTableClient(); }
public static CloudBlobClient createCloudBlobClient(SharedAccessAccountPolicy policy, boolean useHttps) throws StorageException, InvalidKeyException, URISyntaxException { CloudStorageAccount sasAccount = getAccount(); final String token = sasAccount.generateSharedAccessSignature(policy); final StorageCredentials creds = new StorageCredentialsSharedAccessSignature(token); final URI blobUri = new URI(useHttps ? "https" : "http", sasAccount.getBlobEndpoint().getAuthority(), sasAccount.getBlobEndpoint().getPath(), sasAccount.getBlobEndpoint().getQuery(), null); sasAccount = new CloudStorageAccount(creds, blobUri, sasAccount.getQueueEndpoint(), sasAccount.getTableEndpoint(), sasAccount.getFileEndpoint()); return sasAccount.createCloudBlobClient(); }
public static CloudFileClient createCloudFileClient(SharedAccessAccountPolicy policy, boolean useHttps) throws StorageException, InvalidKeyException, URISyntaxException { CloudStorageAccount sasAccount = getAccount(); final String token = sasAccount.generateSharedAccessSignature(policy); final StorageCredentials creds = new StorageCredentialsSharedAccessSignature(token); final URI fileUri = new URI(useHttps ? "https" : "http", sasAccount.getFileEndpoint().getAuthority(), sasAccount.getFileEndpoint().getPath(), sasAccount.getFileEndpoint().getQuery(), null); sasAccount = new CloudStorageAccount( creds, sasAccount.getBlobEndpoint(), sasAccount.getQueueEndpoint(), sasAccount.getTableEndpoint(), fileUri); return sasAccount.createCloudFileClient(); }
@Test public void testCloudStorageAccountAnonymousRoundtrip() throws InvalidKeyException, URISyntaxException { String accountString = "BlobEndpoint=http://blobs/"; assertEquals(accountString, CloudStorageAccount.parse(accountString).toString(true)); CloudStorageAccount account = new CloudStorageAccount( null, new StorageUri(new URI("http://blobs/")), null, null, null); AccountsAreEqual(account, CloudStorageAccount.parse(account.toString(true))); }
@Override public void init() throws Exception { super.init(); // Validate configuration // Can throw IAEs this.validateConfiguration(); try { StorageCredentials credentials = new StorageCredentialsAccountAndKey(storage_account_name, storage_access_key); CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, use_https); CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); containerReference = blobClient.getContainerReference(container); boolean created = containerReference.createIfNotExists(); if (created) { log.info("Created container named '%s'.", container); } else { log.debug("Using existing container named '%s'.", container); } } catch (Exception ex) { log.error("Error creating a storage client! Check your configuration.", ex); } }
@Test public void testCloudStorageAccountDefaultStorageAccountWithHttp() throws URISyntaxException, InvalidKeyException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(cred, false); assertEquals(cloudStorageAccount.getBlobEndpoint(), new URI(String.format("http://%s.blob.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getQueueEndpoint(), new URI(String.format("http://%s.queue.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getTableEndpoint(), new URI(String.format("http://%s.table.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getFileEndpoint(), new URI(String.format("http://%s.file.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getBlobStorageUri().getSecondaryUri(), new URI(String.format("http://%s-secondary.blob.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getQueueStorageUri().getSecondaryUri(), new URI(String.format("http://%s-secondary.queue.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getTableStorageUri().getSecondaryUri(), new URI(String.format("http://%s-secondary.table.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getFileStorageUri().getSecondaryUri(), new URI(String.format("http://%s-secondary.file.core.windows.net", ACCOUNT_NAME))); String cloudStorageAccountToStringWithSecrets = cloudStorageAccount.toString(true); CloudStorageAccount testAccount = CloudStorageAccount.parse(cloudStorageAccountToStringWithSecrets); AccountsAreEqual(testAccount, cloudStorageAccount); }
@Test public void testCloudStorageAccountDefaultStorageAccountWithHttps() throws URISyntaxException, InvalidKeyException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(cred, true); assertEquals(cloudStorageAccount.getBlobEndpoint(), new URI(String.format("https://%s.blob.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getQueueEndpoint(), new URI(String.format("https://%s.queue.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getTableEndpoint(), new URI(String.format("https://%s.table.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getFileEndpoint(), new URI(String.format("https://%s.file.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getBlobStorageUri().getSecondaryUri(), new URI(String.format("https://%s-secondary.blob.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getQueueStorageUri().getSecondaryUri(), new URI(String.format("https://%s-secondary.queue.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getTableStorageUri().getSecondaryUri(), new URI(String.format("https://%s-secondary.table.core.windows.net", ACCOUNT_NAME))); assertEquals(cloudStorageAccount.getFileStorageUri().getSecondaryUri(), new URI(String.format("https://%s-secondary.file.core.windows.net", ACCOUNT_NAME))); String cloudStorageAccountToStringWithSecrets = cloudStorageAccount.toString(true); CloudStorageAccount testAccount = CloudStorageAccount.parse(cloudStorageAccountToStringWithSecrets); AccountsAreEqual(testAccount, cloudStorageAccount); }
@Override public void init() throws Exception { super.init(); // Validate configuration // Can throw IAEs this.validateConfiguration(); try { StorageCredentials credentials = new StorageCredentialsAccountAndKey(storage_account_name, storage_access_key); CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, use_https); CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); containerReference = blobClient.getContainerReference(container); boolean created = containerReference.createIfNotExists(); if (created) { log.info("Created container named '%s'.", container); } else { log.debug("Using existing container named '%s'.", container); } } catch (Exception ex) { log.error("Error creating a storage client! Check your configuration."); throw ex; } }
@Test public void testCloudStorageAccountClientUriVerify() throws URISyntaxException, StorageException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(cred, true); CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient(); CloudBlobContainer container = blobClient.getContainerReference("container1"); assertEquals(cloudStorageAccount.getBlobEndpoint().toString() + "/container1", container.getUri().toString()); CloudQueueClient queueClient = cloudStorageAccount.createCloudQueueClient(); CloudQueue queue = queueClient.getQueueReference("queue1"); assertEquals(cloudStorageAccount.getQueueEndpoint().toString() + "/queue1", queue.getUri().toString()); CloudTableClient tableClient = cloudStorageAccount.createCloudTableClient(); CloudTable table = tableClient.getTableReference("table1"); assertEquals(cloudStorageAccount.getTableEndpoint().toString() + "/table1", table.getUri().toString()); CloudFileClient fileClient = cloudStorageAccount.createCloudFileClient(); CloudFileShare share = fileClient.getShareReference("share1"); assertEquals(cloudStorageAccount.getFileEndpoint().toString() + "/share1", share.getUri().toString()); }
@Test public void testIsUsePathStyleUri() throws InvalidKeyException, URISyntaxException, StorageException { // normal account StorageCredentials creds = new StorageCredentialsAccountAndKey("testAccountName", "/g6UPBuy0ypCpAbYTL6/KA+dI/7gyoWvLFYmah3IviUP1jykOHHOlA=="); CloudStorageAccount account = new CloudStorageAccount(creds); testIsUsePathStyleUri(creds, account.getTableEndpoint().toString(), false); // normal account with path creds = new StorageCredentialsAccountAndKey("testAccountName", "/g6UPBuy0ypCpAbYTL6/KA+dI/7gyoWvLFYmah3IviUP1jykOHHOlA=="); account = new CloudStorageAccount(creds); testIsUsePathStyleUri(creds, account.getTableEndpoint().toString() + "/mytable", false); // custom endpoint creds = new StorageCredentialsAccountAndKey("testAccountName", "/g6UPBuy0ypCpAbYTL6/KA+dI/7gyoWvLFYmah3IviUP1jykOHHOlA=="); testIsUsePathStyleUri(creds, "http://www.contoso.com", false); // dev store account = CloudStorageAccount.getDevelopmentStorageAccount(); testIsUsePathStyleUri(account.getCredentials(), account.getTableEndpoint().toString(), true); // dev store with proxy account = CloudStorageAccount.getDevelopmentStorageAccount(new URI("http://ipv4.fiddler")); testIsUsePathStyleUri(account.getCredentials(), account.getTableEndpoint().toString(), true); // custom endpoints ipv4 with path-style (internal test) creds = new StorageCredentialsAccountAndKey("testAccountName", "/g6UPBuy0ypCpAbYTL6/KA+dI/7gyoWvLFYmah3IviUP1jykOHHOlA=="); testIsUsePathStyleUri(creds, "http://93.184.216.119/testAccountName", true); }
@Test public void testCloudStorageAccountClientMethods() throws URISyntaxException { StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY); CloudStorageAccount account = new CloudStorageAccount(cred, false); CloudBlobClient blob = account.createCloudBlobClient(); CloudQueueClient queue = account.createCloudQueueClient(); CloudTableClient table = account.createCloudTableClient(); CloudFileClient file = account.createCloudFileClient(); // check endpoints assertEquals("Blob endpoint doesn't match account", account.getBlobEndpoint(), blob.getEndpoint()); assertEquals("Queue endpoint doesn't match account", account.getQueueEndpoint(), queue.getEndpoint()); assertEquals("Table endpoint doesn't match account", account.getTableEndpoint(), table.getEndpoint()); assertEquals("File endpoint doesn't match account", account.getFileEndpoint(), file.getEndpoint()); // check storage uris assertEquals("Blob endpoint doesn't match account", account.getBlobStorageUri(), blob.getStorageUri()); assertEquals("Queue endpoint doesn't match account", account.getQueueStorageUri(), queue.getStorageUri()); assertEquals("Table endpoint doesn't match account", account.getTableStorageUri(), table.getStorageUri()); assertEquals("File endpoint doesn't match account", account.getFileStorageUri(), file.getStorageUri()); // check creds assertEquals("Blob creds don't match account", account.getCredentials(), blob.getCredentials()); assertEquals("Queue creds don't match account", account.getCredentials(), queue.getCredentials()); assertEquals("Table creds don't match account", account.getCredentials(), table.getCredentials()); assertEquals("File creds don't match account", account.getCredentials(), file.getCredentials()); }