Java Code Examples for com.google.cloud.storage.Bucket#create()

The following examples show how to use com.google.cloud.storage.Bucket#create() . 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: ITBucketSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequesterPays() throws Exception {
  EnableRequesterPays.enableRequesterPays(PROJECT_ID, BUCKET);
  Bucket bucket = storage.get(BUCKET);
  assertTrue(bucket.requesterPays());
  String projectId = ServiceOptions.getDefaultProjectId();
  String blobName = "test-create-empty-blob-requester-pays";
  byte[] content = {0xD, 0xE, 0xA, 0xD};
  Blob remoteBlob =
      bucket.create(blobName, content, Bucket.BlobTargetOption.userProject(projectId));
  assertNotNull(remoteBlob);
  DownloadRequesterPaysObject.downloadRequesterPaysObject(
      projectId, BUCKET, blobName, Paths.get(blobName));
  byte[] readBytes = Files.readAllBytes(Paths.get(blobName));
  assertArrayEquals(content, readBytes);
  DisableRequesterPays.disableRequesterPays(PROJECT_ID, BUCKET);
  assertFalse(storage.get(BUCKET).requesterPays());
}
 
Example 2
Source File: CreateAndListBucketsAndBlobs.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) {
  // Create a service object
  // Credentials are inferred from the environment.
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // Create a bucket
  String bucketName = "my_unique_bucket"; // Change this to something unique
  Bucket bucket = storage.create(BucketInfo.of(bucketName));

  // Upload a blob to the newly created bucket
  Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");

  // Read the blob content from the server
  String blobContent = new String(blob.getContent(), UTF_8);

  // List all your buckets
  System.out.println("My buckets:");
  for (Bucket currentBucket : storage.list().iterateAll()) {
    System.out.println(currentBucket);
  }

  // List the blobs in a particular bucket
  System.out.println("My blobs:");
  for (Blob currentBlob : bucket.list().iterateAll()) {
    System.out.println(currentBlob);
  }
}
 
Example 3
Source File: ITStorageSnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBucketRetention() {
  Bucket bucket = storageSnippets.setRetentionPolicy(BUCKET, RETENTION_PERIOD);
  assertEquals(bucket.getRetentionPeriod(), RETENTION_PERIOD);
  assertNotNull(bucket.getRetentionEffectiveTime());
  bucket = storageSnippets.getRetentionPolicy(BUCKET);
  assertEquals(bucket.getRetentionPeriod(), RETENTION_PERIOD);
  assertNotNull(bucket.getRetentionEffectiveTime());
  assertNull(bucket.retentionPolicyIsLocked());
  bucket = storageSnippets.enableDefaultEventBasedHold(BUCKET);
  assertTrue(bucket.getDefaultEventBasedHold());
  bucket = storageSnippets.getDefaultEventBasedHold(BUCKET);
  assertTrue(bucket.getDefaultEventBasedHold());
  String blobName = "test-create-empty-blob-retention-policy";
  Blob remoteBlob = bucket.create(blobName, BLOB_BYTE_CONTENT);
  assertTrue(remoteBlob.getEventBasedHold());
  remoteBlob = storageSnippets.setEventBasedHold(BUCKET, blobName);
  assertTrue(remoteBlob.getEventBasedHold());
  remoteBlob = storageSnippets.releaseEventBasedHold(BUCKET, blobName);
  assertFalse(remoteBlob.getEventBasedHold());
  assertNotNull(remoteBlob.getRetentionExpirationTime());
  bucket = storageSnippets.removeRetentionPolicy(BUCKET);
  assertNull(bucket.getRetentionPeriod());
  assertNull(bucket.getRetentionEffectiveTime());
  bucket = storageSnippets.disableDefaultEventBasedHold(BUCKET);
  assertFalse(bucket.getDefaultEventBasedHold());
  remoteBlob = storageSnippets.setTemporaryHold(BUCKET, blobName);
  assertTrue(remoteBlob.getTemporaryHold());
  remoteBlob = storageSnippets.releaseTemporaryHold(BUCKET, blobName);
  assertFalse(remoteBlob.getTemporaryHold());
}
 
Example 4
Source File: GcsPinotFS.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Override
public void copyFromLocalFile(File srcFile, URI dstUri) throws Exception {
  LOGGER.info("Copying file {} to uri {}", srcFile.getAbsolutePath(), dstUri);
  Bucket bucket = getBucket(dstUri);
  Blob blob = bucket.create(sanitizePath(dstUri.getPath()), new byte[0]);
  WriteChannel writeChannel = blob.writer();
  writeChannel.setChunkSize(BUFFER_SIZE);
  ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
  SeekableByteChannel channel = Files.newByteChannel(srcFile.toPath());
  for (int bytesRead = channel.read(buffer); bytesRead != -1; bytesRead = channel.read(buffer)) {
    buffer.flip();
    writeChannel.write(buffer);
    buffer.clear();
  }
  writeChannel.close();
}
 
Example 5
Source File: StorageClientIT.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
private Blob createTextBlob(Bucket bucket, String contents) {
  String fileName = "data_" + System.currentTimeMillis() + ".txt";
  return bucket.create(fileName, contents.getBytes(), "text/plain");
}