From 37a806d7b449b5f69513fdaf64aaf6c50c3bb2a9 Mon Sep 17 00:00:00 2001 From: adisesha Date: Fri, 31 May 2013 18:24:07 +0530 Subject: [PATCH] Demonstrates usage of blobstore file system api in scala Formatted Changed link to documentation Updated scala version String interpolation for output Main extends App trait Using scala-arm for managing context Link to scala-arm in documentation Calling 'acquireAndGet' instead 'acquireFor' managed context. Changed output format to remove usage of string interpolation escape syntax. Removed leading space in library dependencies configuration. Removed superfluous comment. Format, and modifications to documentation. Removed superfluous comment. Format, and modifications to documentation. --- blobstore-scala-filesystem/README.md | 10 ++++ blobstore-scala-filesystem/build.sbt | 10 ++++ .../src/main/scala/Main.scala | 59 ++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 blobstore-scala-filesystem/README.md create mode 100644 blobstore-scala-filesystem/build.sbt create mode 100644 blobstore-scala-filesystem/src/main/scala/Main.scala diff --git a/blobstore-scala-filesystem/README.md b/blobstore-scala-filesystem/README.md new file mode 100644 index 0000000..f478049 --- /dev/null +++ b/blobstore-scala-filesystem/README.md @@ -0,0 +1,10 @@ +# blobstore-scala-filesystem +This is a simple example command line client that creates a container and test blob in a filesystem [BlobStore](http://jclouds.incubator.apache.org/documentation/userguide/blobstore-guide/) using Scala. This example uses [scala-arm](https://github.com/jsuereth/scala-arm) to manage the [BlobStoreContext](http://javadocs.jclouds.cloudbees.net/org/jclouds/blobstore/BlobStoreContext.html) +## Build +Ensure that sbt is installed. Tested with 0.12.2 +## Run +Run sbt from the root of your project and invoke run _basedir_, where basedir is a directory in which the container will be created. E.g. if your basedir is /home/blobstore, run + +run /home/blobstore +## License +Licensed under the Apache License, Version 2.0 \ No newline at end of file diff --git a/blobstore-scala-filesystem/build.sbt b/blobstore-scala-filesystem/build.sbt new file mode 100644 index 0000000..6233ca3 --- /dev/null +++ b/blobstore-scala-filesystem/build.sbt @@ -0,0 +1,10 @@ +name :="blobstore-scala-filesystem" + +scalaVersion :="2.10.1" + +version :="1.0-SNAPSHOT" + +libraryDependencies ++= Seq( "org.jclouds.api" % "filesystem" % "1.6.0", + "com.google.code.findbugs" % "jsr305" % "1.3.+", + "com.jsuereth" %% "scala-arm" % "1.3" + ) diff --git a/blobstore-scala-filesystem/src/main/scala/Main.scala b/blobstore-scala-filesystem/src/main/scala/Main.scala new file mode 100644 index 0000000..2e9d206 --- /dev/null +++ b/blobstore-scala-filesystem/src/main/scala/Main.scala @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.jclouds.blobstore.BlobStoreContext +import org.jclouds.ContextBuilder +import org.jclouds.filesystem.reference.FilesystemConstants +import resource._ + + +/** + * Demonstrates the use of the filesystem [[org.jclouds.blobstore.BlobStore]] in Scala + * + * Usage is: run \"basedir\" + * + * @author adisesha + */ +object Main extends App { + require(args.length == 1, "Invalid number of parameters. Usage is: \"baseDir\" ") + + val baseDir = args(0) + + val properties = new java.util.Properties() + properties.setProperty(FilesystemConstants.PROPERTY_BASEDIR, baseDir) + + //Using scala-arm for context management. See https://github.com/jsuereth/scala-arm + managed(ContextBuilder.newBuilder("filesystem") + .overrides(properties) + .buildView(classOf[BlobStoreContext])) + .acquireAndGet(context => { + + val blobStore = context.getBlobStore + blobStore.createContainerInLocation(null, "test") + + val blob = blobStore.blobBuilder("test").payload("testdata").build() + blobStore.putBlob("test", blob) + + val filePath = baseDir + System.getProperty("file.separator") + "test" + println(s"File 'test' stored under, $filePath") + + }) + + +} -- 1.8.1.6