FilesController.ts 1.7 KB

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