FilesController.ts 1.8 KB

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