@bunny.net/ storage-sdk (英语)
这个 @bunny.net/storage-sdk 一个帮助您互动的库
BunnyCDN存储 API.
兔子存储 SDK
此寄存器包含 @bunny.net/storage-sdk,一个旨在简化BunnyCDN存储 API的用法的库.
使用量
与 @bunny.net/storage-sdk您可以与 BunnyCDN 存储 API 交互。 下面是帮助您建立本地服务器的简单例子。 关于其他实例和使用案例,请参见: 示例文件夹。 。 。 .
在您的存储区域中列出文件
import * as BunnySDK from "@bunny.net/edgescript-sdk";
import * as BunnyStorageSDK from "@bunny.net/storage-sdk";
let sz_zone = process.env.STORAGE_ZONE!;
let access_key = process.env.STORAGE_ACCESS_KEY!;
let sz = BunnyStorageSDK.zone.connect_with_accesskey(BunnyStorageSDK.regions.StorageRegion.Falkenstein, sz_zone, access_key);
console.log("Starting server...");
BunnySDK.net.http.serve({ port: 8080, hostname: '127.0.0.1' }, async (req) => {
let list = await BunnyStorageSDK.file.list(sz, "/");
console.log(`[INFO]: ${req.method} - ${req.url}`);
return new Response(JSON.stringify(list));
});
此示例使用 Bunny Edge 脚本 SDK 设置了本地的 HTTP 服务器,使用 BunnyCDN 存储 SDK 在存储区列出文件 。 您可以在127.0.0.1:8080访问服务器并观察实时请求日志.
快速启动
获取文件
获取文件时, 您可以获取文件的元数据, 或是内容 文档.
元数据描述文件.
import * as BunnyStorageSDK from "@bunny.net/storage-sdk";
let storageZone = BunnyStorageSDK.zone.connect_with_accesskey(BunnyStorageSDK.regions.StorageRegion.Falkenstein, "storage-zone-name", "token")
let obj = await BunnyStorageSDK.file.get(storageZone, "/my-folder/my-file");
/*
* Here obj will be equal to something like this:
const obj = {
Guid: '123',
UserId: 'user1',
LastChanged: '2023-01-01T00:00:00Z',
DateCreated: '2022-01-01T00:00:00Z',
StorageZoneName: 'test-zone',
Path: '/test/path',
ObjectName: 'test-file.txt',
Length: 100,
StorageZoneId: 1,
IsDirectory: false,
ServerId: 1,
Checksum: 'abc123',
ReplicatedZones: 'UK,NY',
ContentType: 'text/plain',
data: () => Promise<{
stream: ReadableStream<Uint8Array>;
response: Response;
length?: number;
}>,
};
*/
在此元数据中, 您可以使用 await obj.data()。 。 。 .
列出文件
您可以使用 :
let list = await BunnyStorageSDK.file.list(sz, "/");
这将给你一个可以浏览的文件元数据列表.
上传文件
要上传一个文件,我们就用杠杆 流线 以允许的方式上传文件 在不提供完整内容流的情况下上传文件.
export async function upload(storageZone: StorageZone.StorageZone, path: string, stream: ReadableStream<Uint8Array>, options?: UploadOptions): Promise<boolean>;
export async function upload(storageZone: StorageZone.StorageZone, path: string, stream: ReadableStream<Uint8Array>): Promise<boolean>;
export async function upload(storageZone: StorageZone.StorageZone, path: string, stream: ReadableStream<Uint8Array>, options?: UploadOptions): Promise<boolean>;
export type UploadOptions = {
/**
* The SHA256 Checksum associated to the data you want to send. If null then
* the server will automatically calculate it.
*/
sha256Checksum?: string;
/**
* You can override the content-type of the file you upload with this option.
*/
contentType?: string;
};
示例
await BunnyStorageSDK.file.upload(sz, "/some-file", random_bytes_10kb);
下载文件
要压低文件, 你有两种选择, 要么使用此功能 直接下载.
export async function download(storageZone: StorageZone.StorageZone, path: string): Promise<{
stream: ReadableStream<Uint8Array>;
response: Response;
length?: number;
}>;
示例
await BunnyStorageSDK.file.download(sz, "/some-file");
你会有内容流 和相关的反应 和长度 如果有的话.
你也可以用这个 data 文件元数据中可用的函数.
删除文件或目录
export async function remove(storageZone: StorageZone.StorageZone, path: string): Promise<boolean>;
export async function removeDirectory(storageZone: StorageZone.StorageZone, path: string): Promise<boolean>;
示例
await BunnyStorageSDK.file.remove(sz, "/some-file");