| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
- import STSClient, { AssumeRoleRequest } from '@alicloud/sts20150401'
- import { Config } from '@alicloud/openapi-client'
- import { RuntimeOptions } from '@alicloud/tea-util'
- import Drive from '@ioc:Adonis/Core/Drive'
- import FilesService from 'App/Services/FilesService'
- export default class FilesController {
- public async store({ request }: HttpContextContract) {
- const file = request.file('file')
- await file?.moveToDisk('./uploads', {})
- // const signedUrl = await Drive.getSignedUrl(file?.fileName!)
- // await Drive.setVisibility(file?.fileName!, 'public')
- return { url: file?.filePath }
- }
- public async sts() {
- // const client = new STSClient({
- // credentials: {
- // accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
- // secretAccessKey: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk'
- // },
- // region: 'cn-hangzhou',
- // endpoint: 'https://sts.cn-hangzhou.aliyuncs.com'
- // // apiVersion: '2015-04-01'
- // })
- const client = new STSClient(
- new Config({
- accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
- accessKeySecret: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk',
- endpoint: 'sts.cn-hangzhou.aliyuncs.com'
- })
- )
- let assumeRoleRequest = new AssumeRoleRequest({
- durationSeconds: 1000,
- roleArn: 'acs:ram::1037005998695187:role/oss-read',
- roleSessionName: 'user'
- })
- try {
- // 复制代码运行请自行打印 API 的返回值
- return await client.assumeRoleWithOptions(assumeRoleRequest, new RuntimeOptions())
- } catch (error) {
- // 错误 message
- console.log(error)
- }
- }
- public async batchDelete({ request, response }: HttpContextContract) {
- try {
- const { filePaths } = request.only(['filePaths'])
- const result = await FilesService.batchDelete(filePaths)
- return response.ok({
- success: true,
- message: '批量删除操作完成',
- data: result
- })
- } catch (error) {
- return response.status(500).json({
- success: false,
- message: '批量删除操作失败',
- error: error.message
- })
- }
- }
- }
|