com.google.cloud.storage.BlobInfo Java Examples

The following examples show how to use com.google.cloud.storage.BlobInfo. 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: ImportProductSetsIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  // Create the product set csv file locally and upload it to GCS
  // This is so that there is a unique product set ID for all java version tests.
  Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService();
  BlobId blobId = BlobId.of(PROJECT_ID, FILEPATH);
  BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
  String csvContents =
      "\"gs://cloud-samples-data/vision/product_search/shoes_1.jpg\","
          + String.format("\"%s\",", IMAGE_URI_1)
          + String.format("\"%s\",", PRODUCT_SET_ID)
          + String.format("\"%s\",", PRODUCT_ID_1)
          + "\"apparel\",,\"style=womens\",\"0.1,0.1,0.9,0.1,0.9,0.9,0.1,0.9\"";
  blob = storage.create(blobInfo, csvContents.getBytes());

  bout = new ByteArrayOutputStream();
  out = new PrintStream(bout);
  System.setOut(out);
}
 
Example #2
Source File: GCPBackuper.java    From cassandra-backup with Apache License 2.0 6 votes vote down vote up
@Override
public FreshenResult freshenRemoteObject(final RemoteObjectReference object) {
    final BlobId blobId = ((GCPRemoteObjectReference) object).blobId;

    try {
        storage.copy(new Storage.CopyRequest.Builder()
                         .setSource(blobId)
                         .setTarget(BlobInfo.newBuilder(blobId).build(), Storage.BlobTargetOption.predefinedAcl(BUCKET_OWNER_FULL_CONTROL))
                         .build());

        return FreshenResult.FRESHENED;
    } catch (final StorageException e) {
        if (e.getCode() != 404) {
            throw e;
        }

        return FreshenResult.UPLOAD_REQUIRED;
    }
}
 
Example #3
Source File: GcsSinkTask.java    From aiven-kafka-connect-gcs with GNU Affero General Public License v3.0 6 votes vote down vote up
private void flushFile(final String filename, final List<SinkRecord> records) {
    final BlobInfo blob = BlobInfo
        .newBuilder(config.getBucketName(), config.getPrefix() + filename)
        .build();

    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        // Don't group these two tries,
        // because the internal one must be closed before writing to GCS.
        try (final OutputStream compressedStream = getCompressedStream(baos)) {
            for (int i = 0; i < records.size() - 1; i++) {
                outputWriter.writeRecord(records.get(i), compressedStream);
            }
            outputWriter.writeLastRecord(records.get(records.size() - 1), compressedStream);
        }
        storage.create(blob, baos.toByteArray());
    } catch (final Exception e) {
        throw new ConnectException(e);
    }
}
 
Example #4
Source File: GCSwriterTest.java    From beast with Apache License 2.0 6 votes vote down vote up
@Test (expected = BQErrorHandlerException.class)
public void testStoreThrowsBQHandlerExceptionAsExpected() throws InvalidProtocolBufferException {
    TestMessage tMsg = getTestMessage("mock-gcs-writer1", Instant.now());
    ColumnMapping columnMapping = new ColumnMapping();
    columnMapping.put("1", "order_number");
    columnMapping.put("2", "order_url");
    columnMapping.put("3", "order_details");
    columnMapping.put("4", "created_at");
    when(gcsStoreMock.create(any(BlobInfo.class), any(byte[].class))).thenThrow(StorageException.class);
    List<Record> records = getKafkaConsumerRecords(columnMapping, Instant.now(), "test-mock", 1, 2,
            clock, tMsg);
    try {
        Status status = errorWriter.writeErrorRecords(records);
    } catch (BQErrorHandlerException bqee) {
        throw bqee;
    }
    fail("expected BQErrorhandlerexception");
}
 
Example #5
Source File: CloudStorageHelper.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
/**
 * Uploads a file to Google Cloud Storage to the bucket specified in the BUCKET_NAME
 * environment variable, appending a timestamp to end of the uploaded filename.
 */
