Skip to content

store ​

store 提供文件上传、下载、获取元数据和删除功能。通过预签名 URL 机制与服务端进行安全的文件传输。

导入:

typescript
import { store } from '@pc-nexus/bridge';

内置方法:

API描述
upload将一个或多个文件/Blob 对象上传到对象存储服务
download根据文件键名从对象存储服务下载文件
getMetadata获取指定文件的元数据信息
delete从对象存储服务中删除指定文件

upload ​

upload 将一个或多个文件/Blob 对象上传到对象存储服务。

函数签名 ​

typescript
function upload(functionKey: string, objects: (File | Blob)[]): Promise<UploadResult[]>;
  
interface UploadResult {
    success: boolean;
    key: string;
    status?: number;
    error?: string;
}

参数 ​

名称描述
functionKey用于获取预签名 URL 的函数标识
objects待上传的文件或 Blob 对象数组,不能为空

返回值 ​

返回值类型为 Promise<UploadResult[]>

UploadResult 类型定义如下:

名称描述
success上传是否成功
key文件在存储中的键名
statusHTTP 状态码
error错误信息(仅失败时存在)

示例 ​

客户端调用 :

typescript
import { store } from '@pc-nexus/bridge';

async function uploadFiles() {
  const fileInput = document.getElementById('fileInput') as HTMLInputElement;
  const files = Array.from(fileInput.files || []);

  const results = await store.upload(
    'filterAndGenerateUploadUrls',
    files
  )

  console.log(results);
}

服务端 Resolver :

typescript
import {
    GenerateUploadUrlPayload,
    nos,
    PreSignedUrlResult,
    PresignedURLToObjectMetadataMap,
} from "@pc-nexus/storage";

function generateTimestampKey(metadata: GenerateUploadUrlPayload, userId: string): string {
    const timestamp = Date.now();
    return `user-${userId}/${timestamp}`;
}

resolver.define(
    "filterAndGenerateUploadUrls",
    async (context: NexusAppContext, payload: GenerateUploadUrlPayload[]): Promise < PresignedURLToObjectMetadataMap > => {
        const userId = context.user?.id;
        const results = await Promise.all(
            payload.map(async (metadata) => {
                const {
                    checksum,
                    checksum_type,
                    size
                } = metadata;
                const key = generateTimestampKey(metadata, userId as string);
                const presignedUrl: PreSignedUrlResult = await nos.createUploadUrl({
                    key,
                    size,
                    checksum,
                    checksum_type
                }, {
                    overwrite: false,
                });

                return {
                    presignedUrl,
                    metadata: {
                        key,
                        size,
                        checksum,
                        checksum_type,
                        overwrite: false,
                    },
                };
            }),
        );
        const presignedURLsToObjectMetadata: PresignedURLToObjectMetadataMap = {};
        results.forEach((result) => {
            presignedURLsToObjectMetadata[typeof result.presignedUrl === "string" ? result.presignedUrl : result.presignedUrl.url] =
                result.metadata;
        });

        return presignedURLsToObjectMetadata;
    },
);

download ​

download 根据文件键名从对象存储服务下载文件。

函数签名 ​

typescript
function download(functionKey: string, keys: string[]): Promise<DownloadResult[]>;

interface DownloadResult {
    success: boolean;
    key: string;
    blob?: Blob;
    name?: string;
    status?: number;
    error?: string;
}

参数 ​

名称描述
functionKey用于获取下载 URL 的函数标识
keys待下载文件的键名数组,不能为空

返回值 ​

返回值类型为 Promise<DownloadResult[]>

DownloadResult 类型定义如下:

名称描述
success下载是否成功
key文件在对象存储中的唯一键名/路径
blob下载的文件内容(仅成功时存在)
name文件名
statusHTTP 状态码
error错误信息(仅失败时存在)

示例 ​

客户端调用 :

typescript
import { store } from '@pc-nexus/bridge';

