本文整理了Java中com.google.cloud.storage.Bucket
类的一些代码示例,展示了Bucket
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bucket
类的具体详情如下:
包路径:com.google.cloud.storage.Bucket
类名称:Bucket
[英]A Google cloud storage bucket.
Objects of this class are immutable. Operations that modify the bucket like #updatereturn a new object. To get a Bucket object with the most recent information use #reload. Bucket adds a layer of service-related functionality over BucketInfo.
[中]谷歌云存储桶。
此类的对象是不可变的。修改bucket的操作(如#updater)将返回一个新对象。要获取包含最新信息的Bucket对象,请使用#reload。Bucket在BucketInfo之上添加了一层与服务相关的功能。
代码示例来源: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
/**
* Returns the requested blob in this bucket or {@code null} if not found.
*
* <p>Example of getting a blob in the bucket, only if its metageneration matches a value,
* otherwise a {@link StorageException} is thrown.
*
* <pre>{@code
* String blobName = "my_blob_name";
* long generation = 42;
* Blob blob = bucket.get(blobName, BlobGetOption.generationMatch(generation));
* }</pre>
*
* @param blob name of the requested blob
* @param options blob search options
* @throws StorageException upon failure
*/
public Blob get(String blob, BlobGetOption... options) {
return storage.get(BlobId.of(getName(), blob), options);
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testDelete() throws Exception {
initializeExpectedBucket(4);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.delete(BUCKET_INFO.getName())).andReturn(true);
replay(storage);
initializeBucket();
assertTrue(bucket.delete());
}
代码示例来源: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 testGetAllIterable() throws Exception {
initializeExpectedBucket(4);
expect(storage.getOptions()).andReturn(mockOptions);
List<BlobId> blobIds =
Lists.transform(
blobResults,
new Function<Blob, BlobId>() {
@Override
public BlobId apply(Blob blob) {
return blob.getBlobId();
}
});
expect(storage.get(blobIds)).andReturn(blobResults);
replay(storage);
initializeBucket();
assertEquals(blobResults, bucket.get(ImmutableList.of("n1", "n2", "n3")));
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testGetBucketWithEmptyFields() {
Capture<Map<StorageRpc.Option, Object>> capturedOptions = Capture.newInstance();
EasyMock.expect(
storageRpcMock.get(
EasyMock.eq(BucketInfo.of(BUCKET_NAME1).toPb()), EasyMock.capture(capturedOptions)))
.andReturn(BUCKET_INFO1.toPb());
EasyMock.replay(storageRpcMock);
initializeService();
Bucket bucket = storage.get(BUCKET_NAME1, BUCKET_GET_METAGENERATION, BUCKET_GET_EMPTY_FIELDS);
assertEquals(
BUCKET_GET_METAGENERATION.getValue(),
capturedOptions.getValue().get(BUCKET_GET_METAGENERATION.getRpcOption()));
String selector = (String) capturedOptions.getValue().get(BLOB_GET_FIELDS.getRpcOption());
assertTrue(selector.contains("name"));
assertEquals(4, selector.length());
assertEquals(BUCKET_INFO1.getName(), bucket.getName());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testExists_True() throws Exception {
initializeExpectedBucket(4);
Storage.BucketGetOption[] expectedOptions = {Storage.BucketGetOption.fields()};
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.get(BUCKET_INFO.getName(), expectedOptions)).andReturn(expectedBucket);
replay(storage);
initializeBucket();
assertTrue(bucket.exists());
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testBuilder() {
initializeExpectedBucket(4);
expect(storage.getOptions()).andReturn(mockOptions).times(4);
replay(storage);
Bucket.Builder builder =
new Bucket.Builder(new Bucket(storage, new BucketInfo.BuilderImpl(BUCKET_INFO)));
Bucket bucket =
builder
.setRetentionPolicyIsLocked(RETENTION_POLICY_IS_LOCKED)
.build();
assertEquals("b", bucket.getName());
assertEquals(ACLS, bucket.getAcl());
assertEquals(ETAG, bucket.getEtag());
assertEquals(GENERATED_ID, bucket.getGeneratedId());
assertEquals(META_GENERATION, bucket.getMetageneration());
assertEquals(OWNER, bucket.getOwner());
assertEquals(SELF_LINK, bucket.getSelfLink());
assertEquals(CREATE_TIME, bucket.getCreateTime());
assertEquals(CORS, bucket.getCors());
assertEquals(DEFAULT_ACL, bucket.getDefaultAcl());
assertEquals(DELETE_RULES, bucket.getDeleteRules());
assertEquals(LIFECYCLE_RULES, bucket.getLifecycleRules());
assertEquals(INDEX_PAGE, bucket.getIndexPage());
assertEquals(NOT_FOUND_PAGE, bucket.getNotFoundPage());
assertEquals(LOCATION, bucket.getLocation());
assertEquals(STORAGE_CLASS, bucket.getStorageClass());
assertEquals(VERSIONING_ENABLED, bucket.versioningEnabled());
assertEquals(BUCKET_LABELS, bucket.getLabels());
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testList() throws Exception {
initializeExpectedBucket(4);
PageImpl<Blob> expectedBlobPage = new PageImpl<>(null, "c", blobResults);
expect(storage.getOptions()).andReturn(mockOptions);
expect(storage.list(BUCKET_INFO.getName())).andReturn(expectedBlobPage);
replay(storage);
initializeBucket();
Page<Blob> blobPage = bucket.list();
Iterator<Blob> blobInfoIterator = blobPage.getValues().iterator();
Iterator<Blob> blobIterator = blobPage.getValues().iterator();
while (blobInfoIterator.hasNext() && blobIterator.hasNext()) {
assertEquals(blobInfoIterator.next(), blobIterator.next());
}
assertFalse(blobInfoIterator.hasNext());
assertFalse(blobIterator.hasNext());
assertEquals(expectedBlobPage.getNextPageToken(), blobPage.getNextPageToken());
}
代码示例来源:origin: googleapis/google-cloud-java
/**
* Creates a new blob in this bucket. Direct upload is used to upload {@code content}. For large
* content, {@link Blob#writer(com.google.cloud.storage.Storage.BlobWriteOption...)} is
* recommended as it uses resumable upload.
*
* <p>Example of creating a blob in the bucket from an input stream.
*
* <pre>{@code
* String blobName = "my_blob_name";
* InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
* Blob blob = bucket.create(blobName, content);
* }</pre>
*
* @param blob a blob name
* @param content the blob content as a stream
* @param options options for blob creation
* @return a complete blob information
* @throws StorageException upon failure
*/
public Blob create(String blob, InputStream content, BlobWriteOption... options) {
BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(getName(), blob)).build();
Tuple<BlobInfo, Storage.BlobWriteOption[]> write =
BlobWriteOption.toWriteOptions(blobInfo, options);
return storage.create(write.x(), content, write.y());
}
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
Bucket bucket = storage.get(bucketName);
com.google.api.gax.paging.Page<Blob> pageList = bucket.list(BlobListOption.prefix(prefix));
代码示例来源:origin: googleapis/google-cloud-java
BlobInfo blob1 =
BlobInfo.newBuilder(BUCKET, "test-list-blobs-empty-selected-fields-blob1")
.setContentType(CONTENT_TYPE)
.build();
assertNotNull(storage.create(blob1));
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());
try {
storage.list(
代码示例来源:origin: googleapis/google-cloud-java
String bucketName = RemoteStorageHelper.generateBucketName();
Bucket remoteBucket =
storage.create(
BucketInfo.newBuilder(bucketName)
.setDefaultKmsKeyName(kmsKeyOneResourcePath)
.setLocation(KMS_KEY_RING_LOCATION)
.build());
assertNotNull(remoteBucket);
assertTrue(remoteBucket.getDefaultKmsKeyName().startsWith(kmsKeyOneResourcePath));
try {
Iterator<Bucket> bucketIterator =
storage
.list(
Storage.BucketListOption.prefix(bucketName),
Storage.BucketListOption.fields(BucketField.ENCRYPTION))
bucketIterator =
storage
.list(
Storage.BucketListOption.prefix(bucketName),
Storage.BucketListOption.fields(BucketField.ENCRYPTION))
assertTrue(bucket.getName().startsWith(bucketName));
assertNotNull(bucket.getDefaultKmsKeyName());
assertTrue(bucket.getDefaultKmsKeyName().startsWith(kmsKeyOneResourcePath));
assertNull(bucket.getCreateTime());
assertNull(bucket.getSelfLink());
代码示例来源:origin: ai.h2o/h2o-persist-gcs
@Override
public boolean mkdirs(String path) {
try {
final String bk[] = split(path);
if (bk.length > 0) {
Bucket b = storageProvider.getStorage().get(bk[0]);
if (b == null || !b.exists()) {
storageProvider.getStorage().create(BucketInfo.of(bk[0]));
}
return true;
} else {
return false;
}
} catch (StorageException e) {
Log.err(e);
return false;
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Override
public void run() {
Page<Bucket> buckets =
storage.list(Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX));
for (Bucket bucket : buckets.iterateAll()) {
if (bucket.getCreateTime() < olderThan) {
try {
for (Blob blob :
bucket
.list(
BlobListOption.fields(
Storage.BlobField.EVENT_BASED_HOLD,
Storage.BlobField.TEMPORARY_HOLD))
.iterateAll()) {
if (blob.getEventBasedHold() == true || blob.getTemporaryHold() == true) {
storage.update(
blob.toBuilder()
.setTemporaryHold(false)
.setEventBasedHold(false)
.build());
}
}
forceDelete(storage, bucket.getName());
} catch (Exception e) {
// Ignore the exception, maybe the bucket is being deleted by someone else.
}
}
}
}
};
代码示例来源:origin: googleapis/google-cloud-java
@Override
public void run(Storage storage, String bucketName) {
if (bucketName == null) {
// list buckets
for (Bucket bucket : storage.list().iterateAll()) {
System.out.println(bucket);
}
} else {
// list a bucket's blobs
Bucket bucket = storage.get(bucketName);
if (bucket == null) {
System.out.println("No such bucket");
return;
}
for (Blob blob : bucket.list().iterateAll()) {
System.out.println(blob);
}
}
}
代码示例来源:origin: googleapis/google-cloud-java
private void testBucketAclRequesterPays(boolean requesterPays) {
if (requesterPays) {
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();
Storage.BucketSourceOption[] bucketOptions =
requesterPays
? new Storage.BucketSourceOption[] {Storage.BucketSourceOption.userProject(projectId)}
: new Storage.BucketSourceOption[] {};
assertNull(storage.getAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
assertFalse(storage.deleteAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
Acl acl = Acl.of(User.ofAllAuthenticatedUsers(), Role.READER);
assertNotNull(storage.createAcl(BUCKET, acl, bucketOptions));
Acl updatedAcl =
storage.updateAcl(BUCKET, acl.toBuilder().setRole(Role.WRITER).build(), bucketOptions);
assertEquals(Role.WRITER, updatedAcl.getRole());
Set<Acl> acls = new HashSet<>();
acls.addAll(storage.listAcls(BUCKET, bucketOptions));
assertTrue(acls.contains(updatedAcl));
assertTrue(storage.deleteAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
assertNull(storage.getAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void testGetBucketEmptyFields() {
Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields());
assertEquals(BUCKET, remoteBucket.getName());
assertNull(remoteBucket.getCreateTime());
assertNull(remoteBucket.getSelfLink());
}
代码示例来源: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);
}
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
public static void main(String... args) throws Exception {
// Instantiates a client
Storage storage = StorageOptions.getDefaultInstance().getService();
// The name for the new bucket
String bucketName = args[0]; // "my-new-bucket";
// Creates the new bucket
Bucket bucket = storage.create(BucketInfo.of(bucketName));
System.out.printf("Bucket %s created.%n", bucket.getName());
}
}
内容来源于网络,如有侵权,请联系作者删除!