Skip to main content

HFBucket

Struct HFBucket 

Source
pub struct HFBucket { /* private fields */ }
Expand description

A handle for a single bucket on the Hugging Face Hub.

HFBucket is created via HFClient::bucket and binds together the client, owner (namespace), and bucket name. All bucket-scoped API operations are methods on this type.

Cheap to clone — the inner HFClient is Arc-backed.

§Example

let client = HFClient::builder().build()?;
let bucket = client.bucket("my-org", "my-bucket");

let info = bucket.info().send().await?;
println!("bucket {} ({} files)", info.id, info.total_files);

let stream = bucket.list_tree().send()?;
futures::pin_mut!(stream);
while let Some(entry) = stream.next().await {
    println!("{:?}", entry?);
}

Implementations§

Source§

impl HFBucket

Source

pub fn sync<'f1>(&'f1 self) -> HFBucketSyncBuilder<'f1>

Available on non-target_family=wasm only.

Synchronize a local directory with this bucket.

The sync is one-way and controlled by direction:

When delete is enabled, files that only exist on the receiving side are removed as part of the sync. The returned BucketSyncPlan describes the operations that were executed; set verbose(true) if you also want explicit Skip entries explaining why untouched files were not transferred.

§Comparison behavior

By default, files are considered different when their sizes differ or when the source file is newer than the destination file. Modification times are compared with a small tolerance to avoid unnecessary transfers caused by filesystem timestamp precision. ignore_times switches the comparison to size-only; ignore_sizes switches it to mtime-only. existing and ignore_existing further limit which files are eligible to transfer.

§Parameters
  • local_path (required): local directory path.
  • direction (required): sync direction (upload or download).
  • prefix: optional prefix within the bucket (subdirectory).
  • delete: delete destination files not present in source.
  • ignore_times: only compare sizes, ignore modification times.
  • ignore_sizes: only compare modification times, ignore sizes.
  • existing: only sync files that already exist at destination.
  • ignore_existing: skip files that already exist at destination.
  • include: glob-style include patterns.
  • exclude: glob-style exclude patterns. Exclusions take precedence over inclusions.
  • verbose: include skip operations in the returned plan.
  • progress: progress handler for upload/download tracking.
Source§

impl HFBucket

Source

pub fn new( client: HFClient, owner: impl Into<String>, name: impl Into<String>, ) -> Self

Construct a new bucket handle. Prefer HFClient::bucket in most cases.

Source

pub fn client(&self) -> &HFClient

Return a reference to the underlying HFClient.

Source

pub fn owner(&self) -> &str

The bucket owner (user or organization namespace).

Source

pub fn name(&self) -> &str

The bucket name (without owner prefix).

Source

pub fn bucket_id(&self) -> String

The full "owner/name" bucket identifier used in Hub API calls.

Source§

impl HFBucket

Source

pub fn info<'f1>(&'f1 self) -> HFBucketInfoBuilder<'f1>

Get metadata about this bucket.

Endpoint: GET /api/buckets/{bucket_id}.

Source

pub fn list_tree<'f1>(&'f1 self) -> HFBucketListTreeBuilder<'f1>

List files and directories in this bucket.

This is the main API for browsing bucket contents. For targeted lookups of a known set of paths, prefer HFBucket::get_paths_info.

Endpoint: GET /api/buckets/{bucket_id}/tree[/{prefix}] (paginated).

§Parameters
  • prefix: filter results to entries under this prefix.
  • recursive (default false): traverse subdirectories.
Source

pub fn get_paths_info<'f1>(&'f1 self) -> HFBucketGetPathsInfoBuilder<'f1>

Get info about specific paths in this bucket.

This is useful when you already know which paths you care about and do not want to stream the full tree. Requests are automatically chunked in batches of 1000 paths.

Endpoint: POST /api/buckets/{bucket_id}/paths-info.

§Parameters
  • paths (required): paths to inspect.
Source

pub fn download_file_stream<'f1>( &'f1 self, ) -> HFBucketDownloadFileStreamBuilder<'f1>

Stream the bytes of a single file in this bucket without writing to disk.

Parallel of HFRepository::download_file_stream for buckets. Returns (content_length, stream)content_length reflects the file size reported by paths-info. Xet-backed files dispatch through xet streaming; non-xet files fall back to a direct GET on the bucket’s resolve URL.

Endpoint (non-xet branch): GET {endpoint}/buckets/{bucket_id}/resolve/{remote_path}.

§Parameters
  • remote_path (required): file path within the bucket.
  • progress: optional progress handler. Start is emitted before the stream is returned; Progress is emitted as the caller polls each chunk; Complete is emitted when the stream is exhausted.
