|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version |
| 4 | + * 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may |
| 5 | + * obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless |
| 6 | + * required by applicable law or agreed to in writing, software * distributed under the License is |
| 7 | + * distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 8 | + * or implied. * See the License for the specific language governing permissions and * limitations |
| 9 | + * under the License. |
| 10 | + */ |
| 11 | + |
| 12 | +package com.google.gcloud.storage; |
| 13 | + |
| 14 | +import static com.google.common.base.Preconditions.checkArgument; |
| 15 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 16 | +import static com.google.gcloud.storage.Blob.BlobSourceOption.convert; |
| 17 | + |
| 18 | +import com.google.common.collect.ImmutableList; |
| 19 | +import com.google.common.collect.Iterables; |
| 20 | +import com.google.gcloud.spi.StorageRpc; |
| 21 | +import com.google.gcloud.storage.Storage.BlobTargetOption; |
| 22 | +import com.google.gcloud.storage.Storage.CopyRequest; |
| 23 | +import com.google.gcloud.storage.Storage.SignUrlOption; |
| 24 | + |
| 25 | +import java.net.URL; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * A Google cloud storage object. |
| 31 | + */ |
| 32 | +public final class Blob { |
| 33 | + |
| 34 | + private final Storage storage; |
| 35 | + private BlobInfo info; |
| 36 | + |
| 37 | + public static class BlobSourceOption extends Option { |
| 38 | + |
| 39 | + private static final long serialVersionUID = 214616862061934846L; |
| 40 | + |
| 41 | + private BlobSourceOption(StorageRpc.Option rpcOption) { |
| 42 | + super(rpcOption, null); |
| 43 | + } |
| 44 | + |
| 45 | + private Storage.BlobSourceOption convert(BlobInfo blobInfo) { |
| 46 | + switch (rpcOption()) { |
| 47 | + case IF_GENERATION_MATCH: |
| 48 | + return Storage.BlobSourceOption.generationMatch(blobInfo.generation()); |
| 49 | + case IF_GENERATION_NOT_MATCH: |
| 50 | + return Storage.BlobSourceOption.generationNotMatch(blobInfo.generation()); |
| 51 | + case IF_METAGENERATION_MATCH: |
| 52 | + return Storage.BlobSourceOption.metagenerationMatch(blobInfo.metageneration()); |
| 53 | + case IF_METAGENERATION_NOT_MATCH: |
| 54 | + return Storage.BlobSourceOption.metagenerationNotMatch(blobInfo.metageneration()); |
| 55 | + default: |
| 56 | + throw new AssertionError("Unexpected enum value"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static BlobSourceOption generationMatch() { |
| 61 | + return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH); |
| 62 | + } |
| 63 | + |
| 64 | + public static BlobSourceOption generationNotMatch() { |
| 65 | + return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH); |
| 66 | + } |
| 67 | + |
| 68 | + public static BlobSourceOption metagenerationMatch() { |
| 69 | + return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH); |
| 70 | + } |
| 71 | + |
| 72 | + public static BlobSourceOption metagenerationNotMatch() { |
| 73 | + return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH); |
| 74 | + } |
| 75 | + |
| 76 | + static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption... options) { |
| 77 | + Storage.BlobSourceOption[] convertedOptions = new Storage.BlobSourceOption[options.length]; |
| 78 | + int index = 0; |
| 79 | + for (BlobSourceOption option : options) { |
| 80 | + convertedOptions[index++] = option.convert(blobInfo); |
| 81 | + } |
| 82 | + return convertedOptions; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public Blob(Storage storage, BlobInfo info) { |
| 87 | + this.storage = checkNotNull(storage); |
| 88 | + this.info = checkNotNull(info); |
| 89 | + } |
| 90 | + |
| 91 | + public BlobInfo info() { |
| 92 | + return info; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns true if this blob exists. |
| 97 | + * |
| 98 | + * @throws StorageException upon failure |
| 99 | + */ |
| 100 | + public boolean exists() { |
| 101 | + return storage.get(info.bucket(), info.name()) != null; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns the blob's content. |
| 106 | + * |
| 107 | + * @throws StorageException upon failure |
| 108 | + */ |
| 109 | + public byte[] content(Storage.BlobSourceOption... options) { |
| 110 | + return storage.readAllBytes(info.bucket(), info.name(), options); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Updates the blob's information. Bucket or blob's name cannot be changed by this method. If you |
| 115 | + * want to rename the blob or move it to a different bucket use the {@link #copyTo} and |
| 116 | + * {@link #delete} operations. |
| 117 | + * |
| 118 | + * @throws StorageException upon failure |
| 119 | + */ |
| 120 | + public void update(BlobInfo blobInfo, BlobTargetOption... options) { |
| 121 | + checkArgument(Objects.equals(blobInfo.bucket(), info.bucket()), "Bucket name must match"); |
| 122 | + checkArgument(Objects.equals(blobInfo.name(), info.name()), "Blob name must match"); |
| 123 | + info = storage.update(blobInfo, options); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Deletes this blob. |
| 128 | + * |
| 129 | + * @return true if bucket was deleted |
| 130 | + * @throws StorageException upon failure |
| 131 | + */ |
| 132 | + public boolean delete(BlobSourceOption... options) { |
| 133 | + return storage.delete(info.bucket(), info.name(), convert(info, options)); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Send a copy request. |
| 138 | + * |
| 139 | + * @return the copied blob. |
| 140 | + * @throws StorageException upon failure |
| 141 | + */ |
| 142 | + public Blob copyTo(BlobInfo target, BlobSourceOption... options) { |
| 143 | + return copyTo(target, ImmutableList.copyOf(options), ImmutableList.<BlobTargetOption>of()); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Send a copy request. |
| 148 | + * |
| 149 | + * @return the copied blob. |
| 150 | + * @throws StorageException upon failure |
| 151 | + */ |
| 152 | + public Blob copyTo(BlobInfo target, Iterable<BlobSourceOption> sourceOptions, |
| 153 | + Iterable<BlobTargetOption> targetOptions) { |
| 154 | + CopyRequest copyRequest = |
| 155 | + CopyRequest.builder().source(info.bucket(), info.name()) |
| 156 | + .sourceOptions(convert(info, Iterables.toArray(sourceOptions, BlobSourceOption.class))) |
| 157 | + .target(target).targetOptions(targetOptions).build(); |
| 158 | + return new Blob(storage, storage.copy(copyRequest)); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns a channel for reading this blob's content. |
| 163 | + * |
| 164 | + * @throws StorageException upon failure |
| 165 | + */ |
| 166 | + public BlobReadChannel reader(BlobSourceOption... options) { |
| 167 | + return storage.reader(info.bucket(), info.name(), convert(info, options)); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Returns a channel for writing to this blob. |
| 172 | + * |
| 173 | + * @throws StorageException upon failure |
| 174 | + */ |
| 175 | + public BlobWriteChannel writer(BlobTargetOption... options) { |
| 176 | + return storage.writer(info, options); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Generates a signed URL for this blob. If you want to allow access to for a fixed amount of time |
| 181 | + * for this blob, you can use this method to generate a URL that is only valid within a certain |
| 182 | + * time period. This is particularly useful if you don't want publicly accessible blobs, but don't |
| 183 | + * want to require users to explicitly log in. |
| 184 | + * |
| 185 | + * @param expirationTimeInSeconds the signed URL expiration (using epoch time) |
| 186 | + * @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed-URLs</a> |
| 187 | + */ |
| 188 | + public URL signUrl(long expirationTimeInSeconds, SignUrlOption... options) { |
| 189 | + return storage.signUrl(info, expirationTimeInSeconds, options); |
| 190 | + } |
| 191 | + |
| 192 | + public Storage storage() { |
| 193 | + return storage; |
| 194 | + } |
| 195 | +} |
0 commit comments