| 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.azureblob.blobstore; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.azure.storage.options.ListOptions.Builder.includeMetadata; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.Constants; |
| 32 | import org.jclouds.azure.storage.domain.BoundedSet; |
| 33 | import org.jclouds.azureblob.AzureBlobAsyncClient; |
| 34 | import org.jclouds.azureblob.blobstore.functions.AzureBlobToBlob; |
| 35 | import org.jclouds.azureblob.blobstore.functions.BlobPropertiesToBlobMetadata; |
| 36 | import org.jclouds.azureblob.blobstore.functions.BlobToAzureBlob; |
| 37 | import org.jclouds.azureblob.blobstore.functions.ContainerToResourceMetadata; |
| 38 | import org.jclouds.azureblob.blobstore.functions.ListBlobsResponseToResourceList; |
| 39 | import org.jclouds.azureblob.blobstore.functions.ListOptionsToListBlobsOptions; |
| 40 | import org.jclouds.azureblob.domain.AzureBlob; |
| 41 | import org.jclouds.azureblob.domain.BlobProperties; |
| 42 | import org.jclouds.azureblob.domain.ContainerProperties; |
| 43 | import org.jclouds.azureblob.domain.ListBlobsResponse; |
| 44 | import org.jclouds.azureblob.domain.PublicAccess; |
| 45 | import org.jclouds.azureblob.options.ListBlobsOptions; |
| 46 | import org.jclouds.blobstore.BlobStoreContext; |
| 47 | import org.jclouds.blobstore.domain.Blob; |
| 48 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 49 | import org.jclouds.blobstore.domain.PageSet; |
| 50 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 51 | import org.jclouds.blobstore.domain.internal.PageSetImpl; |
| 52 | import org.jclouds.blobstore.functions.BlobToHttpGetOptions; |
| 53 | import org.jclouds.blobstore.internal.BaseAsyncBlobStore; |
| 54 | import org.jclouds.blobstore.options.CreateContainerOptions; |
| 55 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 56 | import org.jclouds.blobstore.options.PutOptions; |
| 57 | import org.jclouds.blobstore.util.BlobUtils; |
| 58 | import org.jclouds.collect.Memoized; |
| 59 | import org.jclouds.concurrent.Futures; |
| 60 | import org.jclouds.domain.Location; |
| 61 | import org.jclouds.http.options.GetOptions; |
| 62 | |
| 63 | import com.google.common.base.Function; |
| 64 | import com.google.common.base.Supplier; |
| 65 | import com.google.common.collect.Iterables; |
| 66 | import com.google.common.util.concurrent.ListenableFuture; |
| 67 | |
| 68 | /** |
| 69 | * @author Adrian Cole |
| 70 | */ |
| 71 | @Singleton |
| 72 | public class AzureAsyncBlobStore extends BaseAsyncBlobStore { |
| 73 | private final AzureBlobAsyncClient async; |
| 74 | private final ContainerToResourceMetadata container2ResourceMd; |
| 75 | private final ListOptionsToListBlobsOptions blobStore2AzureContainerListOptions; |
| 76 | private final ListBlobsResponseToResourceList azure2BlobStoreResourceList; |
| 77 | private final AzureBlobToBlob azureBlob2Blob; |
| 78 | private final BlobToAzureBlob blob2AzureBlob; |
| 79 | private final BlobPropertiesToBlobMetadata blob2BlobMd; |
| 80 | private final BlobToHttpGetOptions blob2ObjectGetOptions; |
| 81 | |
| 82 | @Inject |
| 83 | AzureAsyncBlobStore(BlobStoreContext context, BlobUtils blobUtils, |
| 84 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService service, Supplier<Location> defaultLocation, |
| 85 | @Memoized Supplier<Set<? extends Location>> locations, AzureBlobAsyncClient async, |
| 86 | ContainerToResourceMetadata container2ResourceMd, |
| 87 | ListOptionsToListBlobsOptions blobStore2AzureContainerListOptions, |
| 88 | ListBlobsResponseToResourceList azure2BlobStoreResourceList, AzureBlobToBlob azureBlob2Blob, |
| 89 | BlobToAzureBlob blob2AzureBlob, BlobPropertiesToBlobMetadata blob2BlobMd, |
| 90 | BlobToHttpGetOptions blob2ObjectGetOptions) { |
| 91 | super(context, blobUtils, service, defaultLocation, locations); |
| 92 | this.async = checkNotNull(async, "async"); |
| 93 | this.container2ResourceMd = checkNotNull(container2ResourceMd, "container2ResourceMd"); |
| 94 | this.blobStore2AzureContainerListOptions = checkNotNull(blobStore2AzureContainerListOptions, |
| 95 | "blobStore2AzureContainerListOptions"); |
| 96 | this.azure2BlobStoreResourceList = checkNotNull(azure2BlobStoreResourceList, "azure2BlobStoreResourceList"); |
| 97 | this.azureBlob2Blob = checkNotNull(azureBlob2Blob, "azureBlob2Blob"); |
| 98 | this.blob2AzureBlob = checkNotNull(blob2AzureBlob, "blob2AzureBlob"); |
| 99 | this.blob2BlobMd = checkNotNull(blob2BlobMd, "blob2BlobMd"); |
| 100 | this.blob2ObjectGetOptions = checkNotNull(blob2ObjectGetOptions, "blob2ObjectGetOptions"); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * This implementation invokes {@link AzureBlobAsyncClient#listContainers} with the |
| 105 | * {@link org.jclouds.azure.storage.options.ListOptions#includeMetadata} option. |
| 106 | */ |
| 107 | @Override |
| 108 | public ListenableFuture<org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata>> list() { |
| 109 | return Futures |
| 110 | .compose( |
| 111 | async.listContainers(includeMetadata()), |
| 112 | new Function<BoundedSet<ContainerProperties>, org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata>>() { |
| 113 | public org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata> apply( |
| 114 | BoundedSet<ContainerProperties> from) { |
| 115 | return new PageSetImpl<StorageMetadata>(Iterables.transform(from, container2ResourceMd), |
| 116 | from.getNextMarker()); |
| 117 | } |
| 118 | }, service); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * This implementation invokes {@link AzureBlobAsyncClient#containerExists} |
| 123 | * |
| 124 | * @param container |
| 125 | * container name |
| 126 | */ |
| 127 | @Override |
| 128 | public ListenableFuture<Boolean> containerExists(String container) { |
| 129 | return async.containerExists(container); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * This implementation invokes {@link AzureBlobAsyncClient#createContainer} |
| 134 | * |
| 135 | * @param location |
| 136 | * ignored |
| 137 | * @param container |
| 138 | * container name |
| 139 | */ |
| 140 | @Override |
| 141 | public ListenableFuture<Boolean> createContainerInLocation(Location location, String container) { |
| 142 | return async.createContainer(container); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * This implementation invokes {@link AzureBlobAsyncClient#listBucket} |
| 147 | * |
| 148 | * @param container |
| 149 | * container name |
| 150 | */ |
| 151 | @Override |
| 152 | public ListenableFuture<PageSet<? extends StorageMetadata>> list(String container, ListContainerOptions options) { |
| 153 | ListBlobsOptions azureOptions = blobStore2AzureContainerListOptions.apply(options); |
| 154 | ListenableFuture<ListBlobsResponse> returnVal = async.listBlobs(container, azureOptions.includeMetadata()); |
| 155 | return Futures.compose(returnVal, azure2BlobStoreResourceList, service); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * This implementation invokes {@link AzureBlobAsyncClient#deleteContainer} |
| 160 | * |
| 161 | * @param container |
| 162 | * container name |
| 163 | */ |
| 164 | @Override |
| 165 | public ListenableFuture<Void> deleteContainer(final String container) { |
| 166 | return async.deleteContainer(container); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * This implementation invokes {@link AzureBlobAsyncClient#getBlob} |
| 171 | * |
| 172 | * @param container |
| 173 | * container name |
| 174 | * @param key |
| 175 | * blob key |
| 176 | */ |
| 177 | @Override |
| 178 | public ListenableFuture<Blob> getBlob(String container, String key, org.jclouds.blobstore.options.GetOptions options) { |
| 179 | GetOptions azureOptions = blob2ObjectGetOptions.apply(options); |
| 180 | ListenableFuture<AzureBlob> returnVal = async.getBlob(container, key, azureOptions); |
| 181 | return Futures.compose(returnVal, azureBlob2Blob, service); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * This implementation invokes {@link AzureBlobAsyncClient#putBlob} |
| 186 | * |
| 187 | * @param container |
| 188 | * container name |
| 189 | * @param blob |
| 190 | * blob |
| 191 | */ |
| 192 | @Override |
| 193 | public ListenableFuture<String> putBlob(String container, Blob blob) { |
| 194 | return async.putBlob(container, blob2AzureBlob.apply(blob)); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * This implementation invokes {@link AzureBlobAsyncClient#deleteObject} |
| 199 | * |
| 200 | * @param container |
| 201 | * container name |
| 202 | * @param key |
| 203 | * blob key |
| 204 | */ |
| 205 | @Override |
| 206 | public ListenableFuture<Void> removeBlob(String container, String key) { |
| 207 | return async.deleteBlob(container, key); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * This implementation invokes {@link AzureBlobAsyncClient#blobExists} |
| 212 | * |
| 213 | * @param container |
| 214 | * bucket name |
| 215 | * @param credential |
| 216 | * object key |
| 217 | */ |
| 218 | @Override |
| 219 | public ListenableFuture<Boolean> blobExists(String container, String name) { |
| 220 | return async.blobExists(container, name); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * This implementation invokes {@link AzureBlobAsyncClient#getBlobProperties} |
| 225 | * |
| 226 | * @param container |
| 227 | * container name |
| 228 | * @param key |
| 229 | * blob key |
| 230 | */ |
| 231 | @Override |
| 232 | public ListenableFuture<BlobMetadata> blobMetadata(String container, String key) { |
| 233 | return Futures.compose(async.getBlobProperties(container, key), new Function<BlobProperties, BlobMetadata>() { |
| 234 | |
| 235 | @Override |
| 236 | public BlobMetadata apply(BlobProperties from) { |
| 237 | return blob2BlobMd.apply(from); |
| 238 | } |
| 239 | |
| 240 | }, service); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | protected boolean deleteAndVerifyContainerGone(String container) { |
| 245 | throw new UnsupportedOperationException("please use deleteContainer"); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public ListenableFuture<String> putBlob(String container, Blob blob, PutOptions options) { |
| 250 | // TODO implement options |
| 251 | return putBlob(container, blob); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public ListenableFuture<Boolean> createContainerInLocation(Location location, String container, |
| 256 | CreateContainerOptions options) { |
| 257 | org.jclouds.azureblob.options.CreateContainerOptions createContainerOptions = new org.jclouds.azureblob.options.CreateContainerOptions(); |
| 258 | if (options.isPublicRead()) |
| 259 | createContainerOptions.withPublicAccess(PublicAccess.CONTAINER); |
| 260 | return async.createContainer(container, createContainerOptions); |
| 261 | } |
| 262 | } |