use of org.jclouds.blobstore.domain.Blob in project legacy-jclouds-examples by jclouds.
the class UploadObjects method uploadObjectFromStringWithMetadata.
/**
* Upload an object from a String with metadata using the BlobStore API.
*/
private void uploadObjectFromStringWithMetadata() {
System.out.println("Upload Object From String With Metadata");
String filename = "uploadObjectFromStringWithMetadata.txt";
Map<String, String> userMetadata = new HashMap<String, String>();
userMetadata.put("key1", "value1");
Blob blob = storage.blobBuilder(filename).payload("uploadObjectFromStringWithMetadata").userMetadata(userMetadata).build();
storage.putBlob(Constants.CONTAINER, blob);
System.out.println(" " + filename);
}
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 qi4j-sdk by Qi4j.
the class JCloudsMapEntityStoreMixin method applyChanges.
@Override
public void applyChanges(MapChanges changes) throws IOException {
final BlobMap blobMap = storeContext.createBlobMap(container);
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 = blobMap.blobBuilder().payload(toString()).build();
blobMap.put(ref.identity(), blob);
}
};
}
@Override
public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
if (!blobMap.containsKey(ref.identity())) {
throw new EntityNotFoundException(ref);
}
return new StringWriter() {
@Override
public void close() throws IOException {
super.close();
Blob blob = blobMap.blobBuilder().payload(toString()).build();
blobMap.put(ref.identity(), blob);
}
};
}
@Override
public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
if (!blobMap.containsKey(ref.identity())) {
throw new EntityNotFoundException(ref);
}
blobMap.remove(ref.identity());
}
});
}
use of org.jclouds.blobstore.domain.Blob in project dhis2-core by dhis2.
the class JCloudsAppStorageService method discoverInstalledApps.
@Override
public Map<String, App> discoverInstalledApps() {
Map<String, App> appMap = new HashMap<>();
List<App> appList = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
log.info("Starting JClouds discovery");
for (StorageMetadata resource : blobStore.list(config.container, prefix(APPS_DIR + "/").delimiter("/"))) {
log.info("Found potential app: " + resource.getName());
// Found potential app
Blob manifest = blobStore.getBlob(config.container, resource.getName() + "manifest.webapp");
if (manifest == null) {
log.warn("Could not find manifest file of " + resource.getName());
continue;
}
try {
InputStream inputStream = manifest.getPayload().openStream();
App app = mapper.readValue(inputStream, App.class);
inputStream.close();
app.setAppStorageSource(AppStorageSource.JCLOUDS);
app.setFolderName(resource.getName());
appList.add(app);
} catch (IOException ex) {
log.error("Could not read manifest file of " + resource.getName(), ex);
log.error(DebugUtils.getStackTrace(ex));
}
}
appList.forEach(app -> {
String namespace = app.getActivities().getDhis().getNamespace();
if (namespace != null && !namespace.isEmpty()) {
reservedNamespaces.put(namespace, app);
}
appMap.put(app.getUrlFriendlyName(), app);
log.info("Discovered app '" + app.getName() + "' from JClouds storage ");
});
if (appList.isEmpty()) {
log.info("No apps found during JClouds discovery.");
}
return appMap;
}
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, StringUtils.EMPTY, file, fileResource.getContentMd5());
if (blob == null) {
return null;
}
blobStore.putBlob(config.container, 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