|
| 1 | +/* |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.gcloud.storage; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | +import static com.google.gcloud.storage.Blob.BlobSourceOption.convert; |
| 22 | + |
| 23 | +import com.google.gcloud.spi.StorageRpc; |
| 24 | +import com.google.gcloud.storage.Storage.BlobTargetOption; |
| 25 | +import com.google.gcloud.storage.Storage.CopyRequest; |
| 26 | +import com.google.gcloud.storage.Storage.SignUrlOption; |
| 27 | + |
| 28 | +import java.net.URL; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +/** |
| 32 | + * A Google cloud storage object. |
| 33 | + * |
| 34 | + * <p> |
| 35 | + * Objects of this class are immutable. Operations that modify the blob like {@link #update} and |
| 36 | + * {@link #copyTo} return a new object. To get a {@code Blob} object with the most recent |
| 37 | + * information use {@link #reload}. |
| 38 | + * </p> |
| 39 | + */ |
| 40 | +public final class Blob { |
| 41 | + |
| 42 | + private final Storage storage; |
| 43 | + private final BlobInfo info; |
| 44 | + |
| 45 | + public static class BlobSourceOption extends Option { |
| 46 | + |
| 47 | + private static final long serialVersionUID = 214616862061934846L; |
| 48 | + |
| 49 | + private BlobSourceOption(StorageRpc.Option rpcOption) { |
| 50 | + super(rpcOption, null); |
| 51 | + } |
| 52 | + |
| 53 | + private Storage.BlobSourceOption convert(BlobInfo blobInfo) { |
| 54 | + switch (rpcOption()) { |
| 55 | + case IF_GENERATION_MATCH: |
| 56 | + return Storage.BlobSourceOption.generationMatch(blobInfo.generation()); |
| 57 | + case IF_GENERATION_NOT_MATCH: |
| 58 | + return Storage.BlobSourceOption.generationNotMatch(blobInfo.generation()); |
| 59 | + case IF_METAGENERATION_MATCH: |
| 60 | + return Storage.BlobSourceOption.metagenerationMatch(blobInfo.metageneration()); |
| 61 | + case IF_METAGENERATION_NOT_MATCH: |
| 62 | + return Storage.BlobSourceOption.metagenerationNotMatch(blobInfo.metageneration()); |
| 63 | + default: |
| 64 | + throw new AssertionError("Unexpected enum value"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static BlobSourceOption generationMatch() { |
| 69 | + return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH); |
| 70 | + } |
| 71 | + |
| 72 | + public static BlobSourceOption generationNotMatch() { |
| 73 | + return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH); |
| 74 | + } |
| 75 | + |
| 76 | + public static BlobSourceOption metagenerationMatch() { |
| 77 | + return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH); |
| 78 | + } |
| 79 | + |
| 80 | + public static BlobSourceOption metagenerationNotMatch() { |
| 81 | + return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH); |
| 82 | + } |
| 83 | + |
| 84 | + static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption... options) { |
| 85 | + Storage.BlobSourceOption[] convertedOptions = new Storage.BlobSourceOption[options.length]; |
| 86 | + int index = 0; |
| 87 | + for (BlobSourceOption option : options) { |
| 88 | + convertedOptions[index++] = option.convert(blobInfo); |
| 89 | + } |
| 90 | + return convertedOptions; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Constructs a {@code Blob} object for the provided {@code BlobInfo}. The storage service is used |
| 96 | + * to issue requests. |
| 97 | + * |
| 98 | + * @param storage the storage service used for issuing requests |
| 99 | + * @param info blob's info |
| 100 | + */ |
| 101 | + public Blob(Storage storage, BlobInfo info) { |
| 102 | + this.storage = checkNotNull(storage); |
| 103 | + this.info = checkNotNull(info); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Constructs a {@code Blob} object for the provided bucket and blob names. The storage service is |
| 108 | + * used to issue requests. |
| 109 | + * |
| 110 | + * @param storage the storage service used for issuing requests |
| 111 | + * @param bucket bucket's name |
| 112 | + * @param blob blob's name |
| 113 | + */ |
| 114 | + public Blob(Storage storage, String bucket, String blob) { |
| 115 | + this.storage = checkNotNull(storage); |
| 116 | + this.info = BlobInfo.of(checkNotNull(bucket), checkNotNull(blob)); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the blob's information. |
| 121 | + */ |
| 122 | + public BlobInfo info() { |
| 123 | + return info; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Checks if this blob exists. |
| 128 | + * |
| 129 | + * @return true if this blob exists, false otherwise |
| 130 | + * @throws StorageException upon failure |
| 131 | + */ |
| 132 | + public boolean exists() { |
| 133 | + return storage.get(info.bucket(), info.name()) != null; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns this blob's content. |
| 138 | + * |
| 139 | + * @param options blob read options |
| 140 | + * @throws StorageException upon failure |
| 141 | + */ |
| 142 | + public byte[] content(Storage.BlobSourceOption... options) { |
| 143 | + return storage.readAllBytes(info.bucket(), info.name(), options); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Fetches current blob's latest information. |
| 148 | + * |
| 149 | + * @param options blob read options |
| 150 | + * @return a {@code Blob} object with latest information |
| 151 | + * @throws StorageException upon failure |
| 152 | + */ |
| 153 | + public Blob reload(BlobSourceOption... options) { |
| 154 | + return new Blob(storage, storage.get(info.bucket(), info.name(), convert(info, options))); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Updates the blob's information. Bucket or blob's name cannot be changed by this method. If you |
| 159 | + * want to rename the blob or move it to a different bucket use the {@link #copyTo} and |
| 160 | + * {@link #delete} operations. A new {@code Blob} object is returned. By default no checks are |
| 161 | + * made on the metadata generation of the current blob. If you want to update the information only |
| 162 | + * if the current blob metadata are at their latest version use the {@code metagenerationMatch} |
| 163 | + * option: {@code blob.update(newInfo, BlobTargetOption.metagenerationMatch())}. |
| 164 | + * |
| 165 | + * @param blobInfo new blob's information. Bucket and blob names must match the current ones |
| 166 | + * @param options update options |
| 167 | + * @return a {@code Blob} object with updated information |
| 168 | + * @throws StorageException upon failure |
| 169 | + */ |
| 170 | + public Blob update(BlobInfo blobInfo, BlobTargetOption... options) { |
| 171 | + checkArgument(Objects.equals(blobInfo.bucket(), info.bucket()), "Bucket name must match"); |
| 172 | + checkArgument(Objects.equals(blobInfo.name(), info.name()), "Blob name must match"); |
| 173 | + return new Blob(storage, storage.update(blobInfo, options)); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Deletes this blob. |
| 178 | + * |
| 179 | + * @param options blob delete options |
| 180 | + * @return true if blob was deleted |
| 181 | + * @throws StorageException upon failure |
| 182 | + */ |
| 183 | + public boolean delete(BlobSourceOption... options) { |
| 184 | + return storage.delete(info.bucket(), info.name(), convert(info, options)); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Copies this blob to the target bucket, preserving its name. Possibly copying also some of the |
| 189 | + * metadata (e.g. content-type). |
| 190 | + * |
| 191 | + * @param targetBucket target bucket's name |
| 192 | + * @param options source blob options |
| 193 | + * @return the copied blob |
| 194 | + * @throws StorageException upon failure |
| 195 | + */ |
| 196 | + public Blob copyTo(String targetBucket, BlobSourceOption... options) { |
| 197 | + return copyTo(targetBucket, info.name(), options); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Copies this blob to the target bucket with a new name. Possibly copying also some of the |
| 202 | + * metadata (e.g. content-type). |
| 203 | + * |
| 204 | + * @param targetBucket target bucket's name |
| 205 | + * @param targetBlob target blob's name |
| 206 | + * @param options source blob options |
| 207 | + * @return the copied blob |
| 208 | + * @throws StorageException upon failure |
| 209 | + */ |
| 210 | + public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) { |
| 211 | + BlobInfo updatedInfo = info.toBuilder().bucket(targetBucket).name(targetBlob).build(); |
| 212 | + CopyRequest copyRequest = |
| 213 | + CopyRequest.builder().source(info.bucket(), info.name()) |
| 214 | + .sourceOptions(convert(info, options)).target(updatedInfo).build(); |
| 215 | + return new Blob(storage, storage.copy(copyRequest)); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Returns a {@code BlobReadChannel} object for reading this blob's content. |
| 220 | + * |
| 221 | + * @param options blob read options |
| 222 | + * @throws StorageException upon failure |
| 223 | + */ |
| 224 | + public BlobReadChannel reader(BlobSourceOption... options) { |
| 225 | + return storage.reader(info.bucket(), info.name(), convert(info, options)); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Returns a {@code BlobWriteChannel} object for writing to this blob. |
| 230 | + * |
| 231 | + * @param options target blob options |
| 232 | + * @throws StorageException upon failure |
| 233 | + */ |
| 234 | + public BlobWriteChannel writer(BlobTargetOption... options) { |
| 235 | + return storage.writer(info, options); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Generates a signed URL for this blob. If you want to allow access to for a fixed amount of time |
| 240 | + * for this blob, you can use this method to generate a URL that is only valid within a certain |
| 241 | + * time period. This is particularly useful if you don't want publicly accessible blobs, but don't |
| 242 | + * want to require users to explicitly log in. |
| 243 | + * |
| 244 | + * @param expirationTimeInSeconds the signed URL expiration (using epoch time) |
| 245 | + * @param options signed url options |
| 246 | + * @return a signed URL for this bucket and the specified options |
| 247 | + * @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed-URLs</a> |
| 248 | + */ |
| 249 | + public URL signUrl(long expirationTimeInSeconds, SignUrlOption... options) { |
| 250 | + return storage.signUrl(info, expirationTimeInSeconds, options); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Returns the blob's {@code Storage} object used to issue requests. |
| 255 | + */ |
| 256 | + public Storage storage() { |
| 257 | + return storage; |
| 258 | + } |
| 259 | +} |
0 commit comments