Refine search
/** * Sets the copy target. Target blob information is copied from source, except for those * options specified in {@code options}. * * @return the builder */ public Builder setTarget(BlobId targetId, BlobTargetOption... options) { this.overrideInfo = false; this.target = BlobInfo.newBuilder(targetId).build(); Collections.addAll(targetOptions, options); return this; }
/** * Sets the copy target. Target blob information is copied from source. * * @return the builder */ public Builder setTarget(BlobId targetId) { this.overrideInfo = false; this.target = BlobInfo.newBuilder(targetId).build(); return this; }
/** * Sets the copy target and target options. Target blob information is copied from source, * except for those options specified in {@code options}. * * @return the builder */ public Builder setTarget(BlobId targetId, Iterable<BlobTargetOption> options) { this.overrideInfo = false; this.target = BlobInfo.newBuilder(targetId).build(); Iterables.addAll(targetOptions, options); return this; }
@Override Tuple<Path, BlobInfo> parse(String... args) throws IOException { if (args.length < 2 || args.length > 3) { throw new IllegalArgumentException(); } Path path = Paths.get(args[0]); String contentType = Files.probeContentType(path); String blob = args.length < 3 ? path.getFileName().toString() : args[2]; return Tuple.of(path, BlobInfo.newBuilder(args[1], blob).setContentType(contentType).build()); }
/** * Example of creating a signed URL that is valid for 2 weeks, using the default credentials for * signing the URL. */ // [TARGET signUrl(BlobInfo, long, TimeUnit, SignUrlOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public URL signUrl(String bucketName, String blobName) { // [START signUrl] URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 14, TimeUnit.DAYS); // [END signUrl] return signedUrl; }
/** Example of replacing blob's metadata. */ // [TARGET update(BlobInfo)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob updateBlob(String bucketName, String blobName) { // [START updateBlob] Map<String, String> newMetadata = new HashMap<>(); newMetadata.put("key", "value"); storage.update(BlobInfo.newBuilder(bucketName, blobName).setMetadata(null).build()); Blob blob = storage.update(BlobInfo.newBuilder(bucketName, blobName).setMetadata(newMetadata).build()); // [END updateBlob] return blob; }
/** Example of creating a blob from an input stream. */ // [TARGET create(BlobInfo, InputStream, BlobWriteOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob createBlobFromInputStream(String bucketName, String blobName) { // [START createBlobFromInputStream] InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8)); BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, content); // [END createBlobFromInputStream] return blob; }
/** * 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()); }
/** Example of creating a blob from a byte array. */ // [TARGET create(BlobInfo, byte[], BlobTargetOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public Blob createBlobFromByteArray(String bucketName, String blobName) { // [START createBlobFromByteArray] BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, "Hello, World!".getBytes(UTF_8)); // [END createBlobFromByteArray] return blob; }
@Override ComposeRequest parse(String... args) { if (args.length < 3) { throw new IllegalArgumentException(); } ComposeRequest.Builder request = ComposeRequest.newBuilder(); request.setTarget(BlobInfo.newBuilder(args[0], args[args.length - 1]).build()); for (int i = 1; i < args.length - 1; i++) { request.addSource(args[i]); } return request.build(); }
/** 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; }
/** Example of uploading an encrypted blob. */ // [TARGET create(BlobInfo, InputStream, BlobWriteOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "my_encryption_key"] public Blob createEncryptedBlob(String bucketName, String blobName, String encryptionKey) { // [START storageUploadEncryptedFile] byte[] data = "Hello, World!".getBytes(UTF_8); BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, data, BlobTargetOption.encryptionKey(encryptionKey)); // [END storageUploadEncryptedFile] 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] } }
private static void fillFile(Storage storage, String bucket, String fname, int size) throws IOException { storage.create(BlobInfo.newBuilder(bucket, fname).build(), randomContents(size)); }
@Test public void testDeleteBlobsFail() { String sourceBlobName1 = "test-delete-blobs-fail-1"; String sourceBlobName2 = "test-delete-blobs-fail-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); List<Boolean> deleteStatus = storage.delete(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertTrue(deleteStatus.get(0)); assertFalse(deleteStatus.get(1)); }
@Test public void testCreateFromStreamNoContentType() throws Exception { initializeExpectedBucket(5); BlobInfo info = BlobInfo.newBuilder("b", "n").build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; InputStream streamContent = new ByteArrayInputStream(content); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, streamContent)).andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", streamContent); assertEquals(expectedBlob, blob); }
private static void fillRequesterPaysFile(Storage storage, String fname, int size) throws IOException { storage.create( BlobInfo.newBuilder(REQUESTER_PAYS_BUCKET, fname).build(), randomContents(size), BlobTargetOption.userProject(project)); }
@Test public void testCreateNoContentType() throws Exception { initializeExpectedBucket(5); BlobInfo info = BlobInfo.newBuilder("b", "n").build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, content)).andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", content); assertEquals(expectedBlob, blob); }
@Test public void testCreateFromStream() throws Exception { initializeExpectedBucket(5); BlobInfo info = BlobInfo.newBuilder("b", "n").setContentType(CONTENT_TYPE).build(); Blob expectedBlob = new Blob(serviceMockReturnsOptions, new BlobInfo.BuilderImpl(info)); byte[] content = {0xD, 0xE, 0xA, 0xD}; InputStream streamContent = new ByteArrayInputStream(content); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.create(info, streamContent)).andReturn(expectedBlob); replay(storage); initializeBucket(); Blob blob = bucket.create("n", streamContent, CONTENT_TYPE); assertEquals(expectedBlob, blob); }
@Test public void testDeleteBlobs() { String sourceBlobName1 = "test-delete-blobs-1"; String sourceBlobName2 = "test-delete-blobs-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); assertNotNull(storage.create(sourceBlob2)); List<Boolean> deleteStatus = storage.delete(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertTrue(deleteStatus.get(0)); assertTrue(deleteStatus.get(1)); }