#amazon-s3 #icp #object-store #oss #storage

ic-oss-can

A Rust library for implementing large file storage in ICP canisters

23 releases (5 stable)

new 1.1.2 May 5, 2025
1.1.0 Apr 30, 2025
1.0.1 Mar 25, 2025
0.9.12 Jan 25, 2025
0.6.6 Jul 17, 2024

#433 in Magic Beans

Download history 89/week @ 2025-01-20 23/week @ 2025-01-27 98/week @ 2025-02-03 45/week @ 2025-02-10 15/week @ 2025-02-17 3/week @ 2025-02-24 1/week @ 2025-03-03 112/week @ 2025-03-24 6/week @ 2025-03-31 1/week @ 2025-04-07 173/week @ 2025-04-28

174 downloads per month

MIT/Apache

105KB
2.5K SLoC

ic-oss-can

License Crates.io CI Docs.rs Latest Version

A Rust library for implementing large file storage in Internet Computer (ICP) canisters. Part of the ic-oss.

Features

  • Simple integration with the ic_oss_fs! macro
  • Automatic generation of filesystem APIs in Candid format
  • Using given FS_CHUNKS_STORE stable storage
  • File chunk management and retrieval
  • Access control with manager roles
  • Compatible with ic-oss-cli for file uploads

Quick Start

Add the following dependencies to your Cargo.toml:

[dependencies]
ic-oss-can = "0.9"
ic-oss-types = "0.9"

Basic Implementation

use ic_stable_structures::{
    memory_manager::{MemoryId, MemoryManager, VirtualMemory},
    DefaultMemoryImpl, StableBTreeMap,
};
use std::cell::RefCell;

use ic_oss_can::ic_oss_fs;
use ic_oss_can::types::{Chunk, FileId, FileMetadata};

type Memory = VirtualMemory<DefaultMemoryImpl>;

const FS_DATA_MEMORY_ID: MemoryId = MemoryId::new(0);

thread_local! {

    static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
        RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));


    // `FS_CHUNKS_STORE`` is needed by `ic_oss_can::ic_oss_fs!` macro
    static FS_CHUNKS_STORE: RefCell<StableBTreeMap<FileId, Chunk, Memory>> = RefCell::new(
        StableBTreeMap::init(
            MEMORY_MANAGER.with_borrow(|m| m.get(FS_DATA_MEMORY_ID)),
        )
    );
}

// need to define `FS_CHUNKS_STORE` before `ic_oss_can::ic_oss_fs!()`
ic_oss_fs!();

Available APIs

Rust Module APIs

// File Management
fs::get_file(id: u32) -> Option<FileMetadata>;
fs::list_files(prev: u32, take: u32) -> Vec<FileInfo>;
fs::add_file(file: FileMetadata) -> Result<u32, String>;
fs::update_file(change: UpdateFileInput, now_ms: u64) -> Result<(), String>;
fs::delete_file(id: u32) -> Result<bool, String>;

// Chunk Operations
fs::get_chunk(id: u32, chunk_index: u32) -> Option<FileChunk>;
fs::get_full_chunks(id: u32) -> Result<Vec<u8>, String>;
fs::update_chunk(id: u32, chunk_index: u32, now_ms: u64, chunk: Vec<u8>) -> Result<u64, String>;

// Configuration
fs::set_max_file_size(size: u64);
fs::set_visibility(visibility: u8);
fs::set_managers(managers: BTreeSet<Principal>);
fs::is_manager(caller: &Principal) -> bool;
fs::with<R>(f: impl FnOnce(&Files) -> R) -> R;
fs::load();
fs::save();

Candid Interface

create_file : (CreateFileInput, opt blob) -> (Result_2);
delete_file : (nat32, opt blob) -> (Result_3);
list_files : (nat32, opt nat32, opt nat32, opt blob) -> (Result_4) query;
update_file_chunk : (UpdateFileChunkInput, opt blob) -> (Result_6);
update_file_info : (UpdateFileInput, opt blob) -> (Result_7);

For complete API definitions and examples, see:

License

Copyright © 2024-2025 LDC Labs.

Licensed under the MIT License. See LICENSE for details.

Dependencies

~12–21MB
~307K SLoC