Search in sources :

Example 6 with CloudBlob

use of com.microsoft.azure.storage.blob.CloudBlob in project jackrabbit-oak by apache.

the class AzureBlobStoreBackend method getAllMetadataRecords.

@Override
public List<DataRecord> getAllMetadataRecords(String prefix) {
    if (null == prefix) {
        throw new NullPointerException("prefix");
    }
    long start = System.currentTimeMillis();
    final List<DataRecord> records = Lists.newArrayList();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        CloudBlobDirectory metaDir = getAzureContainer().getDirectoryReference(META_DIR_NAME);
        for (ListBlobItem item : metaDir.listBlobs(prefix)) {
            if (item instanceof CloudBlob) {
                CloudBlob blob = (CloudBlob) item;
                records.add(new AzureBlobStoreDataRecord(this, connectionString, containerName, new DataIdentifier(stripMetaKeyPrefix(blob.getName())), blob.getProperties().getLastModified().getTime(), blob.getProperties().getLength(), true));
            }
        }
        LOG.debug("Metadata records read. recordsRead={} metadataFolder={} duration={}", records.size(), prefix, (System.currentTimeMillis() - start));
    } catch (StorageException e) {
        LOG.info("Error reading all metadata records. metadataFolder={}", prefix, e);
    } catch (DataStoreException | URISyntaxException e) {
        LOG.debug("Error reading all metadata records. metadataFolder={}", prefix, e);
    } finally {
        if (null != contextClassLoader) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    return records;
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) CloudBlobDirectory(com.microsoft.azure.storage.blob.CloudBlobDirectory) URISyntaxException(java.net.URISyntaxException) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) AbstractDataRecord(org.apache.jackrabbit.oak.spi.blob.AbstractDataRecord) DataRecord(org.apache.jackrabbit.core.data.DataRecord) StorageException(com.microsoft.azure.storage.StorageException)

Example 7 with CloudBlob

use of com.microsoft.azure.storage.blob.CloudBlob in project nifi by apache.

the class FetchAzureBlobStorage method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final long startNanos = System.nanoTime();
    String containerName = context.getProperty(AzureStorageUtils.CONTAINER).evaluateAttributeExpressions(flowFile).getValue();
    String blobPath = context.getProperty(BLOB).evaluateAttributeExpressions(flowFile).getValue();
    AtomicReference<Exception> storedException = new AtomicReference<>();
    try {
        CloudBlobClient blobClient = AzureStorageUtils.createCloudBlobClient(context, getLogger(), flowFile);
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        final Map<String, String> attributes = new HashMap<>();
        final CloudBlob blob = container.getBlockBlobReference(blobPath);
        // TODO - we may be able do fancier things with ranges and
        // distribution of download over threads, investigate
        flowFile = session.write(flowFile, os -> {
            try {
                blob.download(os);
            } catch (StorageException e) {
                storedException.set(e);
                throw new IOException(e);
            }
        });
        long length = blob.getProperties().getLength();
        attributes.put("azure.length", String.valueOf(length));
        if (!attributes.isEmpty()) {
            flowFile = session.putAllAttributes(flowFile, attributes);
        }
        session.transfer(flowFile, REL_SUCCESS);
        final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
        session.getProvenanceReporter().fetch(flowFile, blob.getSnapshotQualifiedUri().toString(), transferMillis);
    } catch (IllegalArgumentException | URISyntaxException | StorageException | ProcessException e) {
        if (e instanceof ProcessException && storedException.get() == null) {
            throw (ProcessException) e;
        } else {
            Exception failureException = Optional.ofNullable(storedException.get()).orElse(e);
            getLogger().error("Failure to fetch Azure blob {}", new Object[] { blobPath }, failureException);
            flowFile = session.penalize(flowFile);
            session.transfer(flowFile, REL_FAILURE);
        }
    }
}
Also used : CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) FlowFile(org.apache.nifi.flowfile.FlowFile) URISyntaxException(java.net.URISyntaxException) CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) ProcessContext(org.apache.nifi.processor.ProcessContext) IOException(java.io.IOException) HashMap(java.util.HashMap) ProcessSession(org.apache.nifi.processor.ProcessSession) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) SeeAlso(org.apache.nifi.annotation.documentation.SeeAlso) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProcessException(org.apache.nifi.processor.exception.ProcessException) TimeUnit(java.util.concurrent.TimeUnit) StorageException(com.microsoft.azure.storage.StorageException) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) Optional(java.util.Optional) Tags(org.apache.nifi.annotation.documentation.Tags) AbstractAzureBlobProcessor(org.apache.nifi.processors.azure.AbstractAzureBlobProcessor) AzureStorageUtils(org.apache.nifi.processors.azure.storage.utils.AzureStorageUtils) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) FlowFile(org.apache.nifi.flowfile.FlowFile) CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ProcessException(org.apache.nifi.processor.exception.ProcessException) StorageException(com.microsoft.azure.storage.StorageException) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) ProcessException(org.apache.nifi.processor.exception.ProcessException) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) StorageException(com.microsoft.azure.storage.StorageException)