@SuppressWarnings("deprecation")
public String uploadFile(Part filePart, final String bucketName) throws IOException {
  DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  String dtString = dt.toString(dtf);
  final String fileName = filePart.getSubmittedFileName() + dtString;

  // the inputstream is closed by default, so we don't need to close it here
  BlobInfo blobInfo =
      storage.create(
          BlobInfo
              .newBuilder(bucketName, fileName)
              // Modify access list to allow all users with link to read file
              .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
              .build(),
          filePart.getInputStream());
  logger.log(
      Level.INFO, "Uploaded file {0} as {1}", new Object[]{
          filePart.getSubmittedFileName(), fileName});
  // return the public download link
  return blobInfo.getMediaLink();
}
 
Example #6
Source File: ITBlobSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testMoveObject() {
  String blob = "movethisblob";
  storage.create(BlobInfo.newBuilder(BlobId.of(BUCKET, blob)).build());
  assertNotNull(storage.get(BUCKET, blob));
  String newBucket = RemoteStorageHelper.generateBucketName();
  storage.create(BucketInfo.newBuilder(newBucket).build());
  try {
    MoveObject.moveObject(PROJECT_ID, BUCKET, blob, newBucket);
    assertNotNull(storage.get(newBucket, blob));
    assertNull(storage.get(BUCKET, blob));
  } finally {
    storage.delete(newBucket, blob);
    storage.delete(newBucket);
  }
}
 
Example #7
Source File: CloudStorageHelper.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
/**
 * Uploads a file to Google Cloud Storage to the bucket specified in the BUCKET_NAME environment
 * variable, appending a timestamp to end of the uploaded filename.
 */
public String uploadFile(FileItemStream fileStream, final String bucketName)
    throws IOException, ServletException {
  checkFileExtension(fileStream.getName());

  System.out.println("FileStream name: " + fileStream.getName() + "\nBucket name: " + bucketName);

  DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  String dtString = dt.toString(dtf);
  final String fileName = fileStream.getName() + dtString;

  // the inputstream is closed by default, so we don't need to close it here
  @SuppressWarnings("deprecation")
  BlobInfo blobInfo =
      storage.create(
          BlobInfo.newBuilder(bucketName, fileName)
              // Modify access list to allow all users with link to read file
              .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
              .build(),
          fileStream.openStream());
  logger.log(
      Level.INFO, "Uploaded file {0} as {1}", new Object[] {fileStream.getName(), fileName});
  // return the public download link
  return blobInfo.getMediaLink();
}
 
Example #8
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to set event-based hold for a blob */
public Blob setEventBasedHold(String bucketName, String blobName) throws StorageException {
  // [START storage_set_event_based_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).setEventBasedHold(true).build());

  System.out.println("Event-based hold was set for " + blobName);
  // [END storage_set_event_based_hold]
  return blob;
}
 
Example #9
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/**
 * Example of creating a signed URL passing the {@link
 * SignUrlOption#signWith(ServiceAccountSigner)} option, that will be used for signing the URL.
 */
// [TARGET signUrl(BlobInfo, long, TimeUnit, SignUrlOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE "/path/to/key.json"]
public URL signUrlWithSigner(String bucketName, String blobName, String keyPath)
    throws IOException {
  // [START signUrlWithSigner]
  URL signedUrl =
      storage.signUrl(
          BlobInfo.newBuilder(bucketName, blobName).build(),
          14,
          TimeUnit.DAYS,
          SignUrlOption.signWith(
              ServiceAccountCredentials.fromStream(new FileInputStream(keyPath))));
  // [END signUrlWithSigner]
  return signedUrl;
}
 
Example #10
Source File: PutGCSObjectTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testAclAttributeDomain() throws Exception {
    reset(storage, blob);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();

    when(storage.create(any(BlobInfo.class), any(InputStream.class), any(Storage.BlobWriteOption.class)))
            .thenReturn(blob);

    final Acl.Domain mockDomain = mock(Acl.Domain.class);
    when(mockDomain.getDomain()).thenReturn(OWNER_DOMAIN);
    when(blob.getOwner()).thenReturn(mockDomain);

    runner.enqueue("test");
    runner.run();

    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_SUCCESS);
    runner.assertTransferCount(PutGCSObject.REL_SUCCESS, 1);

    final MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(PutGCSObject.REL_SUCCESS).get(0);
    mockFlowFile.assertAttributeEquals(OWNER_ATTR, OWNER_DOMAIN);
    mockFlowFile.assertAttributeEquals(OWNER_TYPE_ATTR, "domain");
}
 
