com.google.cloud.storage.Bucket.create()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(12.6k)|赞(0)|评价(0)|浏览(154)

本文整理了Java中com.google.cloud.storage.Bucket.create()方法的一些代码示例,展示了Bucket.create()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bucket.create()方法的具体详情如下:
包路径:com.google.cloud.storage.Bucket
类名称:Bucket
方法名:create

Bucket.create介绍

[英]Creates a new blob in this bucket. Direct upload is used to upload content. For large content, Blob#writer(com.google.cloud.storage.Storage.BlobWriteOption...) is recommended as it uses resumable upload.

Example of creating a blob in the bucket from an input stream with a content type.

String blobName = "my_blob_name";

[中]

代码示例

代码示例来源:origin: googleapis/google-cloud-java

/** Example of creating a blob in the bucket from a byte array. */
// [TARGET create(String, byte[], BlobTargetOption...)]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromByteArray(String blobName) {
 // [START createBlobFromByteArray]
 Blob blob = bucket.create(blobName, "Hello, World!".getBytes(UTF_8));
 // [END createBlobFromByteArray]
 return blob;
}

代码示例来源:origin: googleapis/google-cloud-java

/** Example of creating a blob in the bucket from a byte array with a content type. */
// [TARGET create(String, byte[], String, BlobTargetOption...)]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromByteArrayWithContentType(String blobName) {
 // [START createBlobFromByteArrayWithContentType]
 Blob blob = bucket.create(blobName, "Hello, World!".getBytes(UTF_8), "text/plain");
 // [END createBlobFromByteArrayWithContentType]
 return blob;
}

代码示例来源:origin: googleapis/google-cloud-java

/** Example of creating a blob in the bucket from an input stream. */
// [TARGET create(String, InputStream, BlobWriteOption...)]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromInputStream(String blobName) {
 // [START createBlobFromInputStream]
 InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
 Blob blob = bucket.create(blobName, content);
 // [END createBlobFromInputStream]
 return blob;
}

代码示例来源:origin: googleapis/google-cloud-java

/** Example of creating a blob in the bucket from an input stream with a content type. */
// [TARGET create(String, InputStream, String, BlobWriteOption...)]
// [VARIABLE "my_blob_name"]
public Blob createBlobFromInputStreamWithContentType(String blobName) {
 // [START createBlobFromInputStreamWithContentType]
 InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
 Blob blob = bucket.create(blobName, content, "text/plain");
 // [END createBlobFromInputStreamWithContentType]
 return blob;
}

代码示例来源:origin: google/data-transfer-project

Blob create(UUID jobId, String keyName, InputStream inputStream) {
 String blobName = getDataKeyName(jobId, keyName);
 // TODO: use result to determine success/failure
 return bucket.create(blobName, inputStream);
}

代码示例来源:origin: googleapis/google-cloud-java

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);
  }
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@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);
}

代码示例来源:origin: googleapis/google-cloud-java

@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);
}

代码示例来源:origin: googleapis/google-cloud-java

