Load document from Azure Blob Storage

Following example demonstrates how to render document from Azure Blob Storage.

    String blobName = "sample.docx";

    Viewer viewer = new Viewer(downloadFile(blobName));

    HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources();

    viewer.view(viewOptions);
    viewer.close();
    public InputStream downloadFile(String blobName) {
        CloudBlobContainer container = getContainer();
        CloudBlob blob = container.getBlobReference(blobName);
        ByteArrayOutputStream memoryStream = new ByteArrayOutputStream();
        blob.downloadToStream(memoryStream);
        return new ByteArrayInputStream(memoryStream.toByteArray());
    }

    private CloudBlobContainer getContainer() {
        String accountName = "***";
        String accountKey = "***";
        String endpoint = "https://{accountName}.blob.core.windows.java/";
        String containerName = "***";
        StorageCredentials storageCredentials = new StorageCredentials(accountName, accountKey);
        CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentials, new URI(endpoint), null, null, null);
        CloudBlobClient cloudBlobClient = cloudStorageAccount.createCloudBlobClient();
        CloudBlobContainer container = cloudBlobClient.getContainerReference(containerName);
        container.createIfNotExists();
        return container;
    }

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.