Example #11
Source File: GcsRepositoryTest.java    From hawkbit-extensions with Eclipse Public License 1.0 6 votes vote down vote up
@Test
@Description("Verifies that the gcs storage client is called to put the object to GCS with the correct inputstream and meta-data")
public void storeInputStreamCallGcsStorageClient() throws IOException {
    final byte[] rndBytes = randomBytes();
    final String knownContentType = "application/octet-stream";

    when(gcsStorageMock.get(anyString(), anyString())).thenReturn(gcpObjectMock);
    when(gcsStorageMock.create(any(), (byte[]) any())).thenReturn(putObjectResultMock);

    // test
    storeRandomBytes(rndBytes, knownContentType);

    // verify
    Mockito.verify(gcsStorageMock).create(blobCaptor.capture(), inputStreamCaptor.capture());

    final BlobInfo recordedObjectMetadata = blobCaptor.getValue();
    assertThat(recordedObjectMetadata.getContentType()).isEqualTo(knownContentType);
    assertThat(recordedObjectMetadata.getMd5()).isNotNull();
}
 
Example #12
Source File: QuickStartIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
private static final void deleteObjects() {
  Storage storage = StorageOptions.getDefaultInstance().getService();
  for (BlobInfo info :
      storage
          .list(
              bucketName,
              BlobListOption.versions(true),
              BlobListOption.currentDirectory(),
              BlobListOption.prefix(path + "/"))
          .getValues()) {
    storage.delete(info.getBlobId());
  }
}
 
Example #13
Source File: PutGCSObjectTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testAclAttributeUser() throws Exception {
    reset(storage, blob);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();

    when(storage.create(any(BlobInfo.class), any(InputStream.class), any(Storage.BlobWriteOption.class)))
            .thenReturn(blob);

    final Acl.User mockUser = mock(Acl.User.class);
    when(mockUser.getEmail()).thenReturn(OWNER_USER_EMAIL);
    when(blob.getOwner()).thenReturn(mockUser);

    runner.enqueue("test");
    runner.run();

    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_SUCCESS);
    runner.assertTransferCount(PutGCSObject.REL_SUCCESS, 1);

    final MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(PutGCSObject.REL_SUCCESS).get(0);
    mockFlowFile.assertAttributeEquals(OWNER_ATTR, OWNER_USER_EMAIL);
    mockFlowFile.assertAttributeEquals(OWNER_TYPE_ATTR, "user");
}
 
Example #14
Source File: PutGCSObjectTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testAclAttributeDomain() throws Exception {
    reset(storage, blob);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();

    when(storage.create(any(BlobInfo.class), any(InputStream.class), any(Storage.BlobWriteOption.class)))
            .thenReturn(blob);

    final Acl.Domain mockDomain = mock(Acl.Domain.class);
    when(mockDomain.getDomain()).thenReturn(OWNER_DOMAIN);
    when(blob.getOwner()).thenReturn(mockDomain);

    runner.enqueue("test");
    runner.run();

    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_SUCCESS);
    runner.assertTransferCount(PutGCSObject.REL_SUCCESS, 1);

    final MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(PutGCSObject.REL_SUCCESS).get(0);
    mockFlowFile.assertAttributeEquals(OWNER_ATTR, OWNER_DOMAIN);
    mockFlowFile.assertAttributeEquals(OWNER_TYPE_ATTR, "domain");
}
 
Example #15
Source File: CloudStorageHelper.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
/**
 * Uploads a file to Google Cloud Storage to the bucket specified in the BUCKET_NAME
 * environment variable, appending a timestamp to end of the uploaded filename.
 */
