Search in sources :

Example 11 with BlobStore

use of org.jclouds.blobstore.BlobStore in project legacy-jclouds-examples by jclouds.

the class MainApp method upload.

/**
 * @param provider
 * @param identity
 * @param credential
 * @param hdfsUrl
 * @param containerName
 * @param objectName
 * @param plainhttp
 * @param threadcount
 * @throws IOException
 */
private void upload(String provider, String identity, String credential, String hdfsUrl, String containerName, String objectName, boolean plainhttp, String threadcount) throws IOException {
    // Init
    Properties overrides = new Properties();
    if (plainhttp)
        // default is https
        overrides.putAll(PLAIN_HTTP_ENDPOINTS);
    if (threadcount != null)
        // without setting,
        overrides.setProperty("jclouds.mpu.parallel.degree", threadcount);
    // default is 4 threads
    overrides.setProperty(provider + ".identity", identity);
    overrides.setProperty(provider + ".credential", credential);
    BlobStoreContext context = new BlobStoreContextFactory().createContext(provider, HDFS_MODULES, overrides);
    try {
        long start = System.currentTimeMillis();
        Configuration conf = getConf();
        if (conf == null) {
            conf = new Configuration();
            setConf(conf);
        }
        // Create Container
        // it can be changed to sync
        BlobStore blobStore = context.getBlobStore();
        // BlobStore
        blobStore.createContainerInLocation(null, containerName);
        Blob blob = blobStore.blobBuilder(objectName).payload(new HdfsPayload(new Path(hdfsUrl), conf)).contentType(MediaType.APPLICATION_OCTET_STREAM).contentDisposition(objectName).build();
        long length = blob.getPayload().getContentMetadata().getContentLength();
        blobStore.putBlob(containerName, blob, multipart());
        printSpeed("Sucessfully uploaded", start, length);
    } finally {
        // Close connection
        context.close();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Blob(org.jclouds.blobstore.domain.Blob) BlobStoreContextFactory(org.jclouds.blobstore.BlobStoreContextFactory) Configuration(org.apache.hadoop.conf.Configuration) HdfsPayload(org.jclouds.examples.blobstore.hdfs.io.payloads.HdfsPayload) BlobStoreContext(org.jclouds.blobstore.BlobStoreContext) Properties(java.util.Properties) BlobStore(org.jclouds.blobstore.BlobStore)

Example 12 with BlobStore

use of org.jclouds.blobstore.BlobStore in project legacy-jclouds-examples by jclouds.

the class BlobStoreServiceImpl method write.

public void write(String bucket, String blobName, Object object) {
    context = new BlobStoreContextFactory().createContext(provider, accessKeyId, secretKey);
    if (context != null) {
        BlobStore blobStore = context.getBlobStore();
        Blob blob = blobStore.blobBuilder(blobName).build();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            blob.setPayload(baos.toByteArray());
            blobStore.putBlob(bucket, blob);
        } catch (IOException e) {
            logger.error("Error while writing blob", e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                }
            }
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                }
            }
        }
    } else
        logger.warn("Blob store context is null.");
}
Also used : Blob(org.jclouds.blobstore.domain.Blob) BlobStoreContextFactory(org.jclouds.blobstore.BlobStoreContextFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) BlobStore(org.jclouds.blobstore.BlobStore)

Example 13 with BlobStore

use of org.jclouds.blobstore.BlobStore in project artifact-manager-s3-plugin by jenkinsci.

the class JCloudsArtifactManager method clearAllStashes.

@Override
public void clearAllStashes(TaskListener listener) throws IOException, InterruptedException {
    String prefix = getBlobPath("stashes/", PREFIX, key, id);
    BlobStore blobStore = getContext(BLOB_CONTAINER).getBlobStore();
    Iterator<StorageMetadata> it = new JCloudsBlobStore.PageSetIterable(blobStore, BLOB_CONTAINER, ListContainerOptions.Builder.prefix(prefix).recursive());
    while (it.hasNext()) {
        StorageMetadata sm = it.next();
        String path = sm.getName();
        assert path.startsWith(prefix);
        LOGGER.fine("deleting " + path);
        blobStore.removeBlob(BLOB_CONTAINER, path);
    }
// TODO print some summary to listener
}
Also used : StorageMetadata(org.jclouds.blobstore.domain.StorageMetadata) BlobStore(org.jclouds.blobstore.BlobStore)

Example 14 with BlobStore

use of org.jclouds.blobstore.BlobStore in project wildfly-camel by wildfly-extras.

the class JCloudsBlobStoreIntegrationTest method testBlobStoreProducer.

@Test
public void testBlobStoreProducer() throws Exception {
    BlobStore blobStore = getBlobStore();
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").setHeader(JcloudsConstants.BLOB_NAME, constant(BLOB_NAME_WITH_DIR)).setHeader(JcloudsConstants.CONTAINER_NAME, constant(CONTAINER_NAME_WITH_DIR)).to("jclouds:blobstore:transient");
        }
    });
    List<BlobStore> blobStores = new ArrayList<>();
    blobStores.add(blobStore);
    JcloudsComponent jclouds = camelctx.getComponent("jclouds", JcloudsComponent.class);
    jclouds.setBlobStores(blobStores);
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        String result = template.requestBody("direct:start", "Hello Kermit", String.class);
        Assert.assertEquals("Hello Kermit", result);
    } finally {
        camelctx.stop();
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) ArrayList(java.util.ArrayList) BlobStore(org.jclouds.blobstore.BlobStore) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) JcloudsComponent(org.apache.camel.component.jclouds.JcloudsComponent) Test(org.junit.Test)

Example 15 with BlobStore

use of org.jclouds.blobstore.BlobStore in project legacy-jclouds-examples by jclouds.

the class BlobStoreServiceImpl method read.

public Object read(String bucket, String blobName) {
    Object result = null;
    ObjectInputStream ois = null;
    context = new BlobStoreContextFactory().createContext(provider, accessKeyId, secretKey);
    if (context != null) {
        BlobStore blobStore = context.getBlobStore();
        blobStore.createContainerInLocation(null, bucket);
        InputStream is = blobStore.getBlob(bucket, blobName).getPayload().getInput();
        try {
            ois = new ObjectInputStream(is);
            result = ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    } else
        logger.warn("Blob store context is null.");
    return result;
}
Also used : BlobStoreContextFactory(org.jclouds.blobstore.BlobStoreContextFactory) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) BlobStore(org.jclouds.blobstore.BlobStore) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

BlobStore (org.jclouds.blobstore.BlobStore)15 BlobStoreContext (org.jclouds.blobstore.BlobStoreContext)5 Blob (org.jclouds.blobstore.domain.Blob)5 StorageMetadata (org.jclouds.blobstore.domain.StorageMetadata)4 ArrayList (java.util.ArrayList)3 CamelContext (org.apache.camel.CamelContext)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 JcloudsComponent (org.apache.camel.component.jclouds.JcloudsComponent)3 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 BlobStoreContextFactory (org.jclouds.blobstore.BlobStoreContextFactory)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 StringPayload (org.jclouds.io.payloads.StringPayload)2 AbortException (hudson.AbortException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Properties (java.util.Properties)1