FilesController.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. export default class FilesController {
  6. public async store({ request }: HttpContextContract) {
  7. const file = request.file('file')
  8. await file?.moveToDisk('./uploads', { visibility: 'public' })
  9. // const signedUrl = await Drive.getSignedUrl(file?.fileName!)
  10. return { url: file?.filePath }
  11. }
  12. public async sts() {
  13. // const client = new STSClient({
  14. // credentials: {
  15. // accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
  16. // secretAccessKey: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk'
  17. // },
  18. // region: 'cn-hangzhou',
  19. // endpoint: 'https://sts.cn-hangzhou.aliyuncs.com'
  20. // // apiVersion: '2015-04-01'
  21. // })
  22. const client = new STSClient(
  23. new Config({
  24. accessKeyId: 'LTAI5tQYh51ihAYExgrrs7Fu',
  25. accessKeySecret: 'fpEdyOcPWsDSEwCjKkZgnEC8ZKnmAk',
  26. endpoint: 'sts.cn-hangzhou.aliyuncs.com'
  27. })
  28. )
  29. let assumeRoleRequest = new AssumeRoleRequest({
  30. durationSeconds: 1000,
  31. roleArn: 'acs:ram::1037005998695187:role/oss-read',
  32. roleSessionName: 'user'
  33. })
  34. try {
  35. // 复制代码运行请自行打印 API 的返回值
  36. return await client.assumeRoleWithOptions(assumeRoleRequest, new RuntimeOptions())
  37. } catch (error) {
  38. // 错误 message
  39. console.log(error)
  40. }
  41. }
  42. }