public String uploadFile(FileItemStream fileStream, final String bucketName)
    throws IOException, ServletException {
  checkFileExtension(fileStream.getName());

  DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  String dtString = dt.toString(dtf);
  final String fileName = fileStream.getName() + dtString;

  // the inputstream is closed by default, so we don't need to close it here
  @SuppressWarnings("deprecation")
  BlobInfo blobInfo =
      storage.create(
          BlobInfo
              .newBuilder(bucketName, fileName)
              // Modify access list to allow all users with link to read file
              .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
              .build(),
          fileStream.openStream());
  logger.log(Level.INFO, "Uploaded file {0} as {1}", new Object[]{
      fileStream.getName(), fileName});
  // return the public download link
  return blobInfo.getMediaLink();
}
 
Example #16
Source File: PutGCSObjectTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testAclAttributeProject() throws Exception {
    reset(storage, blob);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();

    when(storage.create(any(BlobInfo.class), any(InputStream.class), any(Storage.BlobWriteOption.class)))
            .thenReturn(blob);

    final Acl.Project mockProject = mock(Acl.Project.class);
    when(mockProject.getProjectId()).thenReturn(OWNER_PROJECT_ID);
    when(blob.getOwner()).thenReturn(mockProject);

    runner.enqueue("test");
    runner.run();

    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_SUCCESS);
    runner.assertTransferCount(PutGCSObject.REL_SUCCESS, 1);

    final MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(PutGCSObject.REL_SUCCESS).get(0);
    mockFlowFile.assertAttributeEquals(OWNER_ATTR, OWNER_PROJECT_ID);
    mockFlowFile.assertAttributeEquals(OWNER_TYPE_ATTR, "project");
}
 
Example #17
Source File: PutGCSObjectTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testAclAttributeUser() throws Exception {
    reset(storage, blob);
    final PutGCSObject processor = getProcessor();
    final TestRunner runner = buildNewRunner(processor);
    addRequiredPropertiesToRunner(runner);
    runner.assertValid();

    when(storage.create(any(BlobInfo.class), any(InputStream.class), any(Storage.BlobWriteOption.class)))
            .thenReturn(blob);

    final Acl.User mockUser = mock(Acl.User.class);
    when(mockUser.getEmail()).thenReturn(OWNER_USER_EMAIL);
    when(blob.getOwner()).thenReturn(mockUser);

    runner.enqueue("test");
    runner.run();

    runner.assertAllFlowFilesTransferred(PutGCSObject.REL_SUCCESS);
    runner.assertTransferCount(PutGCSObject.REL_SUCCESS, 1);

    final MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(PutGCSObject.REL_SUCCESS).get(0);
    mockFlowFile.assertAttributeEquals(OWNER_ATTR, OWNER_USER_EMAIL);
    mockFlowFile.assertAttributeEquals(OWNER_TYPE_ATTR, "user");
}
 
Example #18
Source File: GcsSpringIntegrationTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilePropagatedToLocalDirectory() {
	BlobId blobId = BlobId.of(this.cloudInputBucket, TEST_FILE_NAME);
	BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
	this.storage.create(blobInfo, "Hello World!".getBytes(StandardCharsets.UTF_8));

	Awaitility.await().atMost(15, TimeUnit.SECONDS)
			.untilAsserted(() -> {
				Path outputFile = Paths.get(this.outputFolder + "/" + TEST_FILE_NAME);
				assertThat(Files.exists(outputFile)).isTrue();
				assertThat(Files.isRegularFile(outputFile)).isTrue();

				String firstLine = Files.lines(outputFile).findFirst().get();
				assertThat(firstLine).isEqualTo("Hello World!");

				List<String> blobNamesInOutputBucket = new ArrayList<>();
				this.storage.list(this.cloudOutputBucket).iterateAll()
						.forEach((b) -> blobNamesInOutputBucket.add(b.getName()));

				assertThat(blobNamesInOutputBucket).contains(TEST_FILE_NAME);
			});
}
 
