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

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

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

Blob.writer介绍

[英]Returns a WriteChannel object for writing to this blob. By default any md5 and crc32c values in the current blob are ignored unless requested via the BlobWriteOption.md5Match and BlobWriteOption.crc32cMatch options.

Example of writing the blob's content through a writer.

byte[] content = "Hello, World!".getBytes(UTF_8);catch (Exception ex)  
// handle exception 
} 
} 
}

[中]返回用于写入此blob的WriteChannel对象。默认情况下,除非通过BLOBWRITE选项请求,否则将忽略当前blob中的任何md5和crc32c值。md5Match和BlobWrite选项。crc32cMatch选项。
通过编写器编写blob内容的示例。

byte[] content = "Hello, World!".getBytes(UTF_8);catch (Exception ex)  
// handle exception 
} 
} 
}

代码示例

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

/** Example of writing the blob's content through a writer. */
// [TARGET writer(BlobWriteOption...)]
public void writer() throws IOException {
 // [START writer]
 byte[] content = "Hello, World!".getBytes(UTF_8);
 try (WriteChannel writer = blob.writer()) {
  try {
   writer.write(ByteBuffer.wrap(content, 0, content.length));
  } catch (Exception ex) {
   // handle exception
  }
 }
 // [END writer]
}

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

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

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

@Test
public void testWriter() throws Exception {
 initializeExpectedBlob(2);
 BlobWriteChannel channel = createMock(BlobWriteChannel.class);
 expect(storage.getOptions()).andReturn(mockOptions);
 expect(storage.writer(eq(expectedBlob))).andReturn(channel);
 replay(storage);
 initializeBlob();
 assertSame(channel, blob.writer());
}

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

@Test
public void testWriterWithEncryptionKey() throws Exception {
 initializeExpectedBlob(2);
 BlobWriteChannel channel = createMock(BlobWriteChannel.class);
 expect(storage.getOptions()).andReturn(mockOptions);
 expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.encryptionKey(BASE64_KEY))))
   .andReturn(channel)
   .times(2);
 replay(storage);
 initializeBlob();
 assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(BASE64_KEY)));
 assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(KEY)));
}

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

@Test
public void testWriterWithKmsKeyName() throws Exception {
 initializeExpectedBlob(2);
 BlobWriteChannel channel = createMock(BlobWriteChannel.class);
 expect(storage.getOptions()).andReturn(mockOptions);
 expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.kmsKeyName(KMS_KEY_NAME))))
   .andReturn(channel);
 replay(storage);
 initializeBlob();
 assertSame(channel, blob.writer(BlobWriteOption.kmsKeyName(KMS_KEY_NAME)));
}

代码示例来源:origin: org.springframework.cloud/spring-cloud-gcp-storage

/**
 * Returns the output stream for a Google Cloud Storage file.
 * @return the object's output stream or {@code null} if the object doesn't exist and cannot be
 * created
 * @throws IOException if an issue occurs getting the OutputStream
 */
@Override
public OutputStream getOutputStream() throws IOException {
  if (isBucket()) {
    throw new IllegalStateException("Cannot open an output stream to a bucket: '" + this.location + "'");
  }
  else {
    Blob blob = getBlob();
    if (blob == null || !blob.exists()) {
      if (!this.autoCreateFiles) {
        throw new FileNotFoundException("The blob was not found: " + this.location);
      }
      blob = createBlob();
    }
    return Channels.newOutputStream(blob.writer());
  }
}

代码示例来源:origin: spring-cloud/spring-cloud-gcp

/**
 * Returns the output stream for a Google Cloud Storage file.
 * @return the object's output stream or {@code null} if the object doesn't exist and cannot be
 * created
 * @throws IOException if an issue occurs getting the OutputStream
 */
@Override
public OutputStream getOutputStream() throws IOException {
  if (isBucket()) {
    throw new IllegalStateException("Cannot open an output stream to a bucket: '" + this.location + "'");
  }
  else {
    Blob blob = getBlob();
    if (blob == null || !blob.exists()) {
      if (!this.autoCreateFiles) {
        throw new FileNotFoundException("The blob was not found: " + this.location);
      }
      blob = createBlob();
    }
    return Channels.newOutputStream(blob.writer());
  }
}

代码示例来源:origin: ai.h2o/h2o-persist-gcs

@Override
public void store(Value v) throws IOException {
 if (!v._key.home()) return;
 final byte payload[] = v.memOrLoad();
 final GcsBlob blob = GcsBlob.of(v._key);
 Log.debug("Storing: " + blob.toString());
 final ByteBuffer buffer = ByteBuffer.wrap(payload);
 storageProvider.getStorage().create(blob.getBlobInfo()).writer().write(buffer);
}

代码示例来源:origin: ai.h2o/h2o-persist-gcs

@Override
public OutputStream create(String path, boolean overwrite) {
 final GcsBlob gcsBlob = GcsBlob.of(path);
 Log.debug("Creating: " + gcsBlob.getCanonical());
 final WriteChannel writer = storageProvider.getStorage().create(gcsBlob.getBlobInfo()).writer();
 return new OutputStream() {
  @Override
  public void write(int b) throws IOException {
   ByteBuffer buffer = ByteBuffer.wrap(new byte[]{(byte) b});
   writer.write(buffer);
  }
  @Override
  public void write(byte[] b) throws IOException {
   ByteBuffer buffer = ByteBuffer.wrap(b);
   writer.write(buffer);
  }
  @Override
  public void write(byte[] b, int off, int len) throws IOException {
   ByteBuffer buffer = ByteBuffer.wrap(b, off, len);
   writer.write(buffer);
  }
  @Override
  public void close() throws IOException {
   writer.close();
  }
 };
}

相关文章