mirror of https://github.com/apache/jclouds.git
Merge pull request #720 from andrewgaul/filesystem-transient-putblob
Harmonize filesystem and transient putBlob
This commit is contained in:
commit
01f121a654
|
@ -457,17 +457,22 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
|
|||
String blobKey = blob.getMetadata().getName();
|
||||
|
||||
logger.debug("Put blob with key [%s] to container [%s]", blobKey, containerName);
|
||||
String eTag = getEtag(blob);
|
||||
if (!storageStrategy.containerExists(containerName)) {
|
||||
return Futures.immediateFailedFuture(new IllegalStateException("containerName not found: " + containerName));
|
||||
}
|
||||
|
||||
try {
|
||||
// TODO
|
||||
// must override existing file?
|
||||
|
||||
storageStrategy.writePayloadOnFile(containerName, blobKey, blob.getPayload());
|
||||
storageStrategy.putBlob(containerName, blob);
|
||||
} catch (IOException e) {
|
||||
logger.error(e, "An error occurred storing the new blob with name [%s] to container [%s].", blobKey,
|
||||
containerName);
|
||||
Throwables.propagate(e);
|
||||
}
|
||||
|
||||
String eTag = getEtag(blob);
|
||||
return immediateFuture(eTag);
|
||||
}
|
||||
|
||||
|
@ -591,13 +596,6 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
|
|||
}
|
||||
}
|
||||
|
||||
private Blob copyBlob(Blob blob) {
|
||||
Blob returnVal = blobFactory.create(copy(blob.getMetadata()));
|
||||
returnVal.setPayload(blob.getPayload());
|
||||
copyPayloadHeadersToBlob(blob.getPayload(), returnVal);
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the object MD5 and returns it as eTag
|
||||
*
|
||||
|
@ -616,6 +614,13 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
|
|||
return eTag;
|
||||
}
|
||||
|
||||
private Blob copyBlob(Blob blob) {
|
||||
Blob returnVal = blobFactory.create(copy(blob.getMetadata()));
|
||||
returnVal.setPayload(blob.getPayload());
|
||||
copyPayloadHeadersToBlob(blob.getPayload(), returnVal);
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean deleteAndVerifyContainerGone(final String container) {
|
||||
storageStrategy.deleteContainer(container);
|
||||
|
|
|
@ -169,11 +169,11 @@ public interface FilesystemStorageStrategy {
|
|||
void removeBlob(String container, String key);
|
||||
|
||||
/**
|
||||
* Write a {@link Blob} {@link Payload} into a file
|
||||
* @param fileName
|
||||
* @param payload
|
||||
* Write a {@link Blob} into a file
|
||||
* @param container
|
||||
* @param blob
|
||||
* @throws IOException
|
||||
*/
|
||||
void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException;
|
||||
void putBlob(String containerName, Blob blob) throws IOException;
|
||||
|
||||
}
|
||||
|
|
|
@ -204,19 +204,13 @@ public class FilesystemStorageStrategyImpl implements FilesystemStorageStrategy
|
|||
return blobFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a {@link Blob} {@link Payload} into a file
|
||||
*
|
||||
* @param container
|
||||
* @param blobKey
|
||||
* @param payload
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void writePayloadOnFile(String container, String blobKey, Payload payload) throws IOException {
|
||||
filesystemContainerNameValidator.validate(container);
|
||||
public void putBlob(final String containerName, final Blob blob) throws IOException {
|
||||
String blobKey = blob.getMetadata().getName();
|
||||
Payload payload = blob.getPayload();
|
||||
filesystemContainerNameValidator.validate(containerName);
|
||||
filesystemBlobKeyValidator.validate(blobKey);
|
||||
File outputFile = getFileForBlobKey(container, blobKey);
|
||||
File outputFile = getFileForBlobKey(containerName, blobKey);
|
||||
FileOutputStream output = null;
|
||||
try {
|
||||
Files.createParentDirs(outputFile);
|
||||
|
|
|
@ -740,6 +740,7 @@ public class FilesystemAsyncBlobStoreTest {
|
|||
* can't be deleted. See http://code.google.com/p/jclouds/issues/detail?id=737
|
||||
*/
|
||||
final String containerName = "containerWithRanges";
|
||||
blobStore.createContainerInLocation(null, containerName);
|
||||
String payload = "abcdefgh";
|
||||
InputStream is;
|
||||
Blob blob = blobStore.blobBuilder("test").payload(new StringPayload(payload)).build();
|
||||
|
|
|
@ -371,8 +371,10 @@ public class FilesystemStorageStrategyImplTest {
|
|||
blobKey = TestUtils.createRandomBlobKey("writePayload-", ".img");
|
||||
sourceFile = TestUtils.getImageForBlobPayload();
|
||||
filePayload = new FilePayload(sourceFile);
|
||||
Blob blob = storageStrategy.newBlob(blobKey);
|
||||
blob.setPayload(filePayload);
|
||||
// write files
|
||||
storageStrategy.writePayloadOnFile(CONTAINER_NAME, blobKey, filePayload);
|
||||
storageStrategy.putBlob(CONTAINER_NAME, blob);
|
||||
// verify that the files is equal
|
||||
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
|
||||
InputSupplier<FileInputStream> expectedInput =
|
||||
|
|
|
@ -472,7 +472,8 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
|
|||
|
||||
storageStrategy.putBlob(containerName, blob);
|
||||
|
||||
return immediateFuture(Iterables.getOnlyElement(blob.getAllHeaders().get(HttpHeaders.ETAG)));
|
||||
String eTag = getEtag(blob);
|
||||
return immediateFuture(eTag);
|
||||
}
|
||||
|
||||
private Blob createUpdatedCopyOfBlobInContainer(String containerName, Blob in) {
|
||||
|
@ -635,6 +636,24 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the object MD5 and returns it as eTag
|
||||
*
|
||||
* @param object
|
||||
* @return
|
||||
*/
|
||||
private String getEtag(Blob object) {
|
||||
try {
|
||||
Payloads.calculateMD5(object, crypto.md5());
|
||||
} catch (IOException ex) {
|
||||
logger.error(ex, "An error occurred calculating MD5 for object with name %s.", object.getMetadata().getName());
|
||||
Throwables.propagate(ex);
|
||||
}
|
||||
|
||||
String eTag = CryptoStreams.hex(object.getPayload().getContentMetadata().getContentMD5());
|
||||
return eTag;
|
||||
}
|
||||
|
||||
private Blob copyBlob(Blob blob) {
|
||||
Blob returnVal = blobFactory.create(copy(blob.getMetadata()));
|
||||
returnVal.setPayload(blob.getPayload());
|
||||
|
|
Loading…
Reference in New Issue