Example #19
Source File: GenerateV4GetObjectSignedUrl.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/**
 * Signing a URL requires Credentials which implement ServiceAccountSigner. These can be set
 * explicitly using the Storage.SignUrlOption.signWith(ServiceAccountSigner) option. If you don't,
 * you could also pass a service account signer to StorageOptions, i.e.
 * StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
 * neither of these options are used, which means the following code only works when the
 * credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
 * credentials are authorized to sign a URL. See the documentation for Storage.signUrl for more
 * details.
 */
public static void generateV4GetObjectSignedUrl(
    String projectId, String bucketName, String objectName) throws StorageException {
  // String projectId = "my-project-id";
  // String bucketName = "my-bucket";
  // String objectName = "my-object";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

  // Define resource
  BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build();

  URL url =
      storage.signUrl(blobInfo, 15, TimeUnit.MINUTES, Storage.SignUrlOption.withV4Signature());

  System.out.println("Generated GET signed URL:");
  System.out.println(url);
  System.out.println("You can use this URL with any user agent, for example:");
  System.out.println("curl '" + url + "'");
}
 
Example #20
Source File: GoogleCloudStorage.java    From metastore with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ByteString payload) {
  try (Scope scope =
      TRACER.spanBuilder("GoogleCloudStorage.write").setRecordEvents(true).startScopedSpan()) {
    storage.create(BlobInfo.newBuilder(bucket, fileName).build(), payload.toByteArray());
  }
}
 
Example #21
Source File: ITBlobSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMakeObjectPublic() {
  String aclBlob = "acl-test-blob";
  assertNull(
      storage.create(BlobInfo.newBuilder(BUCKET, aclBlob).build()).getAcl(Acl.User.ofAllUsers()));
  MakeObjectPublic.makeObjectPublic(PROJECT_ID, BUCKET, aclBlob);
  assertNotNull(storage.get(BUCKET, aclBlob).getAcl(Acl.User.ofAllUsers()));
}
 
Example #22
Source File: ITStorageSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadWriteAndSignUrl() throws IOException {
  String blobName = "text-read-write-sign-url";
  byte[] content = "Hello, World!".getBytes(UTF_8);
  Blob blob = storage.create(BlobInfo.newBuilder(BUCKET, blobName).build(), content);
  assertArrayEquals(
      content, storageSnippets.readBlobFromId(BUCKET, blobName, blob.getGeneration()));
  assertArrayEquals(
      content,
      storageSnippets.readBlobFromStringsWithGeneration(BUCKET, blobName, blob.getGeneration()));
  storageSnippets.readerFromId(BUCKET, blobName);
  storageSnippets.readerFromStrings(BUCKET, blobName);
  storageSnippets.writer(BUCKET, blobName);
  URL signedUrl = storageSnippets.signUrl(BUCKET, blobName);
  URLConnection connection = signedUrl.openConnection();
  byte[] readBytes = new byte[content.length];
  try (InputStream responseStream = connection.getInputStream()) {
    assertEquals(content.length, responseStream.read(readBytes));
    assertArrayEquals(content, readBytes);
  }
  signedUrl =
      storageSnippets.signUrlWithSigner(
          BUCKET, blobName, System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
  connection = signedUrl.openConnection();
  try (InputStream responseStream = connection.getInputStream()) {
    assertEquals(content.length, responseStream.read(readBytes));
    assertArrayEquals(content, readBytes);
  }
  blob.delete();
}
 
Example #23
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #24
Source File: BucketUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Take a GCS path and return a signed url to the same resource which allows unauthenticated users to access the file.
 * @param path String representing a GCS path
 * @param hoursToLive how long in hours the url will remain valid
 * @return A signed url which provides access to the bucket location over http allowing unauthenticated users to access it
 */
public static String createSignedUrlToGcsObject(String path, final long hoursToLive) {
    final Storage storage = StorageOptions.getDefaultInstance().getService();
    final BlobInfo info = BlobInfo.newBuilder(getBucket(path), getPathWithoutBucket(path)).build();
    final URL signedUrl = storage.signUrl(info, hoursToLive, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.GET));
    return signedUrl.toString();
}
 
