| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.s3.blobstore; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Provider; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.blobstore.BlobStoreContext; |
| 31 | import org.jclouds.blobstore.domain.Blob; |
| 32 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 33 | import org.jclouds.blobstore.domain.PageSet; |
| 34 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 35 | import org.jclouds.blobstore.domain.internal.PageSetImpl; |
| 36 | import org.jclouds.blobstore.functions.BlobToHttpGetOptions; |
| 37 | import org.jclouds.blobstore.internal.BaseBlobStore; |
| 38 | import org.jclouds.blobstore.options.CreateContainerOptions; |
| 39 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 40 | import org.jclouds.blobstore.options.PutOptions; |
| 41 | import org.jclouds.blobstore.strategy.internal.FetchBlobMetadata; |
| 42 | import org.jclouds.blobstore.util.BlobUtils; |
| 43 | import org.jclouds.collect.Memoized; |
| 44 | import org.jclouds.domain.Location; |
| 45 | import org.jclouds.http.options.GetOptions; |
| 46 | import org.jclouds.s3.S3Client; |
| 47 | import org.jclouds.s3.blobstore.functions.BlobToObject; |
| 48 | import org.jclouds.s3.blobstore.functions.BucketToResourceList; |
| 49 | import org.jclouds.s3.blobstore.functions.BucketToResourceMetadata; |
| 50 | import org.jclouds.s3.blobstore.functions.ContainerToBucketListOptions; |
| 51 | import org.jclouds.s3.blobstore.functions.ObjectToBlob; |
| 52 | import org.jclouds.s3.blobstore.functions.ObjectToBlobMetadata; |
| 53 | import org.jclouds.s3.domain.AccessControlList; |
| 54 | import org.jclouds.s3.domain.AccessControlList.GroupGranteeURI; |
| 55 | import org.jclouds.s3.domain.AccessControlList.Permission; |
| 56 | import org.jclouds.s3.domain.BucketMetadata; |
| 57 | import org.jclouds.s3.domain.CannedAccessPolicy; |
| 58 | import org.jclouds.s3.options.ListBucketOptions; |
| 59 | import org.jclouds.s3.options.PutBucketOptions; |
| 60 | import org.jclouds.s3.options.PutObjectOptions; |
| 61 | import org.jclouds.s3.util.S3Utils; |
| 62 | import org.jclouds.util.Assertions; |
| 63 | |
| 64 | import com.google.common.base.Function; |
| 65 | import com.google.common.base.Supplier; |
| 66 | import com.google.common.collect.Iterables; |
| 67 | |
| 68 | /** |
| 69 | * |
| 70 | * @author Adrian Cole |
| 71 | */ |
| 72 | @Singleton |
| 73 | public class S3BlobStore extends BaseBlobStore { |
| 74 | private final S3Client sync; |
| 75 | private final BucketToResourceMetadata bucket2ResourceMd; |
| 76 | private final ContainerToBucketListOptions container2BucketListOptions; |
| 77 | private final BucketToResourceList bucket2ResourceList; |
| 78 | private final ObjectToBlob object2Blob; |
| 79 | private final BlobToObject blob2Object; |
| 80 | private final ObjectToBlobMetadata object2BlobMd; |
| 81 | private final BlobToHttpGetOptions blob2ObjectGetOptions; |
| 82 | private final Provider<FetchBlobMetadata> fetchBlobMetadataProvider; |
| 83 | private final Map<String, AccessControlList> bucketAcls; |
| 84 | |
| 85 | @Inject |
| 86 | protected S3BlobStore(BlobStoreContext context, BlobUtils blobUtils, Supplier<Location> defaultLocation, |
| 87 | @Memoized Supplier<Set<? extends Location>> locations, S3Client sync, |
| 88 | BucketToResourceMetadata bucket2ResourceMd, ContainerToBucketListOptions container2BucketListOptions, |
| 89 | BucketToResourceList bucket2ResourceList, ObjectToBlob object2Blob, |
| 90 | BlobToHttpGetOptions blob2ObjectGetOptions, BlobToObject blob2Object, ObjectToBlobMetadata object2BlobMd, |
| 91 | Provider<FetchBlobMetadata> fetchBlobMetadataProvider, Map<String, AccessControlList> bucketAcls) { |
| 92 | super(context, blobUtils, defaultLocation, locations); |
| 93 | this.blob2ObjectGetOptions = checkNotNull(blob2ObjectGetOptions, "blob2ObjectGetOptions"); |
| 94 | this.sync = checkNotNull(sync, "sync"); |
| 95 | this.bucket2ResourceMd = checkNotNull(bucket2ResourceMd, "bucket2ResourceMd"); |
| 96 | this.container2BucketListOptions = checkNotNull(container2BucketListOptions, "container2BucketListOptions"); |
| 97 | this.bucket2ResourceList = checkNotNull(bucket2ResourceList, "bucket2ResourceList"); |
| 98 | this.object2Blob = checkNotNull(object2Blob, "object2Blob"); |
| 99 | this.blob2Object = checkNotNull(blob2Object, "blob2Object"); |
| 100 | this.object2BlobMd = checkNotNull(object2BlobMd, "object2BlobMd"); |
| 101 | this.fetchBlobMetadataProvider = checkNotNull(fetchBlobMetadataProvider, "fetchBlobMetadataProvider"); |
| 102 | this.bucketAcls = checkNotNull(bucketAcls, "bucketAcls"); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * This implementation invokes {@link S3Client#listOwnedBuckets} |
| 107 | */ |
| 108 | @Override |
| 109 | public PageSet<? extends StorageMetadata> list() { |
| 110 | return new Function<Set<BucketMetadata>, org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata>>() { |
| 111 | public org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata> apply(Set<BucketMetadata> from) { |
| 112 | return new PageSetImpl<StorageMetadata>(Iterables.transform(from, bucket2ResourceMd), null); |
| 113 | } |
| 114 | }.apply(sync.listOwnedBuckets()); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * This implementation invokes {@link S3Client#bucketExists} |
| 119 | * |
| 120 | * @param container |
| 121 | * bucket name |
| 122 | */ |
| 123 | @Override |
| 124 | public boolean containerExists(String container) { |
| 125 | return sync.bucketExists(container); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * This implementation invokes {@link S3Client#putBucketInRegion} |
| 130 | * |
| 131 | * @param location |
| 132 | * corresponds to a Region |
| 133 | * @param container |
| 134 | * bucket name |
| 135 | */ |
| 136 | @Override |
| 137 | public boolean createContainerInLocation(Location location, String container) { |
| 138 | return createContainerInLocation(location, container, CreateContainerOptions.NONE); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * This implementation invokes {@link S3Client#listBucket} |
| 143 | * |
| 144 | * @param container |
| 145 | * bucket name |
| 146 | */ |
| 147 | @Override |
| 148 | public PageSet<? extends StorageMetadata> list(String container, ListContainerOptions options) { |
| 149 | ListBucketOptions httpOptions = container2BucketListOptions.apply(options); |
| 150 | PageSet<? extends StorageMetadata> list = bucket2ResourceList.apply(sync.listBucket(container, httpOptions)); |
| 151 | return options.isDetailed() ? fetchBlobMetadataProvider.get().setContainerName(container).apply(list) : list; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * This implementation invokes {@link #deleteAndEnsurePathGone} |
| 156 | * |
| 157 | * @param container |
| 158 | * bucket name |
| 159 | */ |
| 160 | @Override |
| 161 | public void deleteContainer(String container) { |
| 162 | clearAndDeleteContainer(container); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * This implementation invokes {@link #clearContainer} then {@link S3Client#deleteBucketIfEmpty} |
| 167 | * until it is true. |
| 168 | */ |
| 169 | public void clearAndDeleteContainer(final String container) { |
| 170 | try { |
| 171 | if (!Assertions.eventuallyTrue(new Supplier<Boolean>() { |
| 172 | public Boolean get() { |
| 173 | clearContainer(container); |
| 174 | return sync.deleteBucketIfEmpty(container); |
| 175 | } |
| 176 | }, 30000)) { |
| 177 | throw new IllegalStateException(container + " still exists after deleting!"); |
| 178 | } |
| 179 | } catch (InterruptedException e) { |
| 180 | new IllegalStateException(container + " interrupted during deletion!", e); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * This implementation invokes {@link S3Client#objectExists} |
| 186 | * |
| 187 | * @param container |
| 188 | * bucket name |
| 189 | * @param key |
| 190 | * object key |
| 191 | */ |
| 192 | @Override |
| 193 | public boolean blobExists(String container, String key) { |
| 194 | return sync.objectExists(container, key); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * This implementation invokes {@link S3Client#headObject} |
| 199 | * |
| 200 | * @param container |
| 201 | * bucket name |
| 202 | * @param key |
| 203 | * object key |
| 204 | */ |
| 205 | @Override |
| 206 | public BlobMetadata blobMetadata(String container, String key) { |
| 207 | return object2BlobMd.apply(sync.headObject(container, key)); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * This implementation invokes {@link S3Client#getObject} |
| 212 | * |
| 213 | * @param container |
| 214 | * bucket name |
| 215 | * @param key |
| 216 | * object key |
| 217 | */ |
| 218 | @Override |
| 219 | public Blob getBlob(String container, String key, org.jclouds.blobstore.options.GetOptions optionsList) { |
| 220 | GetOptions httpOptions = blob2ObjectGetOptions.apply(optionsList); |
| 221 | return object2Blob.apply(sync.getObject(container, key, httpOptions)); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * This implementation invokes {@link S3Client#putObject} |
| 226 | * |
| 227 | * @param container |
| 228 | * bucket name |
| 229 | * @param blob |
| 230 | * object |
| 231 | */ |
| 232 | @Override |
| 233 | public String putBlob(String container, Blob blob) { |
| 234 | PutObjectOptions options = new PutObjectOptions(); |
| 235 | try { |
| 236 | AccessControlList acl = bucketAcls.get(container); |
| 237 | if (acl != null && acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ)) |
| 238 | options.withAcl(CannedAccessPolicy.PUBLIC_READ); |
| 239 | } catch (NullPointerException e) { |
| 240 | // MapMaker |
| 241 | } |
| 242 | return sync.putObject(container, blob2Object.apply(blob), options); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * This implementation invokes {@link S3Client#putObject} |
| 247 | * |
| 248 | * @param container |
| 249 | * bucket name |
| 250 | * @param blob |
| 251 | * object |
| 252 | */ |
| 253 | @Override |
| 254 | public String putBlob(String container, Blob blob, PutOptions options) { |
| 255 | // TODO implement options |
| 256 | return putBlob(container, blob); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * This implementation invokes {@link S3Client#deleteObject} |
| 261 | * |
| 262 | * @param container |
| 263 | * bucket name |
| 264 | * @param key |
| 265 | * object key |
| 266 | */ |
| 267 | @Override |
| 268 | public void removeBlob(String container, String key) { |
| 269 | sync.deleteObject(container, key); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * This implementation invokes {@link S3Utils#deleteAndVerifyContainerGone} |
| 274 | */ |
| 275 | protected boolean deleteAndVerifyContainerGone(final String container) { |
| 276 | return S3Utils.deleteAndVerifyContainerGone(sync, container); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public boolean createContainerInLocation(Location location, String container, CreateContainerOptions options) { |
| 281 | PutBucketOptions putBucketOptions = new PutBucketOptions(); |
| 282 | if (options.isPublicRead()) |
| 283 | putBucketOptions.withBucketAcl(CannedAccessPolicy.PUBLIC_READ); |
| 284 | location = location != null ? location : defaultLocation.get(); |
| 285 | return sync.putBucketInRegion(location.getId(), container, putBucketOptions); |
| 286 | } |
| 287 | } |