Source

pub fn get_file_metadata<'f1>(&'f1 self) -> HFBucketGetFileMetadataBuilder<'f1>

Get metadata for a single file in this bucket via a HEAD request.

Endpoint: HEAD /buckets/{bucket_id}/resolve/{path}.

§Parameters
  • remote_path (required): file path within the bucket.
Source

pub fn batch_operations<'f1>(&'f1 self) -> HFBucketBatchOperationsBuilder<'f1>

Execute batch file operations (add, delete, copy) on this bucket.

This is the low-level file mutation API. add operations only register metadata on the bucket side; the file contents must already have been uploaded to xet so each entry has a valid BucketAddFile::xet_hash.

For simpler upload and download flows, prefer HFBucket::upload_files and HFBucket::download_files.

Endpoint: POST /api/buckets/{bucket_id}/batch (NDJSON). Operations are chunked at 1000 entries per request.

All paths in add, delete, and copy are bucket-relative, slash-separated paths (e.g., "data/train/0001.bin") — no leading slash, forward slashes regardless of platform, no bucket_id prefix.

§Parameters
  • add_files: files to register in the bucket. Each BucketAddFile requires:
    • path (String): bucket-relative destination path.
    • xet_hash (String): xet content hash from a prior xet upload — the bytes must already be in xet storage; this call only registers metadata.
    • size (u64): file size in bytes.
    • mtime (Option<u64>): last modification time as a Unix timestamp in seconds.
    • content_type (Option<String>): MIME type (e.g., "text/plain").
  • delete: bucket-relative paths to remove from the bucket.
  • copy: server-side copies into this bucket. Each BucketCopyFile requires:
    • path (String): bucket-relative destination path.
    • xet_hash (String): xet content hash of the source bytes (already present in xet storage from another repo or bucket — copies are by-hash, no data is transferred).
    • source_repo_type (BucketCopySourceType): repo type of the source — one of Bucket, Model, Dataset, or Space. Serializes to the lowercase wire string the Hub expects.
    • source_repo_id (String): full source identifier in "namespace/name" form (e.g., "user/my-bucket").
Source

pub fn delete_files<'f1>(&'f1 self) -> HFBucketDeleteFilesBuilder<'f1>

Delete files from this bucket by path.

Convenience wrapper around HFBucket::batch_operations that sends only deleteFile operations.

§Parameters
  • paths (required): paths to delete from the bucket.
Source

pub fn upload_source_files<'f1>( &'f1 self, ) -> HFBucketUploadSourceFilesBuilder<'f1>

Upload an arbitrary set of AddSource-backed files to the bucket.

More general analog of HFBucket::upload_files (which only reads from local paths): each pair can use any AddSource variant — Bytes for in-memory content, File for a local path, or Stream for sources too large to materialize at once. For the common in-memory case, build entries with AddSource::bytes:

vec![("logs/run-1.txt".into(), AddSource::bytes(b"hello".as_slice()))]

Goes through the same preupload + xet pipeline as HFBucket::upload_files.

§Parameters
  • files (required): list of (remote_path, source) pairs.
  • progress: optional progress handler.
Source

pub fn upload_files<'f1>(&'f1 self) -> HFBucketUploadFilesBuilder<'f1>

Upload local files to the bucket.

File contents are uploaded to xet first, then registered in the bucket via the batch endpoint. Each entry pairs a local source with a bucket-relative destination via the BucketUpload struct (use BucketUpload::new to construct one).

§Parameters
  • files (required): list of BucketUpload entries describing each localremote mapping.
  • progress: optional progress handler.
Source

pub fn download_files<'f1>(&'f1 self) -> HFBucketDownloadFilesBuilder<'f1>

Download files from the bucket to local paths.

The method first resolves xet metadata for each remote path, then downloads the file contents through xet. Directory entries are rejected. Each entry pairs a bucket-relative source with a local destination via the BucketDownload struct (use BucketDownload::new to construct one).

§Parameters
  • files (required): list of BucketDownload entries describing each remotelocal mapping.
  • progress: optional progress handler.

Trait Implementations§

Source§

impl Clone for HFBucket

Source§

fn clone(&self) -> HFBucket

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HFBucket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DropFlavorWrapper<T> for T

Source§

type Flavor = MayDrop

The DropFlavor that wraps T into Self
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<E> ResultError for E
where E: Send + Debug + Sync,

Source§

impl<T> ResultType for T
where T: Send + Clone + Sync + Debug,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more