FilesController.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import STSClient, { AssumeRoleRequest } from '@alicloud/sts20150401'
  3. import { Config } from '@alicloud/openapi-client'
  4. import { RuntimeOptions } from '@alicloud/tea-util'
  5. import Drive from '@ioc:Adonis/Core/Drive'
  6. import FilesService from 'App/Services/FilesService'
  7. export default class FilesController {
  8. public async store({ request }: HttpContextContract) {
  9. const file = request.file('file')
  10. await file?.moveToDisk('./uploads', {})
  11. // const signedUrl = await Drive.getSignedUrl(file?.fileName!)
  12. // await Drive.setVisibility(file?.fileName!, 'public')
  13. return { url: file?.filePath }
  14. }
  15. public async sts() {
  16. // const client = new STSClient({
  17. // credentials: {
  18. // accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
  19. // secretAccessKey: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk'
  20. // },
  21. // region: 'cn-hangzhou',
  22. // endpoint: 'https://sts.cn-hangzhou.aliyuncs.com'
  23. // // apiVersion: '2015-04-01'
  24. // })
  25. const client = new STSClient(
  26. new Config({
  27. accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
  28. accessKeySecret: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk',
  29. endpoint: 'sts.cn-hangzhou.aliyuncs.com'
  30. })
  31. )
  32. let assumeRoleRequest = new AssumeRoleRequest({
  33. durationSeconds: 1000,
  34. roleArn: 'acs:ram::1037005998695187:role/oss-read',
  35. roleSessionName: 'user'
  36. })
  37. try {
  38. // 复制代码运行请自行打印 API 的返回值
  39. return await client.assumeRoleWithOptions(assumeRoleRequest, new RuntimeOptions())
  40. } catch (error) {
  41. // 错误 message
  42. console.log(error)
  43. }
  44. }
  45. public async batchDelete({ request, response }: HttpContextContract) {
  46. try {
  47. const { filePaths } = request.only(['filePaths'])
  48. const result = await FilesService.batchDelete(filePaths)
  49. return response.ok({
  50. success: true,
  51. message: '批量删除操作完成',
  52. data: result
  53. })
  54. } catch (error) {
  55. return response.status(500).json({
  56. success: false,
  57. message: '批量删除操作失败',
  58. error: error.message
  59. })
  60. }
  61. }
  62. }