fix: Use own API Key on edge_storage

This commit is contained in:
Radical 2025-05-24 02:58:55 +02:00
parent 52df8dae74
commit e55dd32305
2 changed files with 26 additions and 17 deletions

View file

@ -2,11 +2,9 @@
//!
//! Contains enums, structs and functions for the Bunny Edge Storage API
use std::sync::Arc;
use crate::Error;
use bytes::Bytes;
use reqwest::Client;
use reqwest::{header::{HeaderMap, HeaderValue}, Client};
use serde::Deserialize;
use url::Url;
@ -94,7 +92,7 @@ pub struct ListFile {
#[derive(Debug, Clone)]
pub struct Storage {
pub(crate) url: Url,
pub(crate) reqwest: Arc<Client>,
pub(crate) reqwest: Client,
}
impl<'a> Storage {
@ -105,18 +103,25 @@ impl<'a> Storage {
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
/// // API key here can be left as "" if you never plan on using anything from the bunny.net api
/// let mut client = Client::new("api_key").await?;
///
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
/// // Requires own API key to use
/// client.storage.init("storage_zone_api_key", Endpoint::Frankfurt, "MyStorageZone").await?;
///
/// Ok(())
/// }
/// ```
pub fn init<T: AsRef<str>>(
pub async fn init<T: AsRef<str>, T1: AsRef<str>>(
&mut self,
api_key: T,
endpoint: Endpoint,
storage_zone: T,
storage_zone: T1,
) -> Result<(), Error> {
let mut headers = HeaderMap::new();
headers.append("AccessKey", HeaderValue::from_str(api_key.as_ref())?);
self.reqwest = Client::builder().default_headers(headers).build()?;
let endpoint: Url = endpoint.try_into()?;
let storage_zone = String::from("/") + storage_zone.as_ref() + "/";
@ -134,9 +139,9 @@ impl<'a> Storage {
/// async fn main() -> Result<(), Error> {
/// let mut client = Client::new("api_key").await?;
///
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
/// client.storage.init("storage_zone_api_key", Endpoint::Frankfurt, "MyStorageZone").await?;
///
/// let file_bytes = fs::read("path/to/file.png").await?;
/// let file_bytes = fs::read("path/to/file.png").await.unwrap();
///
/// // Will put a file in STORAGE_ZONE/images/file.png
/// client.storage.upload("/images/file.png", file_bytes).await?;
@ -173,13 +178,13 @@ impl<'a> Storage {
/// async fn main() -> Result<(), Error> {
/// let mut client = Client::new("api_key").await?;
///
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
/// client.storage.init("storage_zone_api_key", Endpoint::Frankfurt, "MyStorageZone").await?;
///
/// // Will download the file STORAGE_ZONE/images/file.png
/// let contents = client.storage.download("/images/file.png").await?;
///
/// let mut file = fs::File::create("file.png").await?;
/// file.write_all(contents).await?;
/// let mut file = fs::File::create("file.png").await.unwrap();
/// file.write_all(contents).await.unwrap();
///
/// Ok(())
/// }
@ -210,7 +215,7 @@ impl<'a> Storage {
/// async fn main() -> Result<(), Error> {
/// let mut client = Client::new("api_key").await?;
///
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
/// client.storage.init("storage_zone_api_key", Endpoint::Frankfurt, "MyStorageZone").await?;
///
/// // Will delete the file STORAGE_ZONE/images/file.png
/// client.storage.delete("/images/file.png").await?;
@ -243,7 +248,7 @@ impl<'a> Storage {
/// async fn main() -> Result<(), Error> {
/// let mut client = Client::new("api_key").await?;
///
/// client.storage.init(Endpoint::Frankfurt, "MyStorageZone");
/// client.storage.init("storage_zone_api_key", Endpoint::Frankfurt, "MyStorageZone").await?;
///
/// // Will list the files in STORAGE_ZONE/images/
/// let files = client.storage.list("/images/").await?;

View file

@ -24,7 +24,6 @@ use reqwest::{
Client as RClient,
header::{HeaderMap, HeaderValue},
};
use std::sync::Arc;
use url::Url;
pub mod edge_storage;
@ -33,6 +32,7 @@ pub mod error;
/// API Client for bunny
#[derive(Debug, Clone)]
pub struct Client {
reqwest: RClient,
/// Used to interact with the Edge Storage API
pub storage: edge_storage::Storage,
}
@ -45,6 +45,7 @@ impl Client {
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
/// // Bunny.net api key
/// let mut client = Client::new("api_key").await?;
///
/// Ok(())
@ -53,13 +54,16 @@ impl Client {
pub async fn new<T: AsRef<str>>(api_key: T) -> Result<Self, Error> {
let mut headers = HeaderMap::new();
headers.append("AccessKey", HeaderValue::from_str(api_key.as_ref())?);
headers.append("accept", HeaderValue::from_str("application/json")?);
let reqwest = Arc::new(RClient::builder().default_headers(headers).build()?);
let reqwest = RClient::builder().default_headers(headers).build()?;
let storage_reqwest = RClient::new();
Ok(Self {
reqwest,
storage: edge_storage::Storage {
url: Url::parse("https://storage.bunnycdn.com").unwrap(),
reqwest,
reqwest: storage_reqwest,
},
})
}