#cloud-storage #gcp #azure #bucket #blob #aws #azure-storage

waihona

用于主要云服务提供商的基本云存储功能的库

3个版本

0.0.3 2022年3月26日
0.0.2 2021年9月15日
0.0.1 2021年9月14日

1193Web编程

Download history 13/week @ 2024-03-01 3/week @ 2024-03-15 1/week @ 2024-03-22 38/week @ 2024-03-29 3/week @ 2024-04-05

每月下载 80

自定义许可证

58KB
1.5K SLoC

waihona

mythra

Crates.io Build Status Publish Status

使用方法

默认启用所有云服务提供商,例如要指定单个提供商,例如aws

[dependencies]
waihona={version = "0.0.3", features = ["aws"], default-features = false }

Rust库,用于主要云服务提供商的云存储。它旨在提供简单的函数来执行对存储桶和块的基本CRUD操作。Waihona在夏威夷语中意为“存储”

功能标志

此crate存在以下功能标志

  • aws:启用aws提供商和依赖项
  • gcp:启用gcp提供商和依赖项
  • azure:启用azure提供商和依赖项

特质

三个主要特质控制每个提供商的行为

存储桶 -> Bucket -> Blob

// all methods of traits are async
 use bytes::Bytes;

 trait Buckets<T, P>
     where T: Bucket<P>, P: Blob{
         fn open(&mut self, bucket_name: &str);
         fn create(&mut self, bucket_name: &str, location: Option<String>);
         fn list(&mut self);
         fn delete(&mut self, bucket_name: &str);
         fn exists(&mut self, bucket_name: &str);
    }

trait Bucket<P>
    where P: Blob{
        fn list_blobs(&self, marker: Option<String>);
        fn get_blob(&self, blob_path: &str, content_range: Option<String>);
        fn copy_blob(&self, blob_path: &str, blob_destination_path: &str, content_type: Option<String>);
        fn write_blob(&self, blob_name: &str, content: Option<Bytes>);
        fn delete_blob(&self, blob_path: &str);
    }

 trait Blob {
     fn delete(&self);
     fn copy(&self, blob_destination_path: &str, content_type: Option<String> );
     fn write(&self, content: Option<Bytes>);
     fn read(&mut self);
    }

示例

以下快速示例将展示如何使用库进行基本操作

列出GCP上waihona项目中的存储桶

// ensure to export service credential using GOOGLE_APPLICATION_CREDENTIALS
#[cfg(feature = "gcp")]
use waihona::providers::gcp::GcpBucket;

#[tokio::test]
#[cfg(feature = "gcp")]
async fn test_list_buckets() -> Vec<GcpBucket> {
   // Import Buckets trait from crate
   use waihona::types::bucket::{Buckets};
   use waihona::providers::gcp;
   let mut gcp_buckets = providers::gcp::GcpBuckets::new(
       "waihona"
       );
   // Returns (Vec<GcpBucket, Option<String>)
   // where Option<String> is the cursor for the token for next page listing
   let resp = gcp_buckets.list().await;
   resp[0]
}

检查AWS上是否存在waihona存储桶


#[tokio::test]
#[cfg(feature = "aws")]
async fn test_bucket_exists() -> bool {
   use waihona::types::bucket::{Buckets};
   use waihona::providers;
   let mut aws_buckets = providers::aws::AwsBuckets::new(
       "us-east-2"
       );
   let resp = aws_buckets.exists(
       "waihona"
       ).await;
       // OR you can do
   let resp = providers::aws::AwsBucket::exists(
       "us-east-2",
       "waihona"
       ).await;
   resp
}

将内容写入Azure上waihona存储桶中的"example.txt"块

#[cfg(feature = "azure")]
use waihona::providers::azure::AzureBlob;



#[tokio::test]
#[cfg(feature = "azure")]
async fn test_create_blob() -> AzureBlob {
   use waihona::types::bucket::{Buckets, Bucket};
   use waihona::types::blob::{Blob};
   use waihona::providers;
   use bytes::Bytes;
   let mut azure_buckets = providers::azure::AzureBuckets::new("waihona".to_owned());
   let waihona = azure_buckets.open(
       "waihona",
       ).await.unwrap();
   let mut blob = waihona.write_blob(
       "example.txt",
        Some(Bytes::from("Hello world"))
       ).await
       .unwrap();
    let read = blob.read().await.unwrap();
    assert!(read.eq(&Bytes::from("Hello world")));
 }

从AWS上的"example.txt"块复制文件内容到GCP上的块,并在之后删除AWS块,假设waihona存储桶在两个平台上都存在

#[cfg(feature = "gcp")]
use waihona::providers::gcp::GcpBlob;


#[tokio::test]
#[cfg(all(feature = "gcp", feature = "aws" ))]
async fn test_transfer_blob() -> GcpBlob {
   use waihona::types::bucket::{Buckets, Bucket};
   use waihona::types::blob::{Blob};
   use waihona::providers;
   use bytes::Bytes;
   let mut aws_blob = providers::aws::AwsBlob::get(
       "us-east-2", // Region
       "waihona", // Bucket name
       "example.txt", // Blob name
       None // Content range
       ).await
       .unwrap();
   let mut gcp_blob = providers::gcp::GcpBlob::get(
       "gcp-project-name", // Project name
       "waihona", // Bucket name
       "example.txt", // Blob name
       None // Content range
       ).await
       .unwrap();
   let content: Bytes = aws_blob.read().unwrap();
   gcp_blob.write(Some(content)).await.unwrap();
    aws_blob.delete().unwrap();
    gcp_blob
 }

许可证

该项目在MIT许可证下开放,允许学术和商业目的的广泛使用

依赖项

~6–24MB
~324K SLoC