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);
}
}
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;
}
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());
}
});
}
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;
}
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();
}
Aggregations