@Override BlobId[] parse(String... args) { if (args.length < 2) { throw new IllegalArgumentException(); } BlobId[] blobs = new BlobId[args.length - 1]; for (int i = 1; i < args.length; i++) { blobs[i - 1] = BlobId.of(args[0], args[i]); } return blobs; }
/** Example of deleting several blobs using a single batch request. */ // [TARGET delete(BlobId...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] // [VARIABLE "my_blob_name2"] public List<Boolean> batchDelete(String bucketName, String blobName1, String blobName2) { // [START batchDelete] BlobId firstBlob = BlobId.of(bucketName, blobName1); BlobId secondBlob = BlobId.of(bucketName, blobName2); List<Boolean> deleted = storage.delete(firstBlob, secondBlob); // [END batchDelete] return deleted; }
/** * Adds a request representing the "get blob" operation to this batch. The {@code options} can be * used in the same way as for {@link Storage#get(BlobId, BlobGetOption...)}. Calling {@link * StorageBatchResult#get()} on the return value yields the requested {@link Blob} if successful, * {@code null} if no such blob exists, or throws a {@link StorageException} if the operation * failed. */ public StorageBatchResult<Blob> get(String bucket, String blob, BlobGetOption... options) { return get(BlobId.of(bucket, blob), options); }
/** Returns a {@code BlobInfo} builder where blob identity is set using the provided values. */ public static Builder newBuilder(String bucket, String name) { return newBuilder(BlobId.of(bucket, name)); }
@Override CopyRequest parse(String... args) { if (args.length != 4) { throw new IllegalArgumentException(); } return CopyRequest.of(args[0], args[1], BlobId.of(args[2], args[3])); }
/** Example of getting information on a blob. */ // [TARGET get(BlobId)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob getBlobFromId(String bucketName, String blobName) { // [START getBlobFromId] BlobId blobId = BlobId.of(bucketName, blobName); Blob blob = storage.get(blobId); // [END getBlobFromId] return blob; }
/** Example of copying the blob to a different bucket with a different name. */ // [TARGET copyTo(BlobId, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "copy_blob_name"] public Blob copyToId(String bucketName, String blobName) { // [START copyToId] CopyWriter copyWriter = blob.copyTo(BlobId.of(bucketName, blobName)); Blob copiedBlob = copyWriter.getResult(); // [END copyToId] return copiedBlob; }
/** Example of getting the ACL entry for a specific user on a blob. */ // [TARGET getAcl(BlobId, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "google-cloud-java-tests@java-docs-samples-tests.iam.gserviceaccount.com"] public Acl getBlobAcl(String bucketName, String blobName, String userEmail) { // [START storagePrintFileAclForUser] BlobId blobId = BlobId.of(bucketName, blobName); Acl acl = storage.getAcl(blobId, new User(userEmail)); // [END storagePrintFileAclForUser] return acl; }
/** Returns path converted to a {@link BlobId} so I/O can be performed. */ BlobId getBlobId() { checkArgument(!path.toString().isEmpty(), "Object names cannot be empty."); return BlobId.of(bucket(), toRealPath().path.toString()); }
/** Example of updating a blob to be public-read. */ // [TARGET createAcl(BlobId, Acl)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] public Acl blobToPublicRead(String bucketName, String blobName, long blobGeneration) { // [START storageMakePublic] BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration); Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER)); // [END storageMakePublic] return acl; }
/** * Creates a {@code ComposeRequest} object. * * @param bucket name of the bucket where the compose operation takes place * @param sources source blobs names * @param target target blob name */ public static ComposeRequest of(String bucket, Iterable<String> sources, String target) { return of(sources, BlobInfo.newBuilder(BlobId.of(bucket, target)).build()); }
static BlobId fromPb(StorageObject storageObject) { return BlobId.of( storageObject.getBucket(), storageObject.getName(), storageObject.getGeneration()); } }
/** Example of creating a blob with no content. */ // [TARGET create(BlobInfo, BlobTargetOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob createBlob(String bucketName, String blobName) { // [START createBlob] BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo); // [END createBlob] return blob; }
public static void main(String... args) { // [START storage_upload_file] Storage storage = StorageOptions.getDefaultInstance().getService(); BlobId blobId = BlobId.of("bucket", "blob_name"); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8)); // [END storage_upload_file] } }
@Test public void testListBlobAcl() { BlobId blobId = BlobId.of(BUCKET_NAME1, BLOB_NAME1, 42L); EasyMock.expect(storageRpcMock.listAcls(BUCKET_NAME1, BLOB_NAME1, 42L)) .andReturn(ImmutableList.of(ACL.toObjectPb(), OTHER_ACL.toObjectPb())); EasyMock.replay(storageRpcMock); initializeService(); List<Acl> acls = storage.listAcls(blobId); assertEquals(ImmutableList.of(ACL, OTHER_ACL), acls); }
@Test public void testBlobId() { assertEquals(BlobId.of("b", "n", GENERATION), BLOB_INFO.getBlobId()); } }
@Test public void testReadAllBytes() { EasyMock.expect( storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(BLOB_CONTENT); EasyMock.replay(storageRpcMock); initializeService(); byte[] readBytes = storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1); assertArrayEquals(BLOB_CONTENT, readBytes); }
@Test public void testGetBlobAclNull() { BlobId blobId = BlobId.of(BUCKET_NAME1, BLOB_NAME1, 42L); EasyMock.expect(storageRpcMock.getAcl(BUCKET_NAME1, BLOB_NAME1, 42L, "allAuthenticatedUsers")) .andReturn(null); EasyMock.replay(storageRpcMock); initializeService(); assertNull(storage.getAcl(blobId, User.ofAllAuthenticatedUsers())); }
@Test public void testGetBlobWithOptions() { EasyMock.expect( storageRpcMock.get(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_GET_OPTIONS)) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.get(BUCKET_NAME1, BLOB_NAME1, BLOB_GET_METAGENERATION, BLOB_GET_GENERATION); assertEquals(expectedBlob1, blob); }
@Test public void testToBuilder() { compareBlobs(BLOB_INFO, BLOB_INFO.toBuilder().build()); BlobInfo blobInfo = BLOB_INFO.toBuilder().setBlobId(BlobId.of("b2", "n2")).setSize(200L).build(); assertEquals("n2", blobInfo.getName()); assertEquals("b2", blobInfo.getBucket()); assertEquals(Long.valueOf(200), blobInfo.getSize()); blobInfo = blobInfo.toBuilder().setBlobId(BlobId.of("b", "n", GENERATION)).setSize(SIZE).build(); compareBlobs(BLOB_INFO, blobInfo); }