Search in sources :

Example 11 with BlobStoreContext

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

the class MainApp method main.

public static void main(String[] args) throws IOException {
    if (args.length < PARAMETERS)
        throw new IllegalArgumentException(INVALID_SYNTAX);
    // Args
    String provider = args[0];
    if (!Iterables.contains(BlobStoreUtils.getSupportedProviders(), provider))
        throw new IllegalArgumentException("provider " + provider + " not in supported list: " + BlobStoreUtils.getSupportedProviders());
    String identity = args[1];
    String credential = args[2];
    String fileName = args[3];
    String containerName = args[4];
    String objectName = args[5];
    boolean plainhttp = args.length >= 7 && "plainhttp".equals(args[6]);
    String threadcount = args.length >= 8 ? args[7] : null;
    // 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, MODULES, overrides);
    try {
        long start = System.currentTimeMillis();
        // Create Container
        // it can be changed to sync
        AsyncBlobStore blobStore = context.getAsyncBlobStore();
        // BlobStore
        ListenableFuture<Boolean> future = blobStore.createContainerInLocation(null, containerName);
        future.get();
        File input = new File(fileName);
        long length = input.length();
        // Add a Blob
        Blob blob = blobStore.blobBuilder(objectName).payload(input).contentType(MediaType.APPLICATION_OCTET_STREAM).contentDisposition(objectName).build();
        // Upload a file
        ListenableFuture<String> futureETag = blobStore.putBlob(containerName, blob, multipart());
        // asynchronously wait for the upload
        String eTag = futureETag.get();
        printSpeed("Sucessfully uploaded eTag(" + eTag + ")", start, length);
    } catch (InterruptedException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } catch (ExecutionException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        // Close connecton
        context.close();
        System.exit(0);
    }
}
Also used : AsyncBlobStore(org.jclouds.blobstore.AsyncBlobStore) Blob(org.jclouds.blobstore.domain.Blob) Properties(java.util.Properties) BlobStoreContextFactory(org.jclouds.blobstore.BlobStoreContextFactory) BlobStoreContext(org.jclouds.blobstore.BlobStoreContext) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Aggregations

BlobStoreContext (org.jclouds.blobstore.BlobStoreContext)11 Properties (java.util.Properties)4 Blob (org.jclouds.blobstore.domain.Blob)3 BlobStore (org.jclouds.blobstore.BlobStore)2 BlobStoreContextFactory (org.jclouds.blobstore.BlobStoreContextFactory)2 File (java.io.File)1 InputStream (java.io.InputStream)1 ExecutionException (java.util.concurrent.ExecutionException)1 PostConstruct (javax.annotation.PostConstruct)1 Configuration (org.apache.hadoop.conf.Configuration)1 Path (org.apache.hadoop.fs.Path)1 AtmosClient (org.jclouds.atmos.AtmosClient)1 AzureBlobAsyncClient (org.jclouds.azureblob.AzureBlobAsyncClient)1 AzureBlobClient (org.jclouds.azureblob.AzureBlobClient)1 AsyncBlobStore (org.jclouds.blobstore.AsyncBlobStore)1 StorageMetadata (org.jclouds.blobstore.domain.StorageMetadata)1 Credentials (org.jclouds.domain.Credentials)1 Location (org.jclouds.domain.Location)1 LocationBuilder (org.jclouds.domain.LocationBuilder)1 HdfsPayload (org.jclouds.examples.blobstore.hdfs.io.payloads.HdfsPayload)1