Search in sources :

Example 11 with Blob

use of org.jclouds.blobstore.domain.Blob 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)

Example 12 with Blob

use of org.jclouds.blobstore.domain.Blob in project jackrabbit-oak by apache.

the class CloudBlobStore method readBlockFromBackend.

/**
     * Reads the data from the actual cloud service.
     */
@Override
protected byte[] readBlockFromBackend(BlockId blockId) throws Exception {
    Preconditions.checkNotNull(context);
    String id = StringUtils.convertBytesToHex(blockId.getDigest());
    byte[] data = cache.get(id);
    if (data == null) {
        Blob cloudBlob = context.getBlobStore().getBlob(cloudContainer, id);
        if (cloudBlob == null) {
            String message = "Did not find block " + id;
            LOG.error(message);
            throw new IOException(message);
        }
        Payload payload = cloudBlob.getPayload();
        try {
            data = ByteStreams.toByteArray(payload.getInput());
            cache.put(id, data);
        } finally {
            payload.close();
        }
    }
    if (blockId.getPos() == 0) {
        return data;
    }
    int len = (int) (data.length - blockId.getPos());
    if (len < 0) {
        return new byte[0];
    }
    byte[] d2 = new byte[len];
    System.arraycopy(data, (int) blockId.getPos(), d2, 0, len);
    return d2;
}
Also used : Blob(org.jclouds.blobstore.domain.Blob) Payload(org.jclouds.io.Payload) IOException(java.io.IOException)

Example 13 with Blob

use of org.jclouds.blobstore.domain.Blob in project qi4j-sdk by Qi4j.

the class JCloudsMapEntityStoreMixin method applyChanges.

@Override
public void applyChanges(MapChanges changes) throws IOException {
    final BlobStore blobStore = storeContext.getBlobStore();
    changes.visitMap(new MapChanger() {

        @Override
        public Writer newEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
            return new StringWriter() {

                @Override
                public void close() throws IOException {
                    super.close();
                    Blob blob = blobStore.blobBuilder(ref.identity()).payload(ByteSource.wrap(toString().getBytes(UTF_8))).build();
                    blobStore.putBlob(container, blob);
                }
            };
        }

        @Override
        public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
            if (!blobStore.blobExists(container, ref.identity())) {
                throw new EntityNotFoundException(ref);
            }
            return new StringWriter() {

                @Override
                public void close() throws IOException {
                    super.close();
                    Blob blob = blobStore.blobBuilder(ref.identity()).payload(ByteSource.wrap(toString().getBytes(UTF_8))).build();
                    blobStore.putBlob(container, blob);
                }
            };
        }

        @Override
        public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
            if (!blobStore.blobExists(container, ref.identity())) {
                throw new EntityNotFoundException(ref);
            }
            blobStore.removeBlob(container, ref.identity());
        }
    });
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) Blob(org.jclouds.blobstore.domain.Blob) StringWriter(java.io.StringWriter) EntityReference(org.qi4j.api.entity.EntityReference) EntityReference.parseEntityReference(org.qi4j.api.entity.EntityReference.parseEntityReference) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) BlobStore(org.jclouds.blobstore.BlobStore) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 14 with Blob

use of org.jclouds.blobstore.domain.Blob in project dhis2-core by dhis2.

the class JCloudsFileResourceContentStore method getFileResourceContent.

// -------------------------------------------------------------------------
// FileResourceContentStore implementation
// -------------------------------------------------------------------------
@Override
public ByteSource getFileResourceContent(String key) {
    final Blob blob = getBlob(key);
    if (blob == null) {
        return null;
    }
    final ByteSource byteSource = new ByteSource() {

        @Override
        public InputStream openStream() {
            try {
                return blob.getPayload().openStream();
            } catch (IOException e) {
                return new NullInputStream(0);
            }
        }
    };
    boolean isEmptyOrFailed;
    try {
        isEmptyOrFailed = byteSource.isEmpty();
    } catch (IOException e) {
        isEmptyOrFailed = true;
    }
    return isEmptyOrFailed ? null : byteSource;
}
Also used : Blob(org.jclouds.blobstore.domain.Blob) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) NullInputStream(org.apache.commons.io.input.NullInputStream)

Example 15 with Blob

use of org.jclouds.blobstore.domain.Blob in project dhis2-core by dhis2.

the class JCloudsFileResourceContentStore method saveFileResourceContent.

@Override
public String saveFileResourceContent(FileResource fileResource, File file) {
    Blob blob = createBlob(fileResource, file);
    if (blob == null) {
        return null;
    }
    putBlob(blob);
    try {
        Files.deleteIfExists(file.toPath());
    } catch (IOException ioe) {
        log.warn(String.format("Temporary file '%s' could not be deleted.", file.toPath()), ioe);
    }
    log.debug(String.format("File resource saved with key: %s", fileResource.getStorageKey()));
    return fileResource.getStorageKey();
}
Also used : Blob(org.jclouds.blobstore.domain.Blob) IOException(java.io.IOException)

Aggregations

Blob (org.jclouds.blobstore.domain.Blob)15 IOException (java.io.IOException)6 BlobStore (org.jclouds.blobstore.BlobStore)4 BlobStoreContext (org.jclouds.blobstore.BlobStoreContext)3 BlobStoreContextFactory (org.jclouds.blobstore.BlobStoreContextFactory)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 Properties (java.util.Properties)2 Payload (org.jclouds.io.Payload)2 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)2 ByteSource (com.google.common.io.ByteSource)1 File (java.io.File)1 ObjectOutputStream (java.io.ObjectOutputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 NullInputStream (org.apache.commons.io.input.NullInputStream)1 Configuration (org.apache.hadoop.conf.Configuration)1