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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/storage/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ google-cloud-storage.workspace = true
google-cloud-type.workspace = true
google-cloud-wkt.workspace = true
rand.workspace = true
tempfile.workspace = true
tokio = { workspace = true, features = ["macros"] }
tracing-subscriber = { workspace = true, features = ["fmt", "std"] }
tracing.workspace = true
Expand Down
14 changes: 14 additions & 0 deletions src/storage/examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,20 @@ pub async fn run_object_examples(buckets: &mut Vec<String>) -> anyhow::Result<()
tracing::info!("running stream_file_download example");
objects::stream_file_download::sample(&client, &id).await?;

tracing::info!("create temp file for upload");
let file_to_upload = tempfile::NamedTempFile::new()?;
let file_to_upload_path = file_to_upload.path().to_str().unwrap();
tokio::fs::write(file_to_upload_path, "hello world from file").await?;
tracing::info!("running upload_file example");
objects::upload_file::sample(&client, &id, "uploaded-file.txt", file_to_upload_path).await?;
tracing::info!("running download_file example");
let downloaded_file = tempfile::NamedTempFile::new()?;
let downloaded_file_path = downloaded_file.path().to_str().unwrap();
objects::download_file::sample(&client, &id, "uploaded-file.txt", downloaded_file_path).await?;
tracing::info!("checking downloaded file content");
let content = tokio::fs::read_to_string(downloaded_file_path).await?;
assert_eq!(content, "hello world from file");

tracing::info!("running download_byte_range example");
objects::download_byte_range::sample(&client, &id, "object-to-download.txt", 4, 10).await?;

Expand Down
2 changes: 2 additions & 0 deletions src/storage/examples/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
pub mod delete_file;
pub mod download_byte_range;
pub mod download_encrypted_file;
pub mod download_file;
pub mod generate_encryption_key;
pub mod list_files;
pub mod list_files_with_prefix;
pub mod set_metadata;
pub mod stream_file_download;
pub mod stream_file_upload;
pub mod upload_encrypted_file;
pub mod upload_file;
39 changes: 39 additions & 0 deletions src/storage/examples/src/objects/download_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START storage_download_file]
use google_cloud_storage::client::Storage;
use tokio::io::AsyncWriteExt;

pub async fn sample(
client: &Storage,
bucket: &str,
object: &str,
file_path: &str,
) -> Result<(), anyhow::Error> {
let mut reader = client
.read_object(format!("projects/_/buckets/{bucket}"), object)
.send()
.await?;

let mut file = tokio::fs::File::create(file_path).await?;
while let Some(data) = reader.next().await.transpose()? {
file.write_all(&data).await?;
}
file.flush().await?;

println!("Downloaded {object} in bucket {bucket} to {file_path}.");
Ok(())
}
// [END storage_download_file]
33 changes: 33 additions & 0 deletions src/storage/examples/src/objects/upload_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START storage_upload_file]
use google_cloud_storage::client::Storage;

pub async fn sample(
client: &Storage,
bucket: &str,
object: &str,
file_path: &str,
) -> Result<(), anyhow::Error> {
let payload = tokio::fs::File::open(file_path).await?;
let _result = client
.write_object(format!("projects/_/buckets/{bucket}"), object, payload)
.send_unbuffered()
.await?;

println!("Uploaded {file_path} to {object} in bucket {bucket}.");
Ok(())
}
// [END storage_upload_file]
Loading