use of com.microsoft.azure.storage.CloudStorageAccount in project azure-iot-sdk-java by Azure.
the class DeviceManagerImportSample method main.
public static void main(String[] args) throws Exception {
System.out.println("Starting import sample...");
// Creating Azure storage container and getting its URI
CloudStorageAccount storageAccount = CloudStorageAccount.parse(SampleUtils.storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(DeviceManagerImportSample.importContainerName);
container.createIfNotExists();
String containerSasUri = SampleUtils.getContainerSasUri(container);
// Creating the list of devices to be submitted for import
StringBuilder devicesToImport = new StringBuilder();
for (int i = 0; i < 1; i++) {
String deviceId = UUID.randomUUID().toString();
Device device = Device.createFromId(deviceId, null, null);
AuthenticationMechanism authentication = new AuthenticationMechanism(device.getSymmetricKey());
ExportImportDevice deviceToAdd = new ExportImportDevice();
deviceToAdd.setId(deviceId);
deviceToAdd.setAuthentication(authentication);
deviceToAdd.setStatus(DeviceStatus.Enabled);
deviceToAdd.setImportMode(ImportMode.CreateOrUpdate);
devicesToImport.append(gson.toJson(deviceToAdd));
if (i < numberOfDevice - 1) {
devicesToImport.append("\r\n");
}
}
byte[] blobToImport = devicesToImport.toString().getBytes(StandardCharsets.UTF_8);
// Creating the Azure storage blob and uploading the serialized string of devices
System.out.println("Uploading " + blobToImport.length + " bytes into Azure storage.");
InputStream stream = new ByteArrayInputStream(blobToImport);
CloudBlockBlob importBlob = container.getBlockBlobReference(DeviceManagerImportSample.importBlobName);
importBlob.deleteIfExists();
importBlob.upload(stream, blobToImport.length);
// Starting the import job
RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
JobProperties importJob = registryManager.importDevices(containerSasUri, containerSasUri);
// Waiting for the import job to complete
while (true) {
importJob = registryManager.getJob(importJob.getJobId());
if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED || importJob.getStatus() == JobProperties.JobStatus.FAILED) {
break;
}
Thread.sleep(500);
}
// Checking the result of the import job
if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED) {
System.out.println("Import job completed. The new devices are now added to the hub.");
} else {
System.out.println("Import job failed. Failure reason: " + importJob.getFailureReason());
}
//Cleaning up the blob
for (ListBlobItem blobItem : container.listBlobs()) {
if (blobItem instanceof CloudBlob) {
CloudBlob blob = (CloudBlockBlob) blobItem;
blob.deleteIfExists();
}
}
}
use of com.microsoft.azure.storage.CloudStorageAccount in project azure-iot-sdk-java by Azure.
the class RegistryManagerIT method setUp.
@BeforeClass
public static void setUp() throws URISyntaxException, InvalidKeyException, StorageException {
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
if (envName.equals(iotHubonnectionStringEnvVarName.toString())) {
iotHubConnectionString = env.get(envName);
} else if (envName.equals(storageAccountConnectionStringEnvVarName.toString())) {
storageAccountConnectionString = env.get(envName);
}
}
String uuid = UUID.randomUUID().toString();
deviceId = deviceId.concat("-" + uuid);
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageAccountConnectionString);
blobClient = storageAccount.createCloudBlobClient();
// Creating the export storage container and getting its URI
String exportContainerName = "exportcontainersample-" + uuid;
exportContainer = blobClient.getContainerReference(exportContainerName);
exportContainer.createIfNotExists();
// Creating the import storage container and getting its URI
String importContainerName = "importcontainersample-" + uuid;
importContainer = blobClient.getContainerReference(importContainerName);
importContainer.createIfNotExists();
}
use of com.microsoft.azure.storage.CloudStorageAccount in project hadoop by apache.
the class AzureNativeFileSystemStore method connectUsingCredentials.
private void connectUsingCredentials(String accountName, StorageCredentials credentials, String containerName) throws URISyntaxException, StorageException, AzureException {
URI blobEndPoint;
if (isStorageEmulatorAccount(accountName)) {
isStorageEmulator = true;
CloudStorageAccount account = CloudStorageAccount.getDevelopmentStorageAccount();
storageInteractionLayer.createBlobClient(account);
} else {
blobEndPoint = new URI(getHTTPScheme() + "://" + accountName);
storageInteractionLayer.createBlobClient(blobEndPoint, credentials);
}
suppressRetryPolicyInClientIfNeeded();
// Capture the container reference for debugging purposes.
container = storageInteractionLayer.getContainerReference(containerName);
rootDirectory = container.getDirectoryReference("");
// Can only create container if using account key credentials
canCreateOrModifyContainer = credentials instanceof StorageCredentialsAccountAndKey;
// Configure Azure storage session.
configureAzureStorageSession();
}
use of com.microsoft.azure.storage.CloudStorageAccount in project hadoop by apache.
the class LocalSASKeyGeneratorImpl method getRelativeBlobSASUri.
/**
* Implementation for generation of Relative Path Blob SAS Uri.
*/
@Override
public URI getRelativeBlobSASUri(String accountName, String container, String relativePath) throws SASKeyGenerationException {
CloudBlobContainer sc = null;
CloudBlobClient client = null;
try {
CloudStorageAccount account = getSASKeyBasedStorageAccountInstance(accountName);
client = account.createCloudBlobClient();
sc = client.getContainerReference(container);
} catch (URISyntaxException uriSyntaxEx) {
throw new SASKeyGenerationException("Encountered URISyntaxException " + "while getting container references for container " + container + " inside storage account : " + accountName, uriSyntaxEx);
} catch (StorageException stoEx) {
throw new SASKeyGenerationException("Encountered StorageException while " + "getting container references for container " + container + " inside storage account : " + accountName, stoEx);
}
CloudBlockBlob blob = null;
try {
blob = sc.getBlockBlobReference(relativePath);
} catch (URISyntaxException uriSyntaxEx) {
throw new SASKeyGenerationException("Encountered URISyntaxException while " + "getting Block Blob references for container " + container + " inside storage account : " + accountName, uriSyntaxEx);
} catch (StorageException stoEx) {
throw new SASKeyGenerationException("Encountered StorageException while " + "getting Block Blob references for container " + container + " inside storage account : " + accountName, stoEx);
}
try {
return client.getCredentials().transformUri(blob.getUri());
} catch (StorageException stoEx) {
throw new SASKeyGenerationException("Encountered StorageException while " + "generating SAS key for Blob: " + relativePath + " inside " + "container : " + container + " in Storage Account : " + accountName, stoEx);
} catch (URISyntaxException uriSyntaxEx) {
throw new SASKeyGenerationException("Encountered URISyntaxException " + "while generating SAS key for Blob: " + relativePath + " inside " + "container: " + container + " in Storage Account : " + accountName, uriSyntaxEx);
}
}
use of com.microsoft.azure.storage.CloudStorageAccount in project hadoop by apache.
the class LocalSASKeyGeneratorImpl method getStorageAccountInstance.
/**
* 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);
}
Aggregations