JCLOUDS-651: portable copy object content metadata

This commit is contained in:
Andrew Gaul 2015-04-06 17:51:17 -07:00
parent 3f2e9f37fd
commit 0647ba8c55
3 changed files with 58 additions and 11 deletions

View File

@ -511,15 +511,36 @@ public final class LocalBlobStore implements BlobStore {
is = blob.getPayload().openStream(); is = blob.getPayload().openStream();
ContentMetadata metadata = blob.getMetadata().getContentMetadata(); ContentMetadata metadata = blob.getMetadata().getContentMetadata();
BlobBuilder.PayloadBlobBuilder builder = blobBuilder(toName) BlobBuilder.PayloadBlobBuilder builder = blobBuilder(toName)
.payload(is) .payload(is);
.contentDisposition(metadata.getContentDisposition())
.contentEncoding(metadata.getContentEncoding())
.contentLanguage(metadata.getContentLanguage())
.contentType(metadata.getContentType());
Long contentLength = metadata.getContentLength(); Long contentLength = metadata.getContentLength();
if (contentLength != null) { if (contentLength != null) {
builder.contentLength(contentLength); builder.contentLength(contentLength);
} }
if (options.getContentMetadata().isPresent()) {
ContentMetadata contentMetadata = options.getContentMetadata().get();
String contentDisposition = contentMetadata.getContentDisposition();
if (contentDisposition != null) {
builder.contentDisposition(contentDisposition);
}
String contentEncoding = contentMetadata.getContentEncoding();
if (contentEncoding != null) {
builder.contentEncoding(contentEncoding);
}
String contentLanguage = contentMetadata.getContentLanguage();
if (contentLanguage != null) {
builder.contentLanguage(contentLanguage);
}
String contentType = contentMetadata.getContentType();
if (contentType != null) {
builder.contentType(contentType);
}
} else {
builder.contentDisposition(metadata.getContentDisposition())
.contentEncoding(metadata.getContentEncoding())
.contentLanguage(metadata.getContentLanguage())
.contentType(metadata.getContentType());
}
Optional<Map<String, String>> userMetadata = options.getUserMetadata(); Optional<Map<String, String>> userMetadata = options.getUserMetadata();
if (userMetadata.isPresent()) { if (userMetadata.isPresent()) {
builder.userMetadata(userMetadata.get()); builder.userMetadata(userMetadata.get());

View File

@ -17,8 +17,12 @@
package org.jclouds.blobstore.options; package org.jclouds.blobstore.options;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map; import java.util.Map;
import org.jclouds.io.ContentMetadata;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@ -27,12 +31,18 @@ import com.google.common.collect.ImmutableMap;
public final class CopyOptions { public final class CopyOptions {
public static final CopyOptions NONE = builder().build(); public static final CopyOptions NONE = builder().build();
private final Optional<ContentMetadata> contentMetadata;
private final Optional<Map<String, String>> userMetadata; private final Optional<Map<String, String>> userMetadata;
private CopyOptions(Builder builder) { private CopyOptions(Builder builder) {
this.contentMetadata = Optional.fromNullable(builder.contentMetadata);
this.userMetadata = Optional.fromNullable(builder.userMetadata); this.userMetadata = Optional.fromNullable(builder.userMetadata);
} }
public Optional<ContentMetadata> getContentMetadata() {
return contentMetadata;
}
public Optional<Map<String, String>> getUserMetadata() { public Optional<Map<String, String>> getUserMetadata() {
return userMetadata; return userMetadata;
} }
@ -42,11 +52,17 @@ public final class CopyOptions {
} }
public static class Builder { public static class Builder {
ContentMetadata contentMetadata;
Map<String, String> userMetadata; Map<String, String> userMetadata;
Builder() { Builder() {
} }
public Builder contentMetadata(ContentMetadata contentMetadata) {
this.contentMetadata = checkNotNull(contentMetadata, "contentMetadata");
return this;
}
public Builder userMetadata(Map<String, String> userMetadata) { public Builder userMetadata(Map<String, String> userMetadata) {
this.userMetadata = ImmutableMap.copyOf(userMetadata); this.userMetadata = ImmutableMap.copyOf(userMetadata);
return this; return this;

View File

@ -62,6 +62,7 @@ import org.jclouds.blobstore.options.PutOptions;
import org.jclouds.crypto.Crypto; import org.jclouds.crypto.Crypto;
import org.jclouds.encryption.internal.JCECrypto; import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.http.HttpResponseException; import org.jclouds.http.HttpResponseException;
import org.jclouds.io.ContentMetadataBuilder;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.ByteSourcePayload; import org.jclouds.io.payloads.ByteSourcePayload;
@ -777,7 +778,11 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
.blobBuilder(fromName) .blobBuilder(fromName)
.userMetadata(ImmutableMap.of("key1", "value1", "key2", "value2")) .userMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"))
.payload(payload) .payload(payload)
.contentLength(payload.size()); .contentLength(payload.size())
.contentDisposition("attachment; filename=original.jpg")
.contentEncoding("compress")
.contentLanguage("fr")
.contentType("audio/ogg");
addContentMetadata(blobBuilder); addContentMetadata(blobBuilder);
Blob blob = blobBuilder.build(); Blob blob = blobBuilder.build();
@ -786,8 +791,15 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
try { try {
blobStore.putBlob(fromContainer, blob); blobStore.putBlob(fromContainer, blob);
Map<String, String> userMetadata = ImmutableMap.of("key3", "value3", "key4", "value4"); Map<String, String> userMetadata = ImmutableMap.of("key3", "value3", "key4", "value4");
blobStore.copyBlob(fromContainer, fromName, toContainer, toName, blobStore.copyBlob(fromContainer, fromName, toContainer, toName, CopyOptions.builder()
CopyOptions.builder().userMetadata(userMetadata).build()); .contentMetadata(ContentMetadataBuilder.create()
.contentType("text/csv")
.contentDisposition("attachment; filename=photo.jpg")
.contentEncoding("gzip")
.contentLanguage("en")
.build())
.userMetadata(userMetadata)
.build());
Blob toBlob = blobStore.getBlob(toContainer, toName); Blob toBlob = blobStore.getBlob(toContainer, toName);
InputStream is = null; InputStream is = null;
try { try {
@ -796,9 +808,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
} finally { } finally {
Closeables2.closeQuietly(is); Closeables2.closeQuietly(is);
} }
// TODO: S3 overrideMetadataWith also overrides system metadata checkContentMetadata(toBlob);
// TODO: Swift does not preserve system metadata
//checkContentMetadata(toBlob);
checkUserMetadata(toBlob.getMetadata().getUserMetadata(), userMetadata); checkUserMetadata(toBlob.getMetadata().getUserMetadata(), userMetadata);
} finally { } finally {
returnContainer(toContainer); returnContainer(toContainer);