Example #25
Source File: GoogleStorageTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testWritableOutputStreamWithAutoCreateOnNonExistantBlob()
		throws Exception {
	String location = "gs://test-spring/test";
	BlobId blobId = BlobId.of("test-spring", "test");
	Blob nonExistantBlob = mock(Blob.class);
	when(nonExistantBlob.exists()).thenReturn(false);
	when(this.mockStorage.get(blobId)).thenReturn(nonExistantBlob);
	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 #26
Source File: FirebaseStorageService.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
/**
 * Uploads a file to Firebase Storage
 * @param path Path at which the file is to be stored on Firebase
 * @param contentType Content-Type of file - Helps Firebase to set meta data on file
 * @param content byte array of file to be stored
 * @return Public facing URL of the uploaded file
 * @throws UnsupportedEncodingException if the file type is unsupported
 */
public CompletableFuture<Optional<String>> uploadFile(String path, String contentType, byte[] content) throws UnsupportedEncodingException {
    if (!firebaseService.canProceed())
        return CompletableFuture.completedFuture(Optional.empty());

    log.debug("Uploading file '{}' of type {}...", path, contentType);
    Bucket bucket = firebaseService.getBucket();
    log.debug("Bucket used : " + bucket.getName());
    String uuid = UUID.randomUUID().toString();
    log.info("Firebase Download Token : {}", uuid);
    Map<String, String> map = new HashMap<>();
    map.put("firebaseStorageDownloadTokens", uuid);

    BlobInfo uploadContent = BlobInfo.newBuilder(bucket.getName(), path)
            .setContentType(contentType)
            .setMetadata(map)
            .setAcl(Collections.singletonList(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER)))
            .build();
    BlobInfo uploaded = bucket.getStorage().create(uploadContent, content);

    log.debug("File Uploaded");
    log.debug("Media Link : {}", uploaded.getMediaLink());
    log.debug("Metadata : {}", uploaded.getMetadata().toString());

    String link = String.format("https://firebasestorage.googleapis.com/v0/b/%s/o/%s?alt=media&auth=%s",
            uploaded.getBucket(), URLEncoder.encode(uploaded.getName(), "UTF-8"), uuid);
    log.info("Firebase Link : {}", link);

    return CompletableFuture.completedFuture(Optional.of(link));
}
 
Example #27
Source File: GoogleStorageTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateBlobWithContents() {
	Storage mockStorage = mock(Storage.class);
	String location = "gs://test-bucket/filename";
	byte[] contentBytes = "test contents".getBytes();

	GoogleStorageResource resource = new GoogleStorageResource(mockStorage, location);
	resource.createBlob(contentBytes);

	verify(mockStorage).create(
			BlobInfo.newBuilder("test-bucket", "filename").build(),
			contentBytes);
}
 
Example #28
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
/** Example of creating a blob with sub array from a byte array. */
// [TARGET create(BlobInfo, byte[], offset, length, BlobTargetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob createBlobWithSubArrayFromByteArray(
    String bucketName, String blobName, int offset, int length) {
  // [START createBlobWithSubArrayFromByteArray]
  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), offset, length);
  // [END createBlobWithSubArrayFromByteArray]
  return blob;
}
 
Example #29
Source File: FirestoreProtoClient.java    From startup-os with Apache License 2.0 5 votes vote down vote up
public String uploadTo(String bucketName, String filePath, String fileName) throws IOException {

    BlobInfo blobInfo =
        storage.create(
            BlobInfo.newBuilder(bucketName, fileName)
                .setAcl(ImmutableList.of(Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER)))
                .build(),
            Files.toByteArray(Paths.get(filePath).toFile()));
    return blobInfo.getMediaLink();
  }
 
Example #30
Source File: GoogleStorageTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testWritable() throws Exception {
	WriteChannel writeChannel = mock(WriteChannel.class);
	when(this.mockStorage.writer(any(BlobInfo.class))).thenReturn(writeChannel);

	Assert.assertTrue(this.remoteResource instanceof WritableResource);
	WritableResource writableResource = (WritableResource) this.remoteResource;
	Assert.assertTrue(writableResource.isWritable());
	writableResource.getOutputStream();
}