Search in sources :

Example 1 with PageRange

use of com.microsoft.azure.storage.blob.PageRange in project camel by apache.

the class BlobServiceUtil method doGetBlob.

private static void doGetBlob(CloudBlob client, Exchange exchange, BlobServiceConfiguration cfg) throws Exception {
    BlobServiceUtil.configureCloudBlobForRead(client, cfg);
    BlobServiceRequestOptions opts = getRequestOptions(exchange);
    LOG.trace("Getting a blob [{}] from exchange [{}]...", cfg.getBlobName(), exchange);
    OutputStream os = exchange.getIn().getBody(OutputStream.class);
    if (os == null) {
        String fileDir = cfg.getFileDir();
        if (fileDir != null) {
            File file = new File(fileDir, getBlobFileName(cfg));
            ExchangeUtil.getMessageForResponse(exchange).setBody(file);
            os = new FileOutputStream(file);
        }
    }
    try {
        if (os == null) {
            // Let the producers like file: deal with it
            InputStream blobStream = client.openInputStream(opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext());
            exchange.getIn().setBody(blobStream);
            exchange.getIn().setHeader(Exchange.FILE_NAME, getBlobFileName(cfg));
        } else {
            Long blobOffset = cfg.getBlobOffset();
            Long blobDataLength = cfg.getDataLength();
            if (client instanceof CloudPageBlob) {
                PageRange range = exchange.getIn().getHeader(BlobServiceConstants.PAGE_BLOB_RANGE, PageRange.class);
                if (range != null) {
                    blobOffset = range.getStartOffset();
                    blobDataLength = range.getEndOffset() - range.getStartOffset();
                }
            }
            client.downloadRange(blobOffset, blobDataLength, os, opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext());
        }
    } finally {
        if (os != null && cfg.isCloseStreamAfterRead()) {
            os.close();
        }
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) CloudPageBlob(com.microsoft.azure.storage.blob.CloudPageBlob) PageRange(com.microsoft.azure.storage.blob.PageRange)

Example 2 with PageRange

use of com.microsoft.azure.storage.blob.PageRange in project hadoop by apache.

the class PageBlobInputStream method getPageBlobDataSize.

/**
   * Helper method to extract the actual data size of a page blob.
   * This typically involves 2 service requests (one for page ranges, another
   * for the last page's data).
   *
   * @param blob The blob to get the size from.
   * @param opContext The operation context to use for the requests.
   * @return The total data size of the blob in bytes.
   * @throws IOException If the format is corrupt.
   * @throws StorageException If anything goes wrong in the requests.
   */
public static long getPageBlobDataSize(CloudPageBlobWrapper blob, OperationContext opContext) throws IOException, StorageException {
    // Get the page ranges for the blob. There should be one range starting
    // at byte 0, but we tolerate (and ignore) ranges after the first one.
    ArrayList<PageRange> pageRanges = blob.downloadPageRanges(new BlobRequestOptions(), opContext);
    if (pageRanges.size() == 0) {
        return 0;
    }
    if (pageRanges.get(0).getStartOffset() != 0) {
        // starting at byte 0.
        throw badStartRangeException(blob, pageRanges.get(0));
    }
    long totalRawBlobSize = pageRanges.get(0).getEndOffset() + 1;
    // Get the last page.
    long lastPageStart = totalRawBlobSize - PAGE_SIZE;
    ByteArrayOutputStream baos = new ByteArrayOutputStream(PageBlobFormatHelpers.PAGE_SIZE);
    blob.downloadRange(lastPageStart, PAGE_SIZE, baos, new BlobRequestOptions(), opContext);
    byte[] lastPage = baos.toByteArray();
    short lastPageSize = getPageSize(blob, lastPage, 0);
    long totalNumberOfPages = totalRawBlobSize / PAGE_SIZE;
    return (totalNumberOfPages - 1) * PAGE_DATA_SIZE + lastPageSize;
}
Also used : BlobRequestOptions(com.microsoft.azure.storage.blob.BlobRequestOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PageRange(com.microsoft.azure.storage.blob.PageRange)

Example 3 with PageRange

use of com.microsoft.azure.storage.blob.PageRange in project camel by apache.

the class BlobServiceProducer method clearPageBlob.

private void clearPageBlob(Exchange exchange) throws Exception {
    LOG.trace("Clearing a page blob [{}] from exchange [{}]...", getConfiguration().getBlobName(), exchange);
    CloudPageBlob client = BlobServiceUtil.createPageBlobClient(getConfiguration());
    BlobServiceRequestOptions opts = BlobServiceUtil.getRequestOptions(exchange);
    Long blobOffset = getConfiguration().getBlobOffset();
    Long blobDataLength = getConfiguration().getDataLength();
    PageRange range = exchange.getIn().getHeader(BlobServiceConstants.PAGE_BLOB_RANGE, PageRange.class);
    if (range != null) {
        blobOffset = range.getStartOffset();
        blobDataLength = range.getEndOffset() - range.getStartOffset();
    }
    if (blobDataLength == null) {
        blobDataLength = blobOffset == 0 ? getPageBlobSize(exchange) : 512L;
    }
    client.clearPages(blobOffset, blobDataLength, opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext());
}
Also used : CloudPageBlob(com.microsoft.azure.storage.blob.CloudPageBlob) PageRange(com.microsoft.azure.storage.blob.PageRange)

Example 4 with PageRange

use of com.microsoft.azure.storage.blob.PageRange in project camel by apache.

the class BlobServiceProducer method getPageBlobRanges.

private void getPageBlobRanges(Exchange exchange) throws Exception {
    CloudPageBlob client = BlobServiceUtil.createPageBlobClient(getConfiguration());
    BlobServiceUtil.configureCloudBlobForRead(client, getConfiguration());
    BlobServiceRequestOptions opts = BlobServiceUtil.getRequestOptions(exchange);
    LOG.trace("Getting the page blob ranges [{}] from exchange [{}]...", getConfiguration().getBlobName(), exchange);
    List<PageRange> ranges = client.downloadPageRanges(opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext());
    ExchangeUtil.getMessageForResponse(exchange).setBody(ranges);
}
Also used : CloudPageBlob(com.microsoft.azure.storage.blob.CloudPageBlob) PageRange(com.microsoft.azure.storage.blob.PageRange)

Example 5 with PageRange

use of com.microsoft.azure.storage.blob.PageRange in project camel by apache.

the class BlobServiceProducer method doUpdatePageBlob.

private void doUpdatePageBlob(CloudPageBlob client, InputStream is, BlobServiceRequestOptions opts, Exchange exchange) throws Exception {
    Long blobOffset = getConfiguration().getBlobOffset();
    Long blobDataLength = getConfiguration().getDataLength();
    PageRange range = exchange.getIn().getHeader(BlobServiceConstants.PAGE_BLOB_RANGE, PageRange.class);
    if (range != null) {
        blobOffset = range.getStartOffset();
        blobDataLength = range.getEndOffset() - range.getStartOffset();
    }
    if (blobDataLength == null) {
        blobDataLength = (long) is.available();
    }
    try {
        client.uploadPages(is, blobOffset, blobDataLength, opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext());
    } finally {
        closeInputStreamIfNeeded(is);
    }
}
Also used : PageRange(com.microsoft.azure.storage.blob.PageRange)

Aggregations

PageRange (com.microsoft.azure.storage.blob.PageRange)5 CloudPageBlob (com.microsoft.azure.storage.blob.CloudPageBlob)3 BlobRequestOptions (com.microsoft.azure.storage.blob.BlobRequestOptions)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1