use of com.azure.storage.blob.specialized.BlockBlobClient in project azure-iot-sdk-java by Azure.
the class FileUploadTests method uploadToBlobAsyncSingleFileGranular.
@Test(timeout = MAX_MILLISECS_TIMEOUT_KILL_TEST)
public void uploadToBlobAsyncSingleFileGranular() throws URISyntaxException, IOException, InterruptedException, IotHubException, GeneralSecurityException {
// arrange
DeviceClient deviceClient = setUpDeviceClient(testInstance.protocol);
// act
FileUploadSasUriResponse sasUriResponse = deviceClient.getFileUploadSasUri(new FileUploadSasUriRequest(testInstance.fileUploadState[0].blobName));
BlockBlobClient blockBlobClient = new BlobClientBuilder().endpoint(sasUriResponse.getBlobUri().toString()).buildClient().getBlockBlobClient();
blockBlobClient.upload(testInstance.fileUploadState[0].fileInputStream, testInstance.fileUploadState[0].fileLength);
FileUploadCompletionNotification fileUploadCompletionNotification = new FileUploadCompletionNotification();
fileUploadCompletionNotification.setCorrelationId(sasUriResponse.getCorrelationId());
fileUploadCompletionNotification.setStatusCode(0);
fileUploadCompletionNotification.setSuccess(true);
fileUploadCompletionNotification.setStatusDescription("Succeed to upload to storage.");
deviceClient.completeFileUpload(fileUploadCompletionNotification);
// assert
assertEquals(buildExceptionMessage("File upload status should be SUCCESS but was " + testInstance.fileUploadState[0].fileUploadStatus, deviceClient), SUCCESS, testInstance.fileUploadState[0].fileUploadStatus);
tearDownDeviceClient(deviceClient);
}
use of com.azure.storage.blob.specialized.BlockBlobClient in project ambry by linkedin.
the class StorageClient method deleteFile.
/**
* Delete a file from blob storage, if it exists.
* @param containerName name of the container containing file to delete.
* @param fileName name of the file to delete.
* @return true if the file was deleted, otherwise false.
* @throws BlobStorageException for any error on ABS side.
*/
boolean deleteFile(String containerName, String fileName) throws BlobStorageException {
AtomicReference<Boolean> retValRef = new AtomicReference<>(false);
if (azureCloudConfig.useAsyncAzureAPIs) {
return deleteFileAsync(containerName, fileName);
}
doStorageClientOperation(() -> {
BlockBlobClient blobClient = getBlockBlobClient(containerName, fileName, false);
if (blobClient.exists()) {
blobClient.delete();
retValRef.set(true);
}
return null;
});
return retValRef.get();
}
use of com.azure.storage.blob.specialized.BlockBlobClient in project ambry by linkedin.
the class AzureBlobDataAccessorTest method setupMockBlobClient.
static BlockBlobClient setupMockBlobClient(BlobServiceClient mockServiceClient) {
BlobContainerClient mockContainerClient = mock(BlobContainerClient.class);
BlobClient mockBlobClient = mock(BlobClient.class);
BlockBlobClient mockBlockBlobClient = mock(BlockBlobClient.class);
when(mockServiceClient.getBlobContainerClient(anyString())).thenReturn(mockContainerClient);
when(mockContainerClient.getBlobClient(anyString())).thenReturn(mockBlobClient);
when(mockContainerClient.exists()).thenReturn(false);
when(mockBlobClient.getBlockBlobClient()).thenReturn(mockBlockBlobClient);
// Rest is to mock getPropertiesWithResponse and not needed everywhere
BlobProperties mockBlobProperties = mock(BlobProperties.class);
Map<String, String> metadataMap = new HashMap<>();
lenient().when(mockBlobProperties.getMetadata()).thenReturn(metadataMap);
Response<BlobProperties> mockPropertiesResponse = mock(Response.class);
lenient().when(mockPropertiesResponse.getValue()).thenReturn(mockBlobProperties);
lenient().when(mockBlockBlobClient.getPropertiesWithResponse(any(), any(), any())).thenReturn(mockPropertiesResponse);
return mockBlockBlobClient;
}
use of com.azure.storage.blob.specialized.BlockBlobClient in project ambry by linkedin.
the class AzureTokenResetTool method resetTokens.
/**
* Reset the offset token by deleting the blob from the container.
* @param containerPrefix the prefix used to filter on Azure containers
* (in case the storage account hosts multiple Ambry clusters).
* @return the number of tokens successfully reset.
*/
public static int resetTokens(String containerPrefix) throws BlobStorageException {
AtomicInteger tokensDeleted = new AtomicInteger(0);
ListBlobContainersOptions listOptions = new ListBlobContainersOptions().setPrefix(containerPrefix);
storageClient.listBlobContainers(listOptions, null).iterator().forEachRemaining(blobContainer -> {
BlockBlobClient blobClient = storageClient.getBlobContainerClient(blobContainer.getName()).getBlobClient(ReplicationConfig.REPLICA_TOKEN_FILE_NAME).getBlockBlobClient();
try {
if (blobClient.exists()) {
blobClient.delete();
tokensDeleted.incrementAndGet();
logger.info("Deleted token for partition {}", blobContainer.getName());
}
} catch (Exception ex) {
logger.error("Failed delete for {}", blobContainer.getName(), ex);
}
});
return tokensDeleted.get();
}
use of com.azure.storage.blob.specialized.BlockBlobClient in project azure-iot-sdk-java by Azure.
the class ExportImportTests method runImportJob.
private static void runImportJob(List<ExportImportDevice> devices, ImportMode importMode, Optional<StorageAuthenticationType> storageAuthenticationType) throws Exception {
// Creating the json string to be submitted for import using the specified importMode
StringBuilder devicesToAdd = new StringBuilder();
for (int i = 0; i < devices.size(); i++) {
devices.get(i).setImportMode(importMode);
ExportImportDeviceParser parser = Deencapsulation.invoke(devices.get(i), "toExportImportDeviceParser");
devicesToAdd.append(parser.toJson());
if (i < devices.size() - 1) {
devicesToAdd.append("\r\n");
}
}
byte[] blobToImport = devicesToAdd.toString().getBytes(StandardCharsets.UTF_8);
// Creating the Azure storage blob and uploading the serialized string of devices
InputStream stream = new ByteArrayInputStream(blobToImport);
String importBlobName = "devices.txt";
BlockBlobClient importBlob = importContainer.getBlobClient(importBlobName).getBlockBlobClient();
importBlob.delete();
importBlob.upload(stream, blobToImport.length);
// Starting the import job
boolean importJobScheduled = false;
JobProperties importJob = null;
while (!importJobScheduled) {
try {
if (storageAuthenticationType.isPresent()) {
// For a given StorageAuthenticationType, create JobProperties and pass it
JobProperties importJobProperties = JobProperties.createForImportJob(getContainerSasUri(importContainer), getContainerSasUri(importContainer), storageAuthenticationType.get());
importJob = registryManager.importDevices(importJobProperties);
} else {
importJob = registryManager.importDevices(getContainerSasUri(importContainer), getContainerSasUri(importContainer));
}
importJobScheduled = true;
} catch (IotHubTooManyDevicesException e) {
// test is being throttled, wait a while and try again
Thread.sleep(10 * 1000);
}
}
// Waiting for the import job to complete
long startTime = System.currentTimeMillis();
while (true) {
importJob = registryManager.getJob(importJob.getJobId());
if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED || importJob.getStatus() == JobProperties.JobStatus.FAILED) {
break;
}
if (System.currentTimeMillis() - startTime > IMPORT_JOB_TIMEOUT_MILLISECONDS) {
fail("Timed out waiting for the import job to complete");
}
Thread.sleep(100);
}
// Checking the result of the import job
if (importJob.getStatus() != JobProperties.JobStatus.COMPLETED) {
Assert.fail("The import job was not completed successfully for " + importMode + " operation.");
}
}
Aggregations