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?