Java Code Examples for com.microsoft.azure.storage.blob.CloudBlobContainer#getBlockBlobReference()

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlobContainer#getBlockBlobReference() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AzureStorageUploader.java    From movie-db-java-on-azure with MIT License 8 votes vote down vote up
/**
 * Upload image file to Azure storage with specified name.
 *
 * @param file     image file object
 * @param fileName specified file name
 * @return relative path of the created image blob
 */
public String uploadToAzureStorage(ApplicationContext applicationContext, MultipartFile file, String fileName) {
    String uri = null;

    try {
        CloudStorageAccount storageAccount =
                (CloudStorageAccount) applicationContext.getBean("cloudStorageAccount");
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        setupContainer(blobClient, this.thumbnailImageContainer);
        CloudBlobContainer originalImageContainer = setupContainer(blobClient, this.originalImageContainer);

        if (originalImageContainer != null) {
            CloudBlockBlob blob = originalImageContainer.getBlockBlobReference(fileName);
            blob.upload(file.getInputStream(), file.getSize());

            uri = blob.getUri().getPath();
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Error uploading image: " + e.getMessage());
    }

    return uri;
}
 
Example 2
Source File: AzureStorageService.java    From crate with Apache License 2.0 7 votes vote down vote up
public void writeBlob(String container, String blobName, InputStream inputStream, long blobSize,
                      boolean failIfAlreadyExists)
    throws URISyntaxException, StorageException, IOException {
    LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {})", blobName, blobSize));
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    final CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);
    try {
        final AccessCondition accessCondition =
            failIfAlreadyExists ? AccessCondition.generateIfNotExistsCondition() : AccessCondition.generateEmptyCondition();
        blob.upload(inputStream, blobSize, accessCondition, null, client.v2().get());
    } catch (final StorageException se) {
        if (failIfAlreadyExists && se.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT &&
            StorageErrorCodeStrings.BLOB_ALREADY_EXISTS.equals(se.getErrorCode())) {
            throw new FileAlreadyExistsException(blobName, null, se.getMessage());
        }
        throw se;
    }
    LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {}) - done", blobName, blobSize));
}
 
Example 3
Source File: AzureCloudBlobClientActions.java    From cloudbreak with Apache License 2.0 7 votes vote down vote up
private void deleteBlobsInDirectory(CloudBlobContainer cloudBlobContainer, String directoryName)
        throws URISyntaxException, StorageException {

    CloudBlobDirectory blobDirectory = cloudBlobContainer.getDirectoryReference(directoryName);
    for (ListBlobItem blobItem : blobDirectory.listBlobs()) {
        if (blobItem instanceof CloudBlobDirectory) {
            deleteBlobsInDirectory(cloudBlobContainer, ((CloudBlobDirectory) blobItem).getPrefix());
        } else if (blobItem instanceof CloudPageBlob) {
            CloudPageBlob cloudPageBlob = cloudBlobContainer.getPageBlobReference(((CloudPageBlob) blobItem).getName());
            cloudPageBlob.deleteIfExists();
        } else if (blobItem instanceof CloudBlockBlob) {
            CloudBlockBlob cloudBlockBlob = cloudBlobContainer.getBlockBlobReference(((CloudBlockBlob) blobItem).getName());
            cloudBlockBlob.deleteIfExists();
        }
    }
}
 
Example 4
Source File: AzureBlobStorageTestAccount.java    From big-c with Apache License 2.0 7 votes vote down vote up
private static CloudBlockBlob primeRootContainer(CloudBlobClient blobClient,
    String accountName, String blobName, int fileSize) throws Exception {

  // Create a container if it does not exist. The container name
  // must be lower case.
  CloudBlobContainer container = blobClient.getContainerReference("https://"
      + accountName + "/" + "$root");
  container.createIfNotExists();

  // Create a blob output stream.
  CloudBlockBlob blob = container.getBlockBlobReference(blobName);
  BlobOutputStream outputStream = blob.openOutputStream();

  outputStream.write(new byte[fileSize]);
  outputStream.close();

  // Return a reference to the block blob object.
  return blob;
}
 
