private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); this.storage = options.getService(); }
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); this.storage = options.getService(); }
private void initStorage() { if (this.storage != null) { return; } if (storageOptions == null) { this.storage = StorageOptions.getDefaultInstance().getService(); } else { this.storage = storageOptions.getService(); } }
@Override public Blob apply(StorageObject storageObject) { return Blob.fromPb(serviceOptions.getService(), storageObject); } });
@Override public Bucket apply(com.google.api.services.storage.model.Bucket bucketPb) { return Bucket.fromPb(serviceOptions.getService(), bucketPb); } });
@Provides @Singleton Bucket getBucket(@ProjectId String projectId) { Storage storage = StorageOptions.getDefaultInstance().getService(); // Must match BUCKET_NAME for user data bucket in setup_gke_environment.sh String bucketId = "user-data-" + projectId; return storage.get(bucketId); }
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(); } } }
private void initializeService() { storage = options.getService(); initializeServiceDependentObjects(); }
/** Example of default auth */ public Page<Bucket> authListBuckets() { // [START auth_cloud_implicit] // If you don't specify credentials when constructing the client, the // client library will look for credentials in the environment. Storage storage = StorageOptions.getDefaultInstance().getService(); Page<Bucket> buckets = storage.list(); for (Bucket bucket : buckets.iterateAll()) { // do something with the info } // [END auth_cloud_implicit] return buckets; }
/** Example of retrieving Requester pays status on a bucket. */ public Bucket getRequesterPaysStatus(String bucketName) throws StorageException { // [START get_requester_pays_status] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the bucket to retrieve requester-pays status, eg. "my-bucket" // String bucketName = "my-bucket" // Retrieve the bucket, throws StorageException on failure Bucket bucket = storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.BILLING)); System.out.println("Requester pays status : " + bucket.requesterPays()); // [END get_requester_pays_status] return bucket; }
/** Example of how to disable default event-based hold for a bucket */ public Bucket disableDefaultEventBasedHold(String bucketName) throws StorageException { // [START storage_disable_default_event_based_hold] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of a bucket, e.g. "my-bucket" // String bucketName = "my-bucket"; Bucket bucket = storage.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(false).build()); System.out.println("Default event-based hold was disabled for " + bucketName); // [END storage_disable_default_event_based_hold] return bucket; }
@Before public void before() { mockOptions = mock(StorageOptions.class); Storage mockStorage = mock(Storage.class); when(mockOptions.getService()).thenReturn(mockStorage); CloudStorageFileSystemProvider.setStorageOptions(mockOptions); }
@Test public void ctorDoesNotCreateStorage() { new CloudStorageFileSystemProvider(); verify(mockOptions, never()).getService(); }
public static void main(String... args) { // [START storage_upload_file] Storage storage = StorageOptions.getDefaultInstance().getService(); BlobId blobId = BlobId.of("bucket", "blob_name"); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build(); Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8)); // [END storage_upload_file] } }
/** Example of enabling Requester pays on a bucket. */ public Bucket enableRequesterPays(String bucketName) throws StorageException { // [START enable_requester_pays] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the existing bucket to enable requester-paying for, e.g. "my-bucket" // String bucketName = "my-bucket" BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setRequesterPays(true).build(); // Update the bucket, throws StorageException on failure Bucket bucket = storage.update(bucketInfo); System.out.println("Requester pay status for " + bucketName + ": " + bucket.requesterPays()); // [END enable_requester_pays] return bucket; }
/** Example of disabling Requester pays on a bucket. */ public Bucket disableRequesterPays(String bucketName) { // [START disable_requester_pays] // Instantiate a Google Cloud Storage client Storage storage = StorageOptions.getDefaultInstance().getService(); // The name of the bucket to disable requester-paying for, e.g. "my-bucket" // String bucketName = "my-bucket" BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setRequesterPays(false).build(); // Update the bucket, throws StorageException on failure Bucket bucket = storage.update(bucketInfo); System.out.println("Requester pays status for " + bucketName + ": " + bucket.requesterPays()); // [END disable_requester_pays] return bucket; }
@Test public void getPathCreatesStorageOnce() { CloudStorageFileSystemProvider provider = new CloudStorageFileSystemProvider(); provider.getPath(URI.create("gs://bucket1/wat")); provider.getPath(URI.create("gs://bucket2/wat")); verify(mockOptions, times(1)).getService(); }
@Test public void getFileSystemCreatesStorageOnce() { CloudStorageFileSystemProvider provider = new CloudStorageFileSystemProvider(); provider.getFileSystem(URI.create("gs://bucket1")); provider.getFileSystem(URI.create("gs://bucket2")); verify(mockOptions, times(1)).getService(); } }
@Before public void setUp() { rpcFactoryMock = createMock(StorageRpcFactory.class); storageRpcMock = createMock(StorageRpc.class); expect(rpcFactoryMock.create(anyObject(StorageOptions.class))).andReturn(storageRpcMock); replay(rpcFactoryMock); options = StorageOptions.newBuilder() .setProjectId("projectid") .setServiceRpcFactory(rpcFactoryMock) .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); result = new Blob(options.getService(), new BlobInfo.BuilderImpl(RESULT_INFO)); }
@BeforeClass public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { remoteStorageHelper = RemoteStorageHelper.create(); topicAdminClient = TopicAdminClient.create(); storageService = remoteStorageHelper.getOptions().getService(); notificationService = new DefaultNotificationFactory().create(remoteStorageHelper.getOptions().getService()); storageService.create(BucketInfo.of(BUCKET)); projectId = ServiceOptions.getDefaultProjectId(); }