Example 8 with CloudBlob

use of com.microsoft.azure.storage.blob.CloudBlob in project nifi by apache.

the class ListAzureBlobStorage method performListing.

@Override
protected List<BlobInfo> performListing(final ProcessContext context, final Long minTimestamp) throws IOException {
    String containerName = context.getProperty(AzureStorageUtils.CONTAINER).evaluateAttributeExpressions().getValue();
    String prefix = context.getProperty(PROP_PREFIX).evaluateAttributeExpressions().getValue();
    if (prefix == null) {
        prefix = "";
    }
    final List<BlobInfo> listing = new ArrayList<>();
    try {
        CloudBlobClient blobClient = AzureStorageUtils.createCloudBlobClient(context, getLogger(), null);
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        for (ListBlobItem blob : container.listBlobs(prefix, true, EnumSet.of(BlobListingDetails.METADATA), null, null)) {
            if (blob instanceof CloudBlob) {
                CloudBlob cloudBlob = (CloudBlob) blob;
                BlobProperties properties = cloudBlob.getProperties();
                StorageUri uri = cloudBlob.getSnapshotQualifiedStorageUri();
                Builder builder = new BlobInfo.Builder().primaryUri(uri.getPrimaryUri().toString()).blobName(cloudBlob.getName()).containerName(containerName).contentType(properties.getContentType()).contentLanguage(properties.getContentLanguage()).etag(properties.getEtag()).lastModifiedTime(properties.getLastModified().getTime()).length(properties.getLength());
                if (uri.getSecondaryUri() != null) {
                    builder.secondaryUri(uri.getSecondaryUri().toString());
                }
                if (blob instanceof CloudBlockBlob) {
                    builder.blobType(AzureStorageUtils.BLOCK);
                } else {
                    builder.blobType(AzureStorageUtils.PAGE);
                }
                listing.add(builder.build());
            }
        }
    } catch (Throwable t) {
        throw new IOException(ExceptionUtils.getRootCause(t));
    }
    return listing;
}
Also used : CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) Builder(org.apache.nifi.processors.azure.storage.utils.BlobInfo.Builder) ArrayList(java.util.ArrayList) StorageUri(com.microsoft.azure.storage.StorageUri) BlobInfo(org.apache.nifi.processors.azure.storage.utils.BlobInfo) IOException(java.io.IOException) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) BlobProperties(com.microsoft.azure.storage.blob.BlobProperties) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer)

Example 9 with CloudBlob

use of com.microsoft.azure.storage.blob.CloudBlob in project nifi by apache.

the class PutAzureBlobStorage method onTrigger.