async function download() {
    const fileKeys = [
        'shared/documents/invoice-001.pdf',
        'shared/documents/invoice-002.pdf',
        'shared/documents/invoice-003.pdf',
    ];
    const result = await store.download('filterAndGenerateDownloadUrls', fileKeys);
    console.log('download result', result);
}

服务端 Resolver :

typescript
import {
    nos,
    PreSignedUrlResult,
    PresignedURLToObjectKeyMap
} from "@pc-nexus/storage";

resolver.define(
    "filterAndGenerateDownloadUrls",
    async (context: NexusAppContext, payload: string[]): Promise < PresignedURLToObjectKeyMap > => {
        const userId = context.user?.id;
        const results = await Promise.all(
            payload
            .filter((key) => key.startsWith(`user-${userId}/`) || key.startsWith("shared/"))
            .map(async (key) => {
                const downloadUrl: PreSignedUrlResult = await nos.createDownloadUrl(key);
                return {
                    downloadUrl,
                    key
                };
            }),
        );
        const downloadUrlsToKeys: PresignedURLToObjectKeyMap = {};
        results.forEach((result) => {
            downloadUrlsToKeys[result.downloadUrl.url] = result.key;
        });
        return downloadUrlsToKeys;
    },
);

getMetadata ​

getMetadata 获取指定文件的元数据信息。

函数签名 ​

typescript
function getMetadata(functionKey: string, keys: string[]): Promise<GetMetadataResult[]>;

export interface GetMetadataResult {
    key: string;
    name: string;
    mime_type: string;
    checksum: string;
    size: number;
    created_at: string;
    error?: string;
}

参数 ​

名称描述
functionKey用于获取元数据的函数标识
keys待查询的文件键名数组,不能为空

返回值 ​

返回值类型为 Promise<GetMetadataResult[]>

GetMetadataResult 类型定义如下:

名称描述
key文件在对象存储中的唯一键名/路径
name文件名
mime_type文件的 MIME 类型(如 image/jpeg 、 application/pdf )
checksum文件的校验和值(通常为 SHA256 编码)
size文件大小
created_at文件创建时间
error错误信息(仅失败时存在)

示例 ​

客户端调用 :

typescript
import { store } from '@pc-nexus/bridge';

async function getObjectMetadata() {
    const fileKeys = [
        'shared/documents/report-2025.pdf',
        'shared/documents/invoice-001.pdf',
        'shared/images/logo.png',
        'shared/videos/tutorial.mp4',
        'shared/data/export.json',
    ];

    const result = await store.getMetadata('getObjectMetadata', fileKeys);
    console.log('get metadata result', result);
}

服务端 Resolver :

typescript
import { nos, ObjectMetadata } from "@pc-nexus/storage";

resolver.define < string > ("getObjectMetadata", async (context: NexusAppContext, payload: string): Promise < ObjectMetadata > => {
    const object: ObjectMetadata = await nos.getMetadata(payload);
    return object;
});

delete ​

delete 从对象存储服务中删除指定文件。

函数签名 ​

typescript
function delete(functionKey: string, keys: string[]): Promise<void>;

参数 ​

名称描述
functionKey用于删除文件的函数标识
keys待删除文件的键名数组,不能为空

返回值 ​

空

示例 ​

客户端调用 :

typescript
import { store } from '@pc-nexus/bridge';

async function delete() {
    const fileKeys = [
        'shared/documents/report-2025.pdf',
        'shared/documents/invoice-001.pdf',
        'shared/images/logo.png',
        'shared/videos/tutorial.mp4',
        'shared/data/export.json',
    ];
    const result = await store.delete('deleteObject', fileKeys);
    console.log('delete result', result);
}

服务端 Resolver :

typescript
import { nos } from "@pc-nexus/storage";

resolver.define < string > ("deleteObject", async (context: NexusAppContext, payload: string) => {
    await nos.delete(payload);
    return null;
});

举报邮箱:abuse@worktile.com