From 78923d2661464ef75af2282a27d949f2e6f873e2 Mon Sep 17 00:00:00 2001 From: maslow Date: Fri, 8 Dec 2023 21:30:15 +0800 Subject: [PATCH] doc: update cloud.storage docs --- docs/.vitepress/config.js | 4 --- docs/guide/oss/gen-oss-url.md | 49 +++++------------------------------ 2 files changed, 7 insertions(+), 46 deletions(-) diff --git a/docs/.vitepress/config.js b/docs/.vitepress/config.js index 0f398e8f99..3cdb318cef 100644 --- a/docs/.vitepress/config.js +++ b/docs/.vitepress/config.js @@ -145,10 +145,6 @@ const guideSiderbarConfig = [ text: "操作数据库", link: "/guide/function/function-sdk#操作数据库", }, - { - text: "调用其他云函数", - link: "/guide/function/function-sdk#调用其他云函数", - }, { text: "云函数鉴权", link: "/guide/function/function-sdk#云函数鉴权", diff --git a/docs/guide/oss/gen-oss-url.md b/docs/guide/oss/gen-oss-url.md index 8c579f4c94..86c96512e4 100644 --- a/docs/guide/oss/gen-oss-url.md +++ b/docs/guide/oss/gen-oss-url.md @@ -15,30 +15,12 @@ title: 使用云函数生成上传和下载地址 ```typescript import cloud from '@lafjs/cloud' -import { S3, PutObjectCommand } from "@aws-sdk/client-s3" -import { getSignedUrl } from "@aws-sdk/s3-request-presigner" - - -const client = new S3({ - region: cloud.env.OSS_REGION, - endpoint: process.env.OSS_EXTERNAL_ENDPOINT, - credentials: { - accessKeyId: cloud.env.OSS_ACCESS_KEY, - secretAccessKey: cloud.env.OSS_ACCESS_SECRET, - }, - forcePathStyle: true, -}) - export default async function (ctx: FunctionContext) { - const bucketName = `${process.env.APPID}-cloud-bin` - - const command = new PutObjectCommand({ - Bucket: bucketName, - Key: 'laf.json', - }) + const bucket = cloud.storage.bucket('cloud-bin') - const url = await getSignedUrl(client, command, { expiresIn: 3600 * 24 }) + // 第二个参数为上传地址有效期,单位为秒, 3600 * 24 为 24 小时 + const url = bucket.getUploadUrl('laf.json', 3600 * 24) return url } @@ -64,29 +46,12 @@ curl -X PUT -H "Content-Type: application/json" -d '{"name":"hi, laf"}' $upload_ ```typescript import cloud from '@lafjs/cloud' -import { S3, GetObjectCommand } from "@aws-sdk/client-s3" -import { getSignedUrl } from "@aws-sdk/s3-request-presigner" - - -const client = new S3({ - region: cloud.env.OSS_REGION, - endpoint: process.env.OSS_EXTERNAL_ENDPOINT, - credentials: { - accessKeyId: cloud.env.OSS_ACCESS_KEY, - secretAccessKey: cloud.env.OSS_ACCESS_SECRET, - }, - forcePathStyle: true, -}) export default async function (ctx: FunctionContext) { - const bucketName = `${process.env.APPID}-cloud-bin` - - const command = new GetObjectCommand({ - Bucket: bucketName, - Key: 'laf.json', - }) - - const url = await getSignedUrl(client, command, { expiresIn: 3600 * 24 }) + const bucket = cloud.storage.bucket('cloud-bin') + + // 第二个参数为下载地址有效期,单位为秒, 3600 * 24 为 24 小时 + const url = await bucket.getDownloadUrl('test.html', 3600 * 24) return url }