Example 5
Source File: AzureFileManager.java    From SEAL-Demo with MIT License 6 votes vote down vote up
/**
 * Delete an existing file from Azure.
 * @param containerName Name of the Azure Blob Container
 * @param destinationName File name
 * @throws URISyntaxException if containerName contains incorrect Uri syntax
 * @throws InvalidKeyException if containerName contains an invalid key
 * @throws StorageException if the blob client is unable to get a container reference
 * @throws ExecutionException if the blob client is unable to get a container reference
 * @throws InterruptedException if the blob client is unable to get a container reference
 */
public static void DeleteFile(String containerName, String destinationName)
        throws URISyntaxException,
               StorageException,
               ExecutionException,
               InterruptedException {
    CloudBlobContainer container = getContainer(containerName);

    if (!container.exists()) {
        throw new IllegalArgumentException("Container does not exist");
    }

    CloudBlockBlob fileBlob = container.getBlockBlobReference(destinationName);
    fileBlob.deleteIfExists();
}
 
Example 6
Source File: PreviewStorageAzure.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] read(String id) throws IOException {
	long startTime = System.nanoTime();

	CloudBlobContainer container = getContainer(id);
	try {
		CloudBlockBlob blob = container.getBlockBlobReference(id);
		if (!blob.exists()) return null;
		blob.downloadAttributes();
		int imageSize = (int) blob.getProperties().getLength();
		byte[] image = new byte[imageSize];
		blob.downloadToByteArray(image, 0);
		VIDEO_PREVIEW_AZURE_DOWNLOAD_SUCCESS.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
		return image;
	} catch (Exception e) {
		VIDEO_PREVIEW_AZURE_DOWNLOAD_FAIL.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
		throw new IOException("Failed to read image " + id);
	}
}
 
Example 7
Source File: AnalyticsTestHelper.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
public static List<String> CreateLogs(CloudBlobContainer container, StorageService service, int count,
        Calendar start, Granularity granularity) throws URISyntaxException, StorageException, IOException {
    String name;
    List<String> blobs = new ArrayList<String>();
    DateFormat hourFormat = new SimpleDateFormat("yyyy/MM/dd/HH");
    hourFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    CloudBlockBlob blockBlob;
    name = service.toString().toLowerCase(Locale.US) + "/" + hourFormat.format(start.getTime()) + "00/000001.log";
    blockBlob = container.getBlockBlobReference(name);
    blockBlob.upload(getRandomDataStream(1), 1, null, null, null);
    blobs.add(name);

    for (int i = 1; i < count; i++) {
        if (granularity.equals(Granularity.HOUR)) {
            start.add(GregorianCalendar.HOUR_OF_DAY, 1);
        }
        else if (granularity.equals(Granularity.DAY)) {
            start.add(GregorianCalendar.DAY_OF_MONTH, 1);
        }
        else if (granularity.equals(Granularity.MONTH)) {
            start.add(GregorianCalendar.MONTH, 1);
        }
        else {
            throw new IllegalArgumentException(String.format(Locale.US,
                    "CreateLogs granularity of '{0}' is invalid.", granularity));
        }

        name = service.toString().toLowerCase(Locale.US) + "/" + hourFormat.format(start.getTime())
                + "00/000001.log";
        blockBlob = container.getBlockBlobReference(name);
        blockBlob.upload(getRandomDataStream(1), 1, null, null, null);
        blobs.add(name);
    }

    return blobs;
}
 
Example 8
Source File: TestWasbUriAndConfiguration.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectUsingSASReadonly() throws Exception {
  // Create the test account with SAS credentials.
  testAccount = AzureBlobStorageTestAccount.create("", EnumSet.of(
      CreateOptions.UseSas, CreateOptions.CreateContainer,
      CreateOptions.Readonly));
  assumeNotNull(testAccount);

  // Create a blob in there
  final String blobKey = "blobForReadonly";
  CloudBlobContainer container = testAccount.getRealContainer();
  CloudBlockBlob blob = container.getBlockBlobReference(blobKey);
  ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1,
      2, 3 });
  blob.upload(inputStream, 3);
  inputStream.close();

  // Make sure we can read it from the file system
  Path filePath = new Path("/" + blobKey);
  FileSystem fs = testAccount.getFileSystem();
  assertTrue(fs.exists(filePath));
  byte[] obtained = new byte[3];
  DataInputStream obtainedInputStream = fs.open(filePath);
  obtainedInputStream.readFully(obtained);
  obtainedInputStream.close();
  assertEquals(3, obtained[2]);
}
 
