use of org.jclouds.blobstore.BlobStoreContextFactory 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();
}
}
use of org.jclouds.blobstore.BlobStoreContextFactory 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.");
}
use of org.jclouds.blobstore.BlobStoreContextFactory 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;
}
use of org.jclouds.blobstore.BlobStoreContextFactory 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);
}
}
Aggregations