Skip to content

Commit

Permalink
doc: update cloud.storage docs
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Dec 8, 2023
1 parent 18afa9b commit 78923d2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
4 changes: 0 additions & 4 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ const guideSiderbarConfig = [
text: "操作数据库",
link: "/guide/function/function-sdk#操作数据库",
},
{
text: "调用其他云函数",
link: "/guide/function/function-sdk#调用其他云函数",
},
{
text: "云函数鉴权",
link: "/guide/function/function-sdk#云函数鉴权",
Expand Down
49 changes: 7 additions & 42 deletions docs/guide/oss/gen-oss-url.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 78923d2

Please sign in to comment.