ATS : Azure Blob Storage Operations

ATS provides support for Microsoft Azure Blob Storage (https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction). You may use com.axway.ats.action.azure.BlobStorageOperations for direct tasks like:

  • Container operations
    • create
    • delete
    • check for existence
    • list blobs/folders
    • list containers
  • Blob operations
    • create 
    • delete
    • upload and download
    • append (if the blob type is APPEND_BLOB)
    • get blob information (name, size, content type, content checksum, etc)
    • check for blob existence
    • add/update blob meta data


Using different blob types

You can create different blob (block, append, page), but the only implemented operation for a specific blob is appended. Other operations like commitBlockList, listBlock, etc are not implemented.

Feel free to log a GitHub issue if such operations will be useful to you (smile)



Folder operations

Working with folders is currently implemented with some limitations.

You can

  • create blobs inside folder, by using the following as a blob name: <folderName>/<blobName>
  • list and/or delete blob from a particular folder.

And here the support for folders sadly ends. 

You cannot:

  • delete folder,
  • upload and/or download folder

Yet again, feel free to log an issue, if more folder operations are needed by your team/project.


Initialization

In order to use ATS (Azure) Blob Storage client, it is needed to:

  •  Add the following dependency (ats-azure-utilities) in your classpath. For Maven, add this into your pom.xml:
pom.xml
<dependency>
    <groupId>com.axway.ats.framework.utilities</groupId>
    <artifactId>ats-azure-utilities</artifactId>
    <version>4.0.7</version> <!-- Or the latest ATS Framework version. 4.0.7 is the minimum version supporting Azure Blob Storage -->
</dependency>
  • Ensure that all of the io.netty jars, are coming from the azure-* dependencies.
  • Ensure that jackson-annotations is the version, used by azure-* dependencies.

If an exception, from the package io.netty.handler, is thrown, some of the above steps were not successful.


Usage

Azure storage operations are handled by the class com.axway.ats.action.azure.BlobStorageOperations

Azure Blob Storage operations example
...
import com.axway.ats.action.azure.BlobStorageOperations;
...

BlobStorageOperations blobOps = new BlobStorageOperations(CONNECTION_STRING, SAS_TOKEN);

// list containers
// you can also specify some prefix for the names of the containers and even set time limit for the operation to complete, so you do not wait here for minutes when the connection is slow
List<String> containers = blobOps.listContainers();

// create empty container
if (blobOps.doesContainerExist(CONTAINER_NAME)) {
    // the container exists, so we will just purge it (deleting all blobs/folders inside)
    blobOps.purgeContainer(CONTAINER_NAME);
} else {
    blobOps.createContainer(CONTAINER_NAME);
}

// upload blob
blobOps.upload(CONTAINER_NAME, BLOB_NAME, LOCAL_FILE_TO_UPLOAD, true); // overwrite any existing blob with the same name

// download blob
blobOps.download(CONTAINER_NAME, BLOB_NAME, LOCAL_FILE_TO_DOWNLOAD, true); // overwrite any existing file, determined by LOCAL_FILE_TO_DOWNLOAD

// if we will be constantly updating a blob with content, we can create and use append blob
// let's upload some log file that is constantly being written to

// first we have to create the append blob
 blobOps.createAppendBlob(CONTAINER_NAME, APPEND_BLOB_NAME, true);

// ensure that the blob exists
if (!blobOps.doesBlobExist(CONTAINER_NAME, APPEND_BLOB_NAME)) {
    throw new RuntimeException("Blob '" + APPEND_BLOB_NAME + "' does not exist");
}

// now start appending
try (FileReader fr = new FileReader(new File(LOG_FILE))) {
    char[] buffer = new char[16384];
    while (fr.read(buffer) != -1) {
        blobOps.appendToBlob(CONTAINER_NAME, APPEND_BLOB_NAME, new String(buffer).getBytes());
    }
}

// now lets check some blob properties (metadata)
BlobInfo bInfo = blobOps.getBlobInfo(CONTAINER_NAME, BLOB_NAME);
log.info("blob size: " + bInfo.getSize());
log.info("blob eTag: " + bInfo.getETag());
log.info("blob content type: " + bInfo.getContentType());
log.info("blob creation time: " + bInfo.getCreationTime().getTime());
log.info("blob last modified: " + bInfo.getLastModified().getTime());
log.info("blob MD5: " + bInfo.getMd5());

// and finally lets clean all of the containers/blobs, created by the test

if (blobOps.doesContainerExist(CONTAINER_NAME)) {
    blobOps.deleteContainer(CONTAINER_NAME); // or just purge it blobOps.purgeContainer(CONTAINER_NAME);
}

For more functionality, you may also refer to Azure Blob Storage Verifications page.


Back to parent page

Go to Table of Contents