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); } } }
/** * 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); }
@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()); }
@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); }
@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"))); }
@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()); }
@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()); }
@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());
@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()); }
/** * 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()); }
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(
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());
@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; } }
@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. } } } } };
@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); } } }
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)); }
@Test public void testGetBucketEmptyFields() { Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields()); assertEquals(BUCKET, remoteBucket.getName()); assertNull(remoteBucket.getCreateTime()); assertNull(remoteBucket.getSelfLink()); }
@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); }
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()); } }