Search in sources :

Example 1 with BlockBlobClient

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);
}
Also used : BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) FileUploadSasUriResponse(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse) BlobClientBuilder(com.azure.storage.blob.BlobClientBuilder) FileUploadCompletionNotification(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification) FileUploadSasUriRequest(com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) Test(org.junit.Test)

Example 2 with BlockBlobClient

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();
}
Also used : BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 3 with BlockBlobClient

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;
}
Also used : BlobContainerClient(com.azure.storage.blob.BlobContainerClient) BlobClient(com.azure.storage.blob.BlobClient) BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) HashMap(java.util.HashMap) BlobProperties(com.azure.storage.blob.models.BlobProperties)

Example 4 with BlockBlobClient

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();
}
Also used : BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) ListBlobContainersOptions(com.azure.storage.blob.models.ListBlobContainersOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BlobStorageException(com.azure.storage.blob.models.BlobStorageException)

Example 5 with BlockBlobClient

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.");
    }
}
Also used : JobProperties(com.microsoft.azure.sdk.iot.service.JobProperties) BlockBlobClient(com.azure.storage.blob.specialized.BlockBlobClient) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobInputStream(com.azure.storage.blob.specialized.BlobInputStream) InputStream(java.io.InputStream) ExportImportDeviceParser(com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser) IotHubTooManyDevicesException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException)

Aggregations

BlockBlobClient (com.azure.storage.blob.specialized.BlockBlobClient)5 BlobClient (com.azure.storage.blob.BlobClient)1 BlobClientBuilder (com.azure.storage.blob.BlobClientBuilder)1 BlobContainerClient (com.azure.storage.blob.BlobContainerClient)1 BlobProperties (com.azure.storage.blob.models.BlobProperties)1 BlobStorageException (com.azure.storage.blob.models.BlobStorageException)1 ListBlobContainersOptions (com.azure.storage.blob.models.ListBlobContainersOptions)1 BlobInputStream (com.azure.storage.blob.specialized.BlobInputStream)1 ExportImportDeviceParser (com.microsoft.azure.sdk.iot.deps.serializer.ExportImportDeviceParser)1 FileUploadCompletionNotification (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadCompletionNotification)1 FileUploadSasUriRequest (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriRequest)1 FileUploadSasUriResponse (com.microsoft.azure.sdk.iot.deps.serializer.FileUploadSasUriResponse)1 JobProperties (com.microsoft.azure.sdk.iot.service.JobProperties)1 IotHubTooManyDevicesException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubTooManyDevicesException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1