• Sales: 1-800-961-2888
  • Support: 1-800-961-4454

Sample Java Application


A little Extra info for the Java Test Application

Unlike scripting languages like Ruby or Python. You have to build the sources and add all the JAR's and additional class files you need to use at compile time; like including the Apache HTTP Commons stuff. You can either do this using the --classpath option with javac but, it can become a dependency nightmare. I highly recommend using an IDE like Eclipse or Netbeans and adding the sources to your buildpaths. Also note that during compilation there is a cloudfiles.properties file that must be added to the buildpath. If you have any other questions please feel free to shoot us an email at cloudfiles@rackspacecloud.com

Simple Java Application

package com.rackspacecloud.test;
import java.io.File;
import java.io.IOException;
import org.apache.commons.httpclient.HttpException;
import com.rackspacecloud.client.cloudfiles.FilesClient;
public class Cftest {
public static void main(String[] args) throws HttpException, IOException {
 FilesClient client = new FilesClient("UserNameGoesHere", "APIKeyGoesHere");
 client.login();
 client.createContainer("ContainerNameGoesHere");
 File f = new File("PathToFileGoesHere");
 client.storeObject("ContainerNameGoesHere", f, "Content Type");
 }
}


© 2011-2013 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER

13 Comments

HI, i am new to cloud computing. Can you tell me where can i get the jars? and which all are the jars necessary for the above example

Hello,
It is not working for me. Same program when i run i got this exception.

com.rackspacecloud.client.cloudfiles.FilesAuthorizationException: You must be logged in

So, please help.

Thank You.

It sounds like the issue might be with the authorization information. Make sure you replace the values in the line creating FileClient that designate the username and API key you're using.

Hello,

When I call client.login(), I get:

Exception in thread "main" java.lang.NullPointerException
at java.net.URI$Parser.parse(URI.java:3004)
at java.net.URI.<init>(URI.java:577)
at java.net.URI.create(URI.java:839)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:69)
at com.rackspacecloud.client.cloudfiles.FilesClient.login(FilesClient.java:270)

Any help would be greatly appreciated.

Thanks.

It looks like an authentication issue. Double-check the username and API key you've set for creating the FilesClient object (line 8).

There are a few problems with the example code. First off, the Authentication URL is null, so it needs to be set before calling client.login(). That is why you are getting the NullPointerException. After you get past that hurdle, you would get an error saying that the timeout value for the connection was 0 or negative. To fix that, you call client.setConnectionTimeout()

Here is the complete code

package com.rackspacecloud.test;
import java.io.File;
import java.io.IOException;
import org.apache.commons.httpclient.HttpException;
import com.rackspacecloud.client.cloudfiles.FilesClient;
public class Cftest {
public static void main(String[] args) throws HttpException, IOException {
FilesClient client = new FilesClient("UserNameGoesHere", "APIKeyGoesHere");
client.setAuthenticationURL("https://auth.api.rackspacecloud.com/v1.0");
client.setConnectionTimeOut(5000); //time in ms
client.login();
client.createContainer("ContainerNameGoesHere");
File f = new File("PathToFileGoesHere");
client.storeObject("ContainerNameGoesHere", f, "Content Type");
}
}

I believe their internal calls using HttpClient aren't right...I think they should be doing method.releaseConnection() in their finally instead of just method.abort()

How do you set the headers on an object you're storing, like X-Delete-After?
Thanks.

You can update the data on an object through the API:

http://docs.rackspace.com/files/api/v1/cf-devguide/content/Update_Object_Metadata-d1e2338.html

well, we tried doing this, but it didn't seem to work. the file didn't get deleted.

Map<String,String> metadata = new HashMap<String,String>();

metadata.put("X-Delete-After", "120");

rackspaceFilesClient.storeStreamedObject(RACKSPACE_CONTAINER, new ByteArrayInputStream(decodedPage.getBytes("UTF-8")), RACKSPACE_MIME_TYPE, filename, metadata);

Hm, I'll pass your code back to the devs and see if they can find the problem.

wow, Ive never seen an api insisting on crapping up every project that uses it like this one. Consider making this more self contained and doing away with all the extra junk. bare-bones would be SO much better. you have doubled the number of lib resources in an already large system, half of which are redundant just because you felt like pushing the particular 3rd parties YOU like, and its nearly all unnecessary.

I fear this is often going to be the case. Most software has dependencies. This is solved in the Java world with Maven (or Gradle or what-have-you). If you really well and truly only want the Cloud Files portion of the Java SDK you can add the following to your Maven pom.xml file:

<dependencies>
<dependency>
<groupId>org.jclouds.provider</groupId>
<artifactId>cloudfiles-us</artifactId>
<version>1.5.5</version>
</dependency>
</dependencies>

That will bring in the minimum number of dependencies to work with Cloud Files US (use artifactId cloudfiles-uk for Cloud Files UK).

Add new comment