Example 9
Source File: AzureFileUtility.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
/**
 * This method will remove the file from Azure Storage.
 *
 * @param fileName
 * @param containerName
 * @return boolean
 */
public static boolean deleteFile(String containerName, String fileName) {
  if (fileName == null) {
    ProjectLogger.log("File name can not be null");
    return false;
  }
  if (StringUtils.isBlank(containerName)) {
    ProjectLogger.log("Container name can't be null or empty");
    return false;
  }
  CloudBlobContainer container = AzureConnectionManager.getContainer(containerName, true);
  if (container == null) {
    ProjectLogger.log("Unable to get Azure contains object");
    return false;
  }
  try {
    // Retrieve reference to a blob named "myimage.jpg".
    CloudBlockBlob blob = container.getBlockBlobReference(fileName);
    // Delete the blob.
    boolean response = blob.deleteIfExists();
    if (!response) {
      ProjectLogger.log("Provided file not found to delete.");
    }
    return true;
  } catch (Exception e) {
    ProjectLogger.log(e.getMessage(), e);
  }
  return false;
}
 
Example 10
Source File: ManageWebAppStorageAccountConnection.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
private static void uploadFileToContainer(CloudBlobContainer container, String fileName, String filePath) {
    try {
        CloudBlob blob = container.getBlockBlobReference(fileName);
        blob.uploadFromFile(filePath);
    } catch (StorageException | URISyntaxException | IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: ManageLinuxWebAppStorageAccountConnection.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
private static void uploadFileToContainer(CloudBlobContainer container, String fileName, String filePath) {
    try {
        CloudBlob blob = container.getBlockBlobReference(fileName);
        blob.uploadFromFile(filePath);
    } catch (StorageException | URISyntaxException | IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: AzureStorageHelper.java    From azure-gradle-plugins with MIT License 5 votes vote down vote up
public static void deleteBlob(final CloudStorageAccount storageAccount, final String containerName,
                              final String blobName) throws Exception {
    final CloudBlobContainer blobContainer = getBlobContainer(storageAccount, containerName);
    if (blobContainer.exists()) {
        final CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);
        blob.deleteIfExists();
    }
}
 
Example 13
Source File: AzureStorageBlobService.java    From components with Apache License 2.0 5 votes vote down vote up
public void upload(final String containerName, final String blobName, final InputStream sourceStream, final long length)
        throws StorageException, IOException, URISyntaxException, InvalidKeyException {
    CloudBlobClient cloudBlobClient = connection.getCloudStorageAccount().createCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName);
    CloudBlockBlob blob = cloudBlobContainer.getBlockBlobReference(blobName);
    blob.upload(sourceStream, length, null, null, AzureStorageUtils.getTalendOperationContext());
}
 
Example 14
Source File: AzureTableStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
@Override
public InputStreamWrapper getStream(UUID jobId, String key) {
  try {
    CloudBlobContainer reference = blobClient.getContainerReference(BLOB_CONTAINER);
    CloudBlockBlob blob = reference.getBlockBlobReference(createRowKey(jobId, key));
    return new InputStreamWrapper(blob.openInputStream(), blob.getProperties().getLength());
  } catch (StorageException | URISyntaxException e) {
    throw new MicrosoftStorageException("Error returning stream for job: " + jobId, e);
  }
}
 
Example 15
Source File: AzureTableStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
@Override
public void create(UUID jobId, String key, InputStream stream) {
  try {
    CloudBlobContainer reference = blobClient.getContainerReference(BLOB_CONTAINER);
    CloudBlockBlob blob = reference.getBlockBlobReference(createRowKey(jobId, key));
    blob.upload(stream, UNKNOWN_LENGTH);
  } catch (StorageException | URISyntaxException | IOException e) {
    throw new MicrosoftStorageException("Error creating stream for job: " + jobId, e);
  }
}
 
Example 16
Source File: DeleteAzureBlobStorage.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();

    if(flowFile == null) {
        return;
    }

    final long startNanos = System.nanoTime();
    final String containerName = context.getProperty(AzureStorageUtils.CONTAINER).evaluateAttributeExpressions(flowFile).getValue();
    final String blobPath = context.getProperty(BLOB).evaluateAttributeExpressions(flowFile).getValue();
    final String deleteSnapshotOptions = context.getProperty(DELETE_SNAPSHOTS_OPTION).getValue();

    try {
        CloudBlobClient blobClient = AzureStorageUtils.createCloudBlobClient(context, getLogger(), flowFile);
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        CloudBlob blob = container.getBlockBlobReference(blobPath);

        final OperationContext operationContext = new OperationContext();
        AzureStorageUtils.setProxy(operationContext, context);
        blob.deleteIfExists(DeleteSnapshotsOption.valueOf(deleteSnapshotOptions), null, null, operationContext);
        session.transfer(flowFile, REL_SUCCESS);

        final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
        session.getProvenanceReporter().invokeRemoteProcess(flowFile, blob.getSnapshotQualifiedUri().toString(), "Blob deleted");
    } catch ( StorageException | URISyntaxException e) {
        getLogger().error("Failed to delete the specified blob {} from Azure Storage. Routing to failure", new Object[]{blobPath}, e);
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
    }
}
 
Example 17
Source File: FileSasTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testFileCopyFromBlobWithSasAndSnapshot()
        throws URISyntaxException, StorageException, InterruptedException, IOException, InvalidKeyException {
    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    CloudBlobContainer container = TestHelper.createCloudBlobClient().getContainerReference(BlobTestHelper.generateRandomContainerName());
    container.createIfNotExists();
    CloudBlockBlob source = container.getBlockBlobReference(blobName);
    String data = "String data";
    source.uploadText(data, Constants.UTF8_CHARSET, null, null, null);

    byte[] buffer = BlobTestHelper.getRandomBuffer(512);
    ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
    source.upload(stream, buffer.length);
    source.getMetadata().put("Test", "value");
    source.uploadMetadata();

    SharedAccessFilePolicy policy = createSharedAccessPolicy(
            EnumSet.of(SharedAccessFilePermissions.READ, SharedAccessFilePermissions.WRITE,
                  SharedAccessFilePermissions.LIST, SharedAccessFilePermissions.DELETE), 5000);

    CloudFile copy = this.share.getRootDirectoryReference().getFileReference("copy");
    String sasToken = copy.generateSharedAccessSignature(policy, null);
    CloudFile copySas = new CloudFile(new URI(copy.getUri().toString() + "?" + sasToken));
    
    // Generate account SAS for the source
    // Cannot generate a SAS directly on a snapshot and the SAS for the destination is only for the destination
    SharedAccessAccountPolicy accountPolicy = new SharedAccessAccountPolicy();
    accountPolicy.setPermissions(EnumSet.of(SharedAccessAccountPermissions.READ, SharedAccessAccountPermissions.WRITE));
    accountPolicy.setServices(EnumSet.of(SharedAccessAccountService.BLOB));
    accountPolicy.setResourceTypes(EnumSet.of(SharedAccessAccountResourceType.OBJECT, SharedAccessAccountResourceType.CONTAINER));
    accountPolicy.setSharedAccessExpiryTime(policy.getSharedAccessExpiryTime());
    final CloudBlobClient sasClient = TestHelper.createCloudBlobClient(accountPolicy, false);

    CloudBlockBlob snapshot = (CloudBlockBlob) source.createSnapshot();
    CloudBlockBlob sasBlob = (CloudBlockBlob) sasClient.getContainerReference(container.getName())
            .getBlobReferenceFromServer(snapshot.getName(), snapshot.getSnapshotID(), null, null, null);
    sasBlob.exists();

    String copyId = copySas.startCopy(BlobTestHelper.defiddler(sasBlob));
    FileTestHelper.waitForCopy(copySas);
    
    copySas.downloadAttributes();
    FileProperties prop1 = copySas.getProperties();
    BlobProperties prop2 = sasBlob.getProperties();

    assertEquals(prop1.getCacheControl(), prop2.getCacheControl());
    assertEquals(prop1.getContentEncoding(), prop2.getContentEncoding());
    assertEquals(prop1.getContentDisposition(),
            prop2.getContentDisposition());
    assertEquals(prop1.getContentLanguage(), prop2.getContentLanguage());
    assertEquals(prop1.getContentMD5(), prop2.getContentMD5());
    assertEquals(prop1.getContentType(), prop2.getContentType());

    assertEquals("value", copySas.getMetadata().get("Test"));
    assertEquals(copyId, copySas.getCopyState().getCopyId());

    snapshot.delete();
    source.delete();
    copySas.delete();
    container.delete();
}
 
Example 18
Source File: AzureBlobStorageTestAccount.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public static void primePublicContainer(CloudBlobClient blobClient,
    String accountName, String containerName, String blobName, int fileSize)
    throws Exception {

  // Create a container if it does not exist. The container name
  // must be lower case.
  CloudBlobContainer container = blobClient
      .getContainerReference(containerName);

  container.createIfNotExists();

  // Create a new shared access policy.
  SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy();

  // Set READ and WRITE permissions.
  //
  sasPolicy.setPermissions(EnumSet.of(
      SharedAccessBlobPermissions.READ,
      SharedAccessBlobPermissions.WRITE,
      SharedAccessBlobPermissions.LIST,
      SharedAccessBlobPermissions.DELETE));

  // Create the container permissions.
  BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

  // Turn public access to the container off.
  containerPermissions
      .setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

  // Set the policy using the values set above.
  containerPermissions.getSharedAccessPolicies().put("testwasbpolicy",
      sasPolicy);
  container.uploadPermissions(containerPermissions);

  // Create a blob output stream.
  CloudBlockBlob blob = container.getBlockBlobReference(blobName);
  BlobOutputStream outputStream = blob.openOutputStream();

  outputStream.write(new byte[fileSize]);
  outputStream.close();
}
 
Example 19
Source File: MaximumExecutionTimeTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class, SlowTests.class })
public void testMaximumExecutionTimeBlobByteArray() throws URISyntaxException, StorageException, IOException {
    int length = 10 * 1024 * 1024;
    byte[] uploadBuffer = BlobTestHelper.getRandomBuffer(length);
    byte[] downloadBuffer = new byte[length];

    // set a delay in sending request
    OperationContext opContext = new OperationContext();
    setDelay(opContext, 2500);

    // set the maximum execution time
    BlobRequestOptions options = new BlobRequestOptions();
    options.setMaximumExecutionTimeInMs(2000);

    CloudBlobClient blobClient = TestHelper.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(generateRandomName("container"));

    String blobName = "testBlob";
    final CloudBlockBlob blockBlobRef = container.getBlockBlobReference(blobName);

    ByteArrayInputStream inputStream = new ByteArrayInputStream(uploadBuffer);

    try {
        container.createIfNotExists();

        blockBlobRef.upload(inputStream, length);
        assertTrue(blockBlobRef.exists());

        try {
            blockBlobRef.downloadToByteArray(downloadBuffer, 0, null, options, opContext);
            fail("Maximum execution time was reached but request did not fail.");
        }
        catch (StorageException e) {
            assertEquals(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION, e.getCause().getMessage());
        }
    }
    finally {
        inputStream.close();
        container.deleteIfExists();
    }
}
 
Example 20
Source File: AzureFileManager.java    From SEAL-Demo with MIT License 3 votes vote down vote up
/**
 * Upload a file to Azure
 * @param file File to upload
 * @param fileSize Size of file to upload
 * @param containerName Name of the Azure Blob Container
 * @param destinationName File name
 * @throws URISyntaxException if containerName contains incorrect Uri syntax
 * @throws InvalidKeyException if containerName contains an invalid key
 * @throws StorageException if the blob client is unable to get a container reference
 * @throws IOException if there is an error reading the file
 * @throws ExecutionException if the blob client is unable to get a container reference
 * @throws InterruptedException if the blob client is unable to get a container reference
 */
public static void UploadFile(InputStream file, long fileSize, String containerName, String destinationName)
        throws URISyntaxException,
               StorageException,
               IOException,
               ExecutionException,
               InterruptedException {
    CloudBlobContainer container = getContainer(containerName);

    CloudBlockBlob fileBlob = container.getBlockBlobReference(destinationName);
    fileBlob.upload(file, fileSize);
}