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