2828import com .google .gcloud .storage .Storage .SignUrlOption ;
2929
3030import java .net .URL ;
31- import java .util .Arrays ;
3231import java .util .Collections ;
3332import java .util .List ;
3433import java .util .Objects ;
@@ -119,7 +118,19 @@ public Blob(Storage storage, BlobInfo info) {
119118 */
120119 public Blob (Storage storage , String bucket , String blob ) {
121120 this .storage = checkNotNull (storage );
122- this .info = BlobInfo .of (checkNotNull (bucket ), checkNotNull (blob ));
121+ this .info = BlobInfo .builder (BlobId .of (bucket , blob )).build ();
122+ }
123+
124+ /**
125+ * Constructs a {@code Blob} object for the provided {@code BlobId}. The storage service is used
126+ * to issue requests.
127+ *
128+ * @param storage the storage service used for issuing requests
129+ * @param blobId blob's identifier
130+ */
131+ public Blob (Storage storage , BlobId blobId ) {
132+ this .storage = checkNotNull (storage );
133+ this .info = BlobInfo .builder (blobId ).build ();
123134 }
124135
125136 /**
@@ -129,14 +140,21 @@ public BlobInfo info() {
129140 return info ;
130141 }
131142
143+ /**
144+ * Returns the blob's id.
145+ */
146+ public BlobId id () {
147+ return info .blobId ();
148+ }
149+
132150 /**
133151 * Checks if this blob exists.
134152 *
135153 * @return true if this blob exists, false otherwise
136154 * @throws StorageException upon failure
137155 */
138156 public boolean exists () {
139- return storage .get (info .bucket (), info . name ()) != null ;
157+ return storage .get (info .blobId ()) != null ;
140158 }
141159
142160 /**
@@ -146,7 +164,7 @@ public boolean exists() {
146164 * @throws StorageException upon failure
147165 */
148166 public byte [] content (Storage .BlobSourceOption ... options ) {
149- return storage .readAllBytes (info .bucket (), info . name (), options );
167+ return storage .readAllBytes (info .blobId (), options );
150168 }
151169
152170 /**
@@ -157,7 +175,7 @@ public byte[] content(Storage.BlobSourceOption... options) {
157175 * @throws StorageException upon failure
158176 */
159177 public Blob reload (BlobSourceOption ... options ) {
160- return new Blob (storage , storage .get (info .bucket (), info . name (), convert (info , options )));
178+ return new Blob (storage , storage .get (info .blobId (), convert (info , options )));
161179 }
162180
163181 /**
@@ -179,6 +197,23 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
179197 return new Blob (storage , storage .update (blobInfo , options ));
180198 }
181199
200+ /**
201+ * Copies this blob to the specified target. Possibly copying also some of the metadata
202+ * (e.g. content-type).
203+ *
204+ * @param targetBlob target blob's id
205+ * @param options source blob options
206+ * @return the copied blob
207+ * @throws StorageException upon failure
208+ */
209+ public Blob copyTo (BlobId targetBlob , BlobSourceOption ... options ) {
210+ BlobInfo updatedInfo = info .toBuilder ().blobId (targetBlob ).build ();
211+ CopyRequest copyRequest =
212+ CopyRequest .builder ().source (info .bucket (), info .name ())
213+ .sourceOptions (convert (info , options )).target (updatedInfo ).build ();
214+ return new Blob (storage , storage .copy (copyRequest ));
215+ }
216+
182217 /**
183218 * Deletes this blob.
184219 *
@@ -187,7 +222,7 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
187222 * @throws StorageException upon failure
188223 */
189224 public boolean delete (BlobSourceOption ... options ) {
190- return storage .delete (info .bucket (), info . name (), convert (info , options ));
225+ return storage .delete (info .blobId (), convert (info , options ));
191226 }
192227
193228 /**
@@ -214,7 +249,7 @@ public Blob copyTo(String targetBucket, BlobSourceOption... options) {
214249 * @throws StorageException upon failure
215250 */
216251 public Blob copyTo (String targetBucket , String targetBlob , BlobSourceOption ... options ) {
217- BlobInfo updatedInfo = info .toBuilder ().bucket ( targetBucket ). name ( targetBlob ).build ();
252+ BlobInfo updatedInfo = info .toBuilder ().blobId ( BlobId . of ( targetBucket , targetBlob ) ).build ();
218253 CopyRequest copyRequest =
219254 CopyRequest .builder ().source (info .bucket (), info .name ())
220255 .sourceOptions (convert (info , options )).target (updatedInfo ).build ();
@@ -228,7 +263,7 @@ public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... o
228263 * @throws StorageException upon failure
229264 */
230265 public BlobReadChannel reader (BlobSourceOption ... options ) {
231- return storage .reader (info .bucket (), info . name (), convert (info , options ));
266+ return storage .reader (info .blobId (), convert (info , options ));
232267 }
233268
234269 /**
@@ -270,18 +305,18 @@ public Storage storage() {
270305 * {@code infos.length > 1} a batch request is used to fetch blobs.
271306 *
272307 * @param storage the storage service used to issue the request
273- * @param infos the blobs to get
308+ * @param blobs the blobs to get
274309 * @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has
275310 * been denied the corresponding item in the list is {@code null}.
276311 * @throws StorageException upon failure
277312 */
278- public static List <Blob > get (final Storage storage , BlobInfo ... infos ) {
313+ public static List <Blob > get (final Storage storage , BlobId ... blobs ) {
279314 checkNotNull (storage );
280- checkNotNull (infos );
281- if (infos .length == 0 ) {
315+ checkNotNull (blobs );
316+ if (blobs .length == 0 ) {
282317 return Collections .emptyList ();
283318 }
284- return Collections .unmodifiableList (Lists .transform (storage .get (infos ),
319+ return Collections .unmodifiableList (Lists .transform (storage .get (blobs ),
285320 new Function <BlobInfo , Blob >() {
286321 @ Override
287322 public Blob apply (BlobInfo f ) {
@@ -320,18 +355,18 @@ public Blob apply(BlobInfo f) {
320355 * {@code infos.length > 1} a batch request is used to delete blobs.
321356 *
322357 * @param storage the storage service used to issue the request
323- * @param infos the blobs to delete
358+ * @param blobs the blobs to delete
324359 * @return an immutable list of booleans. If a blob has been deleted the corresponding item in the
325360 * list is {@code true}. If deletion failed or access to the resource was denied the item is
326361 * {@code false}.
327362 * @throws StorageException upon failure
328363 */
329- public static List <Boolean > delete (Storage storage , BlobInfo ... infos ) {
364+ public static List <Boolean > delete (Storage storage , BlobId ... blobs ) {
330365 checkNotNull (storage );
331- checkNotNull (infos );
332- if (infos .length == 0 ) {
366+ checkNotNull (blobs );
367+ if (blobs .length == 0 ) {
333368 return Collections .emptyList ();
334369 }
335- return storage .delete (infos );
370+ return storage .delete (blobs );
336371 }
337372}
0 commit comments