Java Code Examples for com.google.cloud.storage.BlobId#of()
The following examples show how to use
com.google.cloud.storage.BlobId#of() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
/** Example of uploading a blob encrypted service side with a Cloud KMS key. */ public Blob createKmsEncrpytedBlob(String bucketName, String blobName, String kmsKeyName) { // [START storage_upload_with_kms_key] byte[] data = "Hello, World!".getBytes(UTF_8); // The name of the existing bucket to set a default KMS key for, e.g. "my-bucket" // String bucketName = "my-bucket" // The name of the KMS-key to use as a default // Key names are provided in the following format: // 'projects/<PROJECT>/locations/<LOCATION>/keyRings/<RING_NAME>/cryptoKeys/<KEY_NAME>' // String kmsKeyName = "" BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, data, BlobTargetOption.kmsKeyName(kmsKeyName)); // [END storage_upload_with_kms_key] return blob; }
Example 2
Source File: StorageSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
/** Example of composing two blobs. */ // [TARGET compose(ComposeRequest)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "source_blob_1"] // [VARIABLE "source_blob_2"] public Blob composeBlobs( String bucketName, String blobName, String sourceBlob1, String sourceBlob2) { // [START composeBlobs] BlobId blobId = BlobId.of(bucketName, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); ComposeRequest request = ComposeRequest.newBuilder() .setTarget(blobInfo) .addSource(sourceBlob1) .addSource(sourceBlob2) .build(); Blob blob = storage.compose(request); // [END composeBlobs] return blob; }
Example 3
Source File: StorageExample.java From google-cloud-java with Apache License 2.0 | 6 votes |
@Override Tuple<BlobId, Acl> parse(String... args) { if (args.length >= 3) { BlobId blob; int nextArg; if (args.length == 3) { blob = BlobId.of(args[0], ""); nextArg = 1; } else if (args.length == 4) { blob = BlobId.of(args[0], args[1]); nextArg = 2; } else { throw new IllegalArgumentException("Too many arguments."); } String[] projectAndRole = args[nextArg++].split(":"); if (projectAndRole.length != 2) { throw new IllegalArgumentException( "Project entity must be specified as <projectId>:(OWNERS|READERS|WRITERS)"); } else { Acl.Project.ProjectRole projectRole = Acl.Project.ProjectRole.valueOf(projectAndRole[1]); Acl.Role role = Acl.Role.valueOf(args[nextArg]); return Tuple.of(blob, Acl.of(new Acl.Project(projectRole, projectAndRole[0]), role)); } } throw new IllegalArgumentException("Missing required bucket, project or role arguments."); }
Example 4
Source File: StorageSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
/** Example of deleting the ACL entry for an entity on a blob. */ // [TARGET deleteAcl(BlobId, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] public boolean deleteBlobAcl(String bucketName, String blobName, long blobGeneration) { // [START deleteBlobAcl] BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration); boolean deleted = storage.deleteAcl(blobId, User.ofAllAuthenticatedUsers()); if (deleted) { // the acl entry was deleted } else { // the acl entry was not found } // [END deleteBlobAcl] return deleted; }
Example 5
Source File: StorageSnippets.java From google-cloud-java with Apache License 2.0 | 6 votes |
/** Example of how to release a temporary hold for a blob */ public Blob releaseTemporaryHold(String bucketName, String blobName) throws StorageException { // [START storage_release_temporary_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; // The name of a blob, e.g. "my-blob" // String blobName = "my-blob"; BlobId blobId = BlobId.of(bucketName, blobName); Blob blob = storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(false).build()); System.out.println("Temporary hold was released for " + blobName); // [END storage_release_temporary_hold] return blob; }
Example 6
Source File: StorageSnippets.java From google-cloud-java with Apache License 2.0 | 5 votes |
/** Example of writing a blob's content through a writer. */ // [TARGET writer(BlobInfo, BlobWriteOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public void writer(String bucketName, String blobName) throws IOException { // [START writer] BlobId blobId = BlobId.of(bucketName, blobName); byte[] content = "Hello, World!".getBytes(UTF_8); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); try (WriteChannel writer = storage.writer(blobInfo)) { try { writer.write(ByteBuffer.wrap(content, 0, content.length)); } catch (Exception ex) { // handle exception } } // [END writer] }
Example 7
Source File: StorageSnippets.java From google-cloud-java with Apache License 2.0 | 5 votes |
/** Example of getting the ACL entry for an entity on a blob. */ // [TARGET getAcl(BlobId, Entity)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] public Acl getBlobAcl(String bucketName, String blobName, long blobGeneration) { // [START getBlobAcl] BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration); Acl acl = storage.getAcl(blobId, User.ofAllAuthenticatedUsers()); // [END getBlobAcl] return acl; }
Example 8
Source File: GoogleStorageTests.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Test public void testWritableOutputStreamWithAutoCreateOnNullBlob() throws Exception { String location = "gs://test-spring/test"; BlobId blobId = BlobId.of("test-spring", "test"); when(this.mockStorage.get(blobId)).thenReturn(null); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); WriteChannel writeChannel = mock(WriteChannel.class); Blob blob = mock(Blob.class); when(blob.writer()).thenReturn(writeChannel); when(this.mockStorage.create(blobInfo)).thenReturn(blob); GoogleStorageResource resource = new GoogleStorageResource(this.mockStorage, location); GoogleStorageResource spyResource = spy(resource); OutputStream os = spyResource.getOutputStream(); Assert.assertNotNull(os); }
Example 9
Source File: UpdateBlob.java From google-cloud-java with Apache License 2.0 | 5 votes |
public static void main(String... args) throws IOException { Storage storage = StorageOptions.getDefaultInstance().getService(); BlobId blobId = BlobId.of("bucket", "blob_name"); Blob blob = storage.get(blobId); if (blob != null) { byte[] prevContent = blob.getContent(); System.out.println(new String(prevContent, UTF_8)); WritableByteChannel channel = blob.writer(); channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8))); channel.close(); } }
Example 10
Source File: GoogleStorageTests.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
@Test public void testValidObjectWithUnderscore() throws Exception { BlobId validBlobWithUnderscore = BlobId.of("test_spring", "images/spring.png"); Blob mockedBlob = mock(Blob.class); when(mockStorage.get(eq(validBlobWithUnderscore))).thenReturn(mockedBlob); Assert.assertTrue(this.remoteResourceWithUnderscore.exists()); }
Example 11
Source File: GoogleStorageResource.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private BlobId getBlobId() { if (isBucket()) { throw new IllegalStateException("No blob id specified in the location: '" + getURI() + "', and the operation is not allowed on buckets."); } return BlobId.of(getBucketName(), getBlobName()); }
Example 12
Source File: GCSNotebookRepoTest.java From zeppelin with Apache License 2.0 | 5 votes |
private BlobId makeBlobId(String noteId, String notePath) { if (basePath.isPresent()) { return BlobId.of(bucketName, basePath.get() + notePath + "_" + noteId +".zpln"); } else { return BlobId.of(bucketName, notePath.substring(1) + "_" + noteId +".zpln"); } }
Example 13
Source File: StorageSnippets.java From google-cloud-java with Apache License 2.0 | 5 votes |
/** 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; }
Example 14
Source File: GCSNotebookRepo.java From zeppelin with Apache License 2.0 | 5 votes |
private BlobId makeBlobId(String noteId, String notePath) throws IOException { if (basePath.isPresent()) { return BlobId.of(bucketName, basePath.get() + "/" + buildNoteFileName(noteId, notePath)); } else { return BlobId.of(bucketName, buildNoteFileName(noteId, notePath)); } }
Example 15
Source File: CloudStorageKeysetManager.java From gcp-token-broker with Apache License 2.0 | 5 votes |
@Override public EncryptedKeyset readEncrypted() throws IOException { BlobId blobId = BlobId.of(dekUri.getAuthority(), dekUri.getPath().substring(1)); return JsonKeysetReader .withBytes(CloudStorageUtils.getCloudStorageClient().readAllBytes(blobId)) .readEncrypted(); }
Example 16
Source File: UploadEncryptedObject.java From google-cloud-java with Apache License 2.0 | 5 votes |
public static void uploadEncryptedObject( String projectId, String bucketName, String objectName, String filePath, String encryptionKey) throws IOException { // The ID of your GCP project // String projectId = "your-project-id"; // The ID of your GCS bucket // String bucketName = "your-unique-bucket-name"; // The ID of your GCS object // String objectName = "your-object-name"; // The path to your file to upload // String filePath = "path/to/your/file" // The key to encrypt the object with // String encryptionKey = "TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g="; Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); BlobId blobId = BlobId.of(bucketName, objectName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); storage.create( blobInfo, Files.readAllBytes(Paths.get(filePath)), Storage.BlobTargetOption.encryptionKey(encryptionKey)); System.out.println( "File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName + " with supplied encryption key"); }
Example 17
Source File: BlobIdToPubsubMessage.java From gcp-ingestion with Mozilla Public License 2.0 | 4 votes |
/** * Read the contents of a {@link PubsubMessage} into a {@link BlobId}. */ public static BlobId decode(PubsubMessage message) { return BlobId.of(message.getAttributesOrThrow(BUCKET), message.getAttributesOrThrow(NAME)); }
Example 18
Source File: GCSNotebookRepoTest.java From zeppelin with Apache License 2.0 | 4 votes |
private void createAt(Note note, String relativePath) throws IOException { BlobId id = BlobId.of(bucketName, makeName(relativePath)); BlobInfo info = BlobInfo.newBuilder(id).setContentType("application/json").build(); storage.create(info, note.toJson().getBytes("UTF-8")); }
Example 19
Source File: ITBlobSnippets.java From google-cloud-java with Apache License 2.0 | 4 votes |
@Test public void testGetObjectMetadata() { String blobName = "test-create-empty-blob"; BlobId blobId = BlobId.of(BUCKET, blobName); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setMetadata(ImmutableMap.of("k", "v")).build(); Blob remoteBlob = storage.create(blobInfo, CONTENT); assertNotNull(remoteBlob); PrintStream systemOut = System.out; final ByteArrayOutputStream snippetOutputCapture = new ByteArrayOutputStream(); System.setOut(new PrintStream(snippetOutputCapture)); GetObjectMetadata.getObjectMetadata(PROJECT_ID, BUCKET, blobName); String snippetOutput = snippetOutputCapture.toString(); System.setOut(systemOut); assertTrue(snippetOutput.contains("Bucket: " + remoteBlob.getBucket())); assertTrue(snippetOutput.contains("Bucket: " + remoteBlob.getBucket())); assertTrue(snippetOutput.contains("CacheControl: " + remoteBlob.getCacheControl())); assertTrue(snippetOutput.contains("ComponentCount: " + remoteBlob.getComponentCount())); assertTrue(snippetOutput.contains("ContentDisposition: " + remoteBlob.getContentDisposition())); assertTrue(snippetOutput.contains("ContentEncoding: " + remoteBlob.getContentEncoding())); assertTrue(snippetOutput.contains("ContentLanguage: " + remoteBlob.getContentLanguage())); assertTrue(snippetOutput.contains("ContentType: " + remoteBlob.getContentType())); assertTrue(snippetOutput.contains("Crc32c: " + remoteBlob.getCrc32c())); assertTrue(snippetOutput.contains("Crc32cHexString: " + remoteBlob.getCrc32cToHexString())); assertTrue(snippetOutput.contains("ETag: " + remoteBlob.getEtag())); assertTrue(snippetOutput.contains("Generation: " + remoteBlob.getGeneration())); assertTrue(snippetOutput.contains("Id: " + remoteBlob.getBlobId())); assertTrue(snippetOutput.contains("KmsKeyName: " + remoteBlob.getKmsKeyName())); assertTrue(snippetOutput.contains("Md5Hash: " + remoteBlob.getMd5())); assertTrue(snippetOutput.contains("Md5HexString: " + remoteBlob.getMd5ToHexString())); assertTrue(snippetOutput.contains("MediaLink: " + remoteBlob.getMediaLink())); assertTrue(snippetOutput.contains("Metageneration: " + remoteBlob.getMetageneration())); assertTrue(snippetOutput.contains("Name: " + remoteBlob.getName())); assertTrue(snippetOutput.contains("Size: " + remoteBlob.getSize())); assertTrue(snippetOutput.contains("StorageClass: " + remoteBlob.getStorageClass())); assertTrue(snippetOutput.contains("TimeCreated: " + new Date(remoteBlob.getCreateTime()))); assertTrue( snippetOutput.contains("Last Metadata Update: " + new Date(remoteBlob.getUpdateTime()))); assertTrue(snippetOutput.contains("temporaryHold: disabled")); assertTrue(snippetOutput.contains("eventBasedHold: disabled")); assertTrue(snippetOutput.contains("User metadata:")); assertTrue(snippetOutput.contains("k=v")); }
Example 20
Source File: FetchGCSObject.java From nifi with Apache License 2.0 | 4 votes |
@Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { FlowFile flowFile = session.get(); if (flowFile == null) { return; } final long startNanos = System.nanoTime(); final String bucketName = context.getProperty(BUCKET).evaluateAttributeExpressions(flowFile).getValue(); final String key = context.getProperty(KEY).evaluateAttributeExpressions(flowFile).getValue(); final Long generation = context.getProperty(GENERATION).evaluateAttributeExpressions(flowFile).asLong(); final String encryptionKey = context.getProperty(ENCRYPTION_KEY).evaluateAttributeExpressions(flowFile).getValue(); final Storage storage = getCloudService(); final BlobId blobId = BlobId.of(bucketName, key, generation); try { final List<Storage.BlobSourceOption> blobSourceOptions = new ArrayList<>(2); if (encryptionKey != null) { blobSourceOptions.add(Storage.BlobSourceOption.decryptionKey(encryptionKey)); } if (generation != null) { blobSourceOptions.add(Storage.BlobSourceOption.generationMatch()); } final Blob blob = storage.get(blobId); if (blob == null) { throw new StorageException(404, "Blob " + blobId + " not found"); } final ReadChannel reader = storage.reader(blobId, blobSourceOptions.toArray(new Storage.BlobSourceOption[0])); flowFile = session.importFrom(Channels.newInputStream(reader), flowFile); final Map<String, String> attributes = StorageAttributes.createAttributes(blob); flowFile = session.putAllAttributes(flowFile, attributes); } catch (StorageException e) { getLogger().error("Failed to fetch GCS Object due to {}", new Object[] {e}, e); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); return; } session.transfer(flowFile, REL_SUCCESS); final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); getLogger().info("Successfully retrieved GCS Object for {} in {} millis; routing to success", new Object[]{flowFile, millis}); session.getProvenanceReporter().fetch(flowFile, "https://" + bucketName + ".storage.googleapis.com/" + key, millis); }