BoxLang 🚀 A New JVM Dynamic Language Learn More...
Azure Blob Storage is a messaging as a service platform which supports queues and topics.
This library is a wrapper for CFML/ColdFusion apps to be able to interact with Azure Blob Storage via the Java SDK.
Apache License, Version 2.0.
Install into your modules folder using the box
cli to install
box install BlobStorageSDK
You are responsible for loading the jars into your application. If
this is a CFML web app, you can add this to your Application.cfc
this.javaSettings = {
loadPaths = directorylist( expandPath( '/modules/BlobStorageSDK/lib' ), true, 'array', '*jar' ),
loadColdFusionClassPath = true,
reloadOnChange = false
};
Or if you are using this module from the CLI, you can load the jars in a task runner or custom command in CommandBox prior to using the module like so:
classLoad( 'expandPath( '/BlobStorageSDK/lib' )' );
This module wraps and simplifies the Java SDK. There are only a few CFCs for you to worry about, and while not 100% of the Java SDK functionality is exposed, all the major functions are here.
There is only one CFC you need to know about:
This is a singleton that represents the main Blob Storage client. This is a singleton which is your entry point to all blob storage operations. It doesn't contain any open connections and doesn't need to be shutdown. It can be re-used across threads.
wirebox.getInstance( 'Client@BlobStorageSDK' );
or
property name='client' inject='Client@BlobStorageSDK';
You can configure the client with the following module settings
moduleSettings = {
endpoint : '',
containerName : '',
overwrite : false,
maxDownloadRetries : 0,
credentials : {
type : 'connectionString', // connectionString, default, ClientSecret, ClientCertificate
connectionString : '',
authorityHost : '',
tenantId : '',
clientId : '',
clientSecret : '',
pemCertificatePath : '',
pfxCertificatePath : '',
certificatePassword : '',
maxRetry : 3,
tokenRefreshOffsetSeconds : 0,
enablePersistentCache : false
}
};
The following methods help manage containers. The container name is required for all of these methods.
Creates a new blob container with the specified name, ignoring if it already exists.
client.createContainer( 'test-container-123456789' );
Deletes the specified blob container, ignoring if it doesn't exist or is being deleted.
client.deleteContainer( 'test-container-123456789' );
Checks if a container exists, returning true
if it does,
false
otherwise.
var exists = client.containerExists( 'test-container-123456789' );
writeOutput( exists ? 'Container exists' : 'Container does not exist' );
Lists all containers in the storage account, returning an array of container objects with name, metadata, and properties.
var containers = client.listContainers();
writeDump( containers );
The following methods help manage blobs inside an existing container.
If you set a containerName
in your module config, you can
omit it from these calls. Module defaults such as
timeoutSeconds
will also default to your module settings.
Uploads a text string to a blob, replacing it if it exists when
overwrite
is true
.
client.uploadBlob(
containerName = 'test-container-123456789',
blobName = 'test-blob-text.txt',
content = 'This is a test blob content.',
overwrite = true
);
Uploads binary data, such as an image, to a blob, replacing it if it exists.
client.uploadBlob(
containerName = 'test-container-123456789',
blobName = 'test-blob-image.jpg',
content = fileReadBinary( expandPath( '/tests/resources/blhat.jpg' ) ),
overwrite = true
);
Uploads a file from disk to a blob, streaming it to handle large files efficiently.
client.uploadBlobFromFile(
containerName = 'test-container-123456789',
blobName = 'my-stored-file.txt',
filePath = '/tests/resources/myFile.txt',
overwrite = true
);
Downloads a text blob to memory, converting the binary result to a string.
var content = client.downloadBlob(
containerName = 'test-container-123456789',
blobName = 'test-blob-text.txt'
);
writeOutput( toString( content ) ); // Outputs: This is a test blob content.
Downloads a binary blob, such as an image, to memory as raw binary data.
var content = client.downloadBlob(
containerName = 'test-container-123456789',
blobName = 'test-blob-image.jpg'
);
Downloads a blob to a local file, streaming it to disk with metadata returned.
var blobProperties = client.downloadBlobToFile(
containerName = 'test-container-123456789',
blobName = 'test-blob-text.txt',
filePath = '/tests/resources/downloaded_blob.txt',
timeoutSeconds = 30,
maxDownloadRetries = 3,
overwrite = true
);
writeDump( blobProperties );
Checks if a blob exists in the specified container, returning
true
or false
.
var exists = client.blobExists(
containerName = 'test-container-123456789',
blobName = 'test-blob-text.txt'
);
writeOutput( exists ? 'Blob exists' : 'Blob does not exist' );
Deletes a blob from the specified container, ignoring if it doesn't exist.
client.deleteBlob(
containerName = 'test-container-123456789',
blobName = 'test-blob-text.txt'
);
Lists all blobs in the specified container, returning an array of blob objects.
var blobs = client.listBlobs(
containerName = 'test-container-123456789'
);
writeDump( blobs );
Lists blobs in the specified container that start with a given prefix, such as a subfolder.
var blobs = client.listBlobs(
containerName = 'test-container-123456789',
prefix = 'subfolder/'
);
writeDump( blobs );
$
box install blobstoragesdk