@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);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreate() 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};
 expect(storage.getOptions()).andReturn(mockOptions);
 expect(storage.create(info, content)).andReturn(expectedBlob);
 replay(storage);
 initializeBucket();
 Blob blob = bucket.create("n", content, CONTENT_TYPE);
 assertEquals(expectedBlob, blob);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateFromStreamWithWrongMetagenerationOptions() throws Exception {
 initializeExpectedBucket(4);
 expect(storage.getOptions()).andReturn(mockOptions);
 replay(storage);
 initializeBucket();
 byte[] content = {0xD, 0xE, 0xA, 0xD};
 InputStream streamContent = new ByteArrayInputStream(content);
 thrown.expect(IllegalArgumentException.class);
 thrown.expectMessage(
   "metagenerationMatch and metagenerationNotMatch options can not be both provided");
 bucket.create(
   "n",
   streamContent,
   CONTENT_TYPE,
   Bucket.BlobWriteOption.metagenerationMatch(42L),
   Bucket.BlobWriteOption.metagenerationNotMatch(24L));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateFromStreamWithWrongGenerationOptions() throws Exception {
 initializeExpectedBucket(4);
 expect(storage.getOptions()).andReturn(mockOptions);
 replay(storage);
 initializeBucket();
 byte[] content = {0xD, 0xE, 0xA, 0xD};
 InputStream streamContent = new ByteArrayInputStream(content);
 thrown.expect(IllegalArgumentException.class);
 thrown.expectMessage(
   "Only one option of generationMatch, doesNotExist or generationNotMatch can be provided");
 bucket.create(
   "n",
   streamContent,
   CONTENT_TYPE,
   Bucket.BlobWriteOption.generationMatch(42L),
   Bucket.BlobWriteOption.generationNotMatch(24L));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateWithWrongMetagenerationOptions() throws Exception {
 initializeExpectedBucket(4);
 expect(storage.getOptions()).andReturn(mockOptions);
 replay(storage);
 initializeBucket();
 byte[] content = {0xD, 0xE, 0xA, 0xD};
 thrown.expect(IllegalArgumentException.class);
 thrown.expectMessage(
   "metagenerationMatch and metagenerationNotMatch options can not be both provided");
 bucket.create(
   "n",
   content,
   CONTENT_TYPE,
   Bucket.BlobTargetOption.metagenerationMatch(42L),
   Bucket.BlobTargetOption.metagenerationNotMatch(24L));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateWithWrongGenerationOptions() throws Exception {
 initializeExpectedBucket(4);
 expect(storage.getOptions()).andReturn(mockOptions);
 replay(storage);
 initializeBucket();
 byte[] content = {0xD, 0xE, 0xA, 0xD};
 thrown.expect(IllegalArgumentException.class);
 thrown.expectMessage(
   "Only one option of generationMatch, doesNotExist or generationNotMatch can be provided");
 bucket.create(
   "n",
   content,
   CONTENT_TYPE,
   Bucket.BlobTargetOption.generationMatch(42L),
   Bucket.BlobTargetOption.generationNotMatch(24L));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateFromStreamNotExists() throws Exception {
 initializeExpectedBucket(5);
 BlobInfo info =
   BlobInfo.newBuilder(BlobId.of("b", "n", 0L)).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, Storage.BlobWriteOption.generationMatch()))
   .andReturn(expectedBlob);
 replay(storage);
 initializeBucket();
 Blob blob =
   bucket.create("n", streamContent, CONTENT_TYPE, Bucket.BlobWriteOption.doesNotExist());
 assertEquals(expectedBlob, blob);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateFromStreamWithEncryptionKey() throws Exception {
 initializeExpectedBucket(5);
 BlobInfo info = BlobInfo.newBuilder(BlobId.of("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, Storage.BlobWriteOption.encryptionKey(KEY)))
   .andReturn(expectedBlob);
 replay(storage);
 initializeBucket();
 Blob blob =
   bucket.create("n", streamContent, CONTENT_TYPE, Bucket.BlobWriteOption.encryptionKey(KEY));
 assertEquals(expectedBlob, blob);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateWithEncryptionKey() throws Exception {
 initializeExpectedBucket(5);
 BlobInfo info = BlobInfo.newBuilder(BlobId.of("b", "n")).setContentType(CONTENT_TYPE).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, Storage.BlobTargetOption.encryptionKey(KEY)))
   .andReturn(expectedBlob);
 replay(storage);
 initializeBucket();
 Blob blob =
   bucket.create("n", content, CONTENT_TYPE, Bucket.BlobTargetOption.encryptionKey(KEY));
 assertEquals(expectedBlob, blob);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateWithKmsKeyName() throws Exception {
 initializeExpectedBucket(5);
 BlobInfo info = BlobInfo.newBuilder(BlobId.of("b", "n")).setContentType(CONTENT_TYPE).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, Storage.BlobTargetOption.kmsKeyName(DEFAULT_KMS_KEY_NAME)))
   .andReturn(expectedBlob);
 replay(storage);
 initializeBucket();
 Blob blob =
   bucket.create(
     "n", content, CONTENT_TYPE, Bucket.BlobTargetOption.kmsKeyName(DEFAULT_KMS_KEY_NAME));
 assertEquals(expectedBlob, blob);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testCreateNotExists() throws Exception {
 initializeExpectedBucket(5);
 BlobInfo info =
   BlobInfo.newBuilder(BlobId.of("b", "n", 0L)).setContentType(CONTENT_TYPE).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, Storage.BlobTargetOption.generationMatch()))
   .andReturn(expectedBlob);
 replay(storage);
 initializeBucket();
 Blob blob = bucket.create("n", content, CONTENT_TYPE, Bucket.BlobTargetOption.doesNotExist());
 assertEquals(expectedBlob, blob);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testUpdateBucketRequesterPays() {
 Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
 assertNull(remoteBucket.requesterPays());
 remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
 Bucket updatedBucket = storage.update(remoteBucket);
 assertTrue(updatedBucket.requesterPays());
 String projectId = remoteStorageHelper.getOptions().getProjectId();
 Bucket.BlobTargetOption option = Bucket.BlobTargetOption.userProject(projectId);
 String blobName = "test-create-empty-blob-requester-pays";
 Blob remoteBlob = updatedBucket.create(blobName, BLOB_BYTE_CONTENT, option);
 assertNotNull(remoteBlob);
 byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
 assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
}

相关文章