aliyun-oss-react-native

index.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var express = require('express');
  2. var STS = require('ali-oss').STS;
  3. var co = require('co');
  4. var fs = require('fs');
  5. var app = express();
  6. app.get('/', function (req, res) {
  7. var conf = JSON.parse(fs.readFileSync('./config.json'));
  8. var policy;
  9. if (conf.PolicyFile) {
  10. policy = fs.readFileSync(conf.PolicyFile).toString('utf-8');
  11. }
  12. var client = new STS({
  13. accessKeyId: conf.AccessKeyId,
  14. accessKeySecret: conf.AccessKeySecret,
  15. });
  16. co(function* () {
  17. var result = yield client.assumeRole(conf.RoleArn, policy, conf.TokenExpireTime);
  18. console.log(result);
  19. res.set('Access-Control-Allow-Origin', '*');
  20. res.set('Access-Control-Allow-METHOD', 'GET');
  21. res.json({
  22. StatusCode: 200,
  23. AccessKeyId: result.credentials.AccessKeyId,
  24. AccessKeySecret: result.credentials.AccessKeySecret,
  25. SecurityToken: result.credentials.SecurityToken,
  26. Expiration: result.credentials.Expiration
  27. });
  28. }).then(function () {
  29. // pass
  30. }).catch(function (err) {
  31. res.json({
  32. StatusCode: 500,
  33. ErrorCode: err.code,
  34. ErrorMessage: err.message
  35. });
  36. });
  37. });
  38. app.listen(9000, function () {
  39. console.log('App started.');
  40. });