Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storage] Add a simple download sample #3673

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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 @@ -1133,6 +1133,32 @@ public Bucket getRequesterPaysStatus(String bucketName) throws StorageException
return bucket;
}

/**
* Example of downloading a file.
*/
public void downloadFile(String bucketName, String srcFilename,

This comment was marked as spam.

Path destFilePath) throws IOException {
// [START storage_download_file]
// 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
// Path destFilePath = Paths.get("/local/path/to/file.txt");

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

// Get specific file from specified bucket
Blob blob = storage.get(BlobId.of(bucketName, srcFilename));

// Download file to specified path
blob.downloadTo(destFilePath);
// [END storage_download_file]
}

/**
* Example of downloading a file using Requester pay.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ public void testAuthListBuckets() {
assertNotNull(bucket);
}

@Test
public void testBlobDownload() throws Exception {
String blobName = "test-create-empty-blob";
BlobId blobId = BlobId.of(BUCKET, blobName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
Blob remoteBlob = storage.create(blobInfo, BLOB_BYTE_CONTENT);
assertNotNull(remoteBlob);
storageSnippets.downloadFile(BUCKET, blobName, Paths.get(blobName));
byte[] readBytes = Files.readAllBytes(Paths.get(blobName));
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
}

@Test
public void testRequesterPays() throws Exception {
Bucket bucket = storageSnippets.enableRequesterPays(BUCKET);
Expand Down