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 //! Contains enums, structs and functions for the Bunny Edge Storage API
use std::sync::Arc;
use crate::Error; use crate::Error;
use bytes::Bytes; use bytes::Bytes;
use reqwest::Client; use reqwest::{header::{HeaderMap, HeaderValue}, Client};
use serde::Deserialize; use serde::Deserialize;
use url::Url; use url::Url;
@ -94,7 +92,7 @@ pub struct ListFile {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Storage { pub struct Storage {
pub(crate) url: Url, pub(crate) url: Url,
pub(crate) reqwest: Arc<Client>, pub(crate) reqwest: Client,
} }
impl<'a> Storage { impl<'a> Storage {
@ -105,18 +103,25 @@ impl<'a> Storage {
/// ///
/// #[tokio::main] /// #[tokio::main]
/// async fn main() -> Result<(), Error> { /// 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?; /// 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(()) /// Ok(())
/// } /// }
/// ``` /// ```
pub fn init<T: AsRef<str>>( pub async fn init<T: AsRef<str>, T1: AsRef<str>>(
&mut self, &mut self,
api_key: T,
endpoint: Endpoint, endpoint: Endpoint,
storage_zone: T, storage_zone: T1,
) -> Result<(), Error> { ) -> 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 endpoint: Url = endpoint.try_into()?;
let storage_zone = String::from("/") + storage_zone.as_ref() + "/"; let storage_zone = String::from("/") + storage_zone.as_ref() + "/";
@ -134,9 +139,9 @@ impl<'a> Storage {
/// async fn main() -> Result<(), Error> { /// async fn main() -> Result<(), Error> {
/// let mut client = Client::new("api_key").await?; /// 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 /// // Will put a file in STORAGE_ZONE/images/file.png
/// client.storage.upload("/images/file.png", file_bytes).await?; /// client.storage.upload("/images/file.png", file_bytes).await?;
@ -173,13 +178,13 @@ impl<'a> Storage {
/// async fn main() -> Result<(), Error> { /// async fn main() -> Result<(), Error> {
/// let mut client = Client::new("api_key").await?; /// 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 /// // Will download the file STORAGE_ZONE/images/file.png
/// let contents = client.storage.download("/images/file.png").await?; /// let contents = client.storage.download("/images/file.png").await?;
/// ///
/// let mut file = fs::File::create("file.png").await?; /// let mut file = fs::File::create("file.png").await.unwrap();
/// file.write_all(contents).await?; /// file.write_all(contents).await.unwrap();
/// ///
/// Ok(()) /// Ok(())
/// } /// }
@ -210,7 +215,7 @@ impl<'a> Storage {
/// async fn main() -> Result<(), Error> { /// async fn main() -> Result<(), Error> {
/// let mut client = Client::new("api_key").await?; /// 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 /// // Will delete the file STORAGE_ZONE/images/file.png
/// client.storage.delete("/images/file.png").await?; /// client.storage.delete("/images/file.png").await?;
@ -243,7 +248,7 @@ impl<'a> Storage {
/// async fn main() -> Result<(), Error> { /// async fn main() -> Result<(), Error> {
/// let mut client = Client::new("api_key").await?; /// 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/ /// // Will list the files in STORAGE_ZONE/images/
/// let files = client.storage.list("/images/").await?; /// let files = client.storage.list("/images/").await?;

View file

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