2

I've got the following method which allows me to upload files to containers on Rackspace CloudFiles :

/**
 * Uploads a file to the storage.
 *
 * @param     f the <code>File</code> which is to be uploaded to the storage.
 * @param     fileContainer a <code>String</code> representing the container
 *            which the provided <code>File</code> is to be uploaded to.
 * @throws    StorageException if an attempt to upload the provided file to
 *            the storage failed.
 */
public static void upload(File file, String fileContainer) throws StorageException {

    if (!file.exists()) {
        throw new StorageException("The file '" + file.getName() + "' does not exist.");
    }

    try {

        BlobStoreContext cb = ContextBuilder.newBuilder("cloudfiles-uk")
            .credentials(USERNAME, PASSWORD)
            .buildView(BlobStoreContext.class);            

        Blob blob = cb.getBlobStore().blobBuilder(file.getName())
            .payload(file)
            .build();

        cb.getBlobStore().putBlob(fileContainer, blob);

    } catch (Exception e) {
        throw new StorageException(e);
    }
}

Right now, I'm creataing a new context every time the method is called. As far as I understand, the code will only authenticate on first call and from there use a key issued during the first authentication on all subsequent calls. However, I'm not sure it that is correct? Will I be re-authenticating if i throw away the BlobStoreContext instance and instantiate a new one every time upload() is invoked? Would it be a better idea to keep the BlobStoreContext instance?

sbrattla
  • 5,274
  • 3
  • 39
  • 63

1 Answers1

1

As you have your code now, you will be reauthenticating on each call to the 'upload' function. Instead, you'll probably want to create a global context variable, call an authentication function to set your credentials, and then use the context in your upload function.

See this example:

https://github.com/jclouds/jclouds-examples/blob/master/rackspace/src/main/java/org/jclouds/examples/rackspace/Authentication.java

JRP
  • 979
  • 6
  • 9