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

Fix object download to file #105

Merged
merged 4 commits into from
Jan 23, 2025
Merged

Conversation

jeasai
Copy link
Contributor

@jeasai jeasai commented Nov 20, 2024

What

Fixed minio::s3::builders::object_content::ObjectContent::to_file() (which tries to write in a file opened in read-only mode), and added an example to show how to use it.

To reproduce the issue

main.rs:

use minio::s3::args::{BucketExistsArgs, MakeBucketArgs};
use minio::s3::builders::ObjectContent;
use minio::s3::client::ClientBuilder;
use minio::s3::creds::StaticProvider;
use minio::s3::http::BaseUrl;
use minio::s3::types::S3Api;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let base_url = "https://play.min.io".parse::<BaseUrl>()?;

    let static_provider = StaticProvider::new(
        "Q3AM3UQ867SPQQA43P2F",
        "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
        None,
    );

    let client = ClientBuilder::new(base_url.clone())
        .provider(Some(Box::new(static_provider)))
        .build()?;

    let bucket_name: &str = "asiatrip";
    let object_name: &str = "asiaphotos-2015.zip";
    let download_path: &str = &format!("/tmp/downloads/{object_name}");

    // Creates the bucket if it doesn't exist

    let exists = client.bucket_exists(&BucketExistsArgs::new(&bucket_name).unwrap()).await?;
    if !exists {
        client.make_bucket(&MakeBucketArgs::new(&bucket_name).unwrap()).await.unwrap();
    }

    // Adds an object in the bucket

    let content = ObjectContent::from(vec![]);
    client.put_object_content(bucket_name, object_name, content).send().await?;

    // Downloads the object to a file

    let get_object = client.get_object(bucket_name, object_name).send().await?;

    if let Err(e) = get_object.content.to_file(&Path::new(download_path)).await {
        println!("ObjectContent::to_file() failed: {e}");
    }

    Ok(())
}

Cargo.toml:

[package]
name = "sample"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
minio = { git="https://github.com/minio/minio-rs.git", rev="c28f576cb8f8cf47fb941bb9db62b2cbd6f080c1" }
tokio = "1.41.1"

Returns:

ObjectContent::to_file() failed: No such file or directory (os error 2)

@HJLebbink HJLebbink self-assigned this Jan 10, 2025
The ObjectContent::to_file() function was not working properly,
as it could not write in a temporary file (created with tokio::fs::File::open(),
which opens a file in read-only mode).

To solve this issue, modified the way temporary files are opened to be
able to write inside of it.
@HJLebbink
Copy link
Member

HJLebbink commented Jan 10, 2025

@jeasai Thank you for providing this fix.

I’ve taken the liberty of rebasing and updating the example to make it runnable without needing to place files in specific positions. I’ve committed the changes here: a7704fe. You can either rebase from my commit, and I’ll review your PR, or I can create a new PR (with you as the co-author) for my colleague to review. Let me know what works best for you!

This new example demonstrates how to download an object and store its
content into a file.
@jeasai jeasai force-pushed the fix-file-download branch from 2dae6c4 to b97b2f6 Compare January 13, 2025 18:06
@jeasai
Copy link
Contributor Author

jeasai commented Jan 13, 2025

Hello @HJLebbink !

Thanks for your answer and the example update.
I just rebased from your commit and squashed it into mine. The example runs well on my machine.
Let me know if there is anything you'd like me to update.

@HJLebbink HJLebbink self-requested a review January 13, 2025 19:32
@HJLebbink HJLebbink added the bug Something isn't working label Jan 23, 2025
HJLebbink
HJLebbink previously approved these changes Jan 23, 2025
Copy link
Member

@HJLebbink HJLebbink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@HJLebbink HJLebbink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@HJLebbink HJLebbink merged commit 26d67b8 into minio:master Jan 23, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants