Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -1042,4 +1045,100 @@ public Page<Bucket> authListBuckets() {
return buckets;
}

/**
* 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;
}

/**
* 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);

System.out.println("Requester pays status : " + bucket.requesterPays());
// [END get_requester_pays_status]
return bucket;
}

/**
* Example of downloading a file using Requester pay.
*/
public void downloadFileUsingRequesterPays(String projectId, String bucketName,
String srcFilename, Path destFilePath) throws IOException {
// [START storage_download_file_requester_pays]
// Instantiate a Google Cloud Storage client

// The project ID to bill
// String projectId = "my-billable-project-id";

This comment was marked as spam.


// The name of the bucket to access
// String bucketName = "my-bucket";

// The name of the remote file to download
// String srcFilename = "file.txt";

// The path to which the file should be downloaded
// String destFilePath = "/local/path/to/file.txt";

// Instantiate a Google Cloud Storage client
Storage storage = StorageOptions.getDefaultInstance().getService();

BlobSourceOption option = BlobSourceOption.userProject(projectId);

// read blob in a single pass
byte[] readBytes = storage.readAllBytes(bucketName, srcFilename, option);

// write out to file
PrintStream out = new PrintStream(new FileOutputStream(destFilePath.toFile()));

This comment was marked as spam.

This comment was marked as spam.

out.write(readBytes);
out.close();
// [END storage_download_file_requester_pays]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import static org.junit.Assert.fail;

import com.google.api.gax.paging.Page;
import com.google.cloud.ServiceOptions;
import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.Role;
import com.google.cloud.storage.Acl.User;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Bucket.BlobTargetOption;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.testing.RemoteStorageHelper;
Expand All @@ -50,6 +52,8 @@
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -64,6 +68,7 @@ public class ITStorageSnippets {

private static final Logger log = Logger.getLogger(ITStorageSnippets.class.getName());
private static final String BUCKET = RemoteStorageHelper.generateBucketName();
private static final byte[] BLOB_BYTE_CONTENT = {0xD, 0xE, 0xA, 0xD};
private static final String USER_EMAIL = "google-cloud-java-tests@"
+ "java-docs-samples-tests.iam.gserviceaccount.com";

Expand Down Expand Up @@ -405,4 +410,23 @@ public void testAuthListBuckets() {
Page<Bucket> bucket = storageSnippets.authListBuckets();
assertNotNull(bucket);
}

@Test
public void testRequesterPays() throws Exception {
Bucket bucket = storageSnippets.enableRequesterPays(BUCKET);
assertTrue(bucket.requesterPays());
bucket = storageSnippets.getRequesterPaysStatus(BUCKET);
assertTrue(bucket.requesterPays());
String projectId = ServiceOptions.getDefaultProjectId();
String blobName = "test-create-empty-blob-requester-pays";
Blob remoteBlob = bucket.create(blobName, BLOB_BYTE_CONTENT,
BlobTargetOption.userProject(projectId));
assertNotNull(remoteBlob);
storageSnippets.downloadFileUsingRequesterPays(projectId, BUCKET, blobName,
Paths.get(blobName));
byte[] readBytes = Files.readAllBytes(Paths.get(blobName));
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
bucket = storageSnippets.disableRequesterPays(BUCKET);
assertFalse(bucket.requesterPays());
}
}