public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final long startNanos = System.nanoTime();
    String containerName = context.getProperty(AzureStorageUtils.CONTAINER).evaluateAttributeExpressions(flowFile).getValue();
    String blobPath = context.getProperty(BLOB).evaluateAttributeExpressions(flowFile).getValue();
    AtomicReference<Exception> storedException = new AtomicReference<>();
    try {
        CloudBlobClient blobClient = AzureStorageUtils.createCloudBlobClient(context, getLogger(), flowFile);
        CloudBlobContainer container = blobClient.getContainerReference(containerName);
        CloudBlob blob = container.getBlockBlobReference(blobPath);
        final Map<String, String> attributes = new HashMap<>();
        long length = flowFile.getSize();
        session.read(flowFile, rawIn -> {
            InputStream in = rawIn;
            if (!(in instanceof BufferedInputStream)) {
                // do not double-wrap
                in = new BufferedInputStream(rawIn);
            }
            try {
                blob.upload(in, length);
                BlobProperties properties = blob.getProperties();
                attributes.put("azure.container", containerName);
                attributes.put("azure.primaryUri", blob.getSnapshotQualifiedUri().toString());
                attributes.put("azure.etag", properties.getEtag());
                attributes.put("azure.length", String.valueOf(length));
                attributes.put("azure.timestamp", String.valueOf(properties.getLastModified()));
            } catch (StorageException | URISyntaxException e) {
                storedException.set(e);
                throw new IOException(e);
            }
        });
        if (!attributes.isEmpty()) {
            flowFile = session.putAllAttributes(flowFile, attributes);
        }
        session.transfer(flowFile, REL_SUCCESS);
        final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
        session.getProvenanceReporter().send(flowFile, blob.getSnapshotQualifiedUri().toString(), transferMillis);
    } catch (IllegalArgumentException | URISyntaxException | StorageException | ProcessException e) {
        if (e instanceof ProcessException && storedException.get() == null) {
            throw (ProcessException) e;
        } else {
            Exception failureException = Optional.ofNullable(storedException.get()).orElse(e);
            getLogger().error("Failed to put Azure blob {}", new Object[] { blobPath }, failureException);
            flowFile = session.penalize(flowFile);
            session.transfer(flowFile, REL_FAILURE);
        }
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) CloudBlobClient(com.microsoft.azure.storage.blob.CloudBlobClient) HashMap(java.util.HashMap) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ProcessException(org.apache.nifi.processor.exception.ProcessException) StorageException(com.microsoft.azure.storage.StorageException) IOException(java.io.IOException) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) ProcessException(org.apache.nifi.processor.exception.ProcessException) BufferedInputStream(java.io.BufferedInputStream) BlobProperties(com.microsoft.azure.storage.blob.BlobProperties) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer) StorageException(com.microsoft.azure.storage.StorageException)

Example 10 with CloudBlob

use of com.microsoft.azure.storage.blob.CloudBlob in project nifi by apache.

the class AbstractAzureBlobStorageIT method uploadBlob.

protected void uploadBlob(String containerName, String filePath) throws URISyntaxException, StorageException, InvalidKeyException, IOException {
    CloudBlobContainer container = AzureTestUtil.getContainer(containerName);
    CloudBlob blob = container.getBlockBlobReference(SAMPLE_BLOB_NAME);
    blob.uploadFromFile(filePath);
}
Also used : CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) CloudBlobContainer(com.microsoft.azure.storage.blob.CloudBlobContainer)

Aggregations

CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)24 StorageException (com.microsoft.azure.storage.StorageException)12 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)12 ListBlobItem (com.microsoft.azure.storage.blob.ListBlobItem)11 URISyntaxException (java.net.URISyntaxException)9 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)3 BlobProperties (com.microsoft.azure.storage.blob.BlobProperties)3 CloudBlobDirectory (com.microsoft.azure.storage.blob.CloudBlobDirectory)3 InputStream (java.io.InputStream)3 FlowFile (org.apache.nifi.flowfile.FlowFile)3 StorageCredentials (com.microsoft.azure.storage.StorageCredentials)2 BlobListingDetails (com.microsoft.azure.storage.blob.BlobListingDetails)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2