oss.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. local aliyun_oss = require("aliyunoss")
  2. local fw = require("fastweb")
  3. local utils = require("utils")
  4. local M = {}
  5. -- 文件扩展名与目录的映射
  6. M.ext_dirpath = {
  7. images = {
  8. "jpg",
  9. "jpeg",
  10. "png",
  11. "gif",
  12. "bmp",
  13. "webp",
  14. "svg",
  15. "ico",
  16. "tif",
  17. "tiff",
  18. },
  19. videos = {
  20. "mp4",
  21. "avi",
  22. "mov",
  23. "wmv",
  24. "flv"
  25. },
  26. audio = {
  27. "mp3",
  28. "wav",
  29. "ogg",
  30. "aac",
  31. "m4a",
  32. "wma"
  33. },
  34. documents = {
  35. "pdf",
  36. "doc",
  37. "docx",
  38. "xls",
  39. "xlsx",
  40. "ppt",
  41. "pptx",
  42. "txt",
  43. "json",
  44. "xml",
  45. "html",
  46. "css",
  47. "js",
  48. }
  49. }
  50. function M.upload_file(config,remote_dir,local_filepath,auto_remove)
  51. if not utils.exists_file(local_filepath) then
  52. return false,"文件不存在,local_filepath: "..local_filepath
  53. end
  54. local ext = utils.ext(local_filepath)
  55. local filename = fw.make_software_guid().."."..ext
  56. local dirpath = ""
  57. for dir, ext_list in pairs(M.ext_dirpath) do
  58. for _, ext_type_ext in ipairs(ext_list) do
  59. if ext_type_ext == ext then
  60. dirpath = dir
  61. break
  62. end
  63. end
  64. if dirpath ~= "" then
  65. break
  66. end
  67. end
  68. if dirpath == "" then
  69. return false,"不支持的文件格式,ext: "..ext
  70. end
  71. if remote_dir == nil or remote_dir == "" then
  72. remote_dir = ""
  73. else
  74. remote_dir = remote_dir.."/"
  75. end
  76. local cos_filepath = remote_dir .. dirpath.."/"..filename
  77. local oss = aliyun_oss.new()
  78. local result = oss:upfile(
  79. config.endpoint,
  80. config.access_key_id,
  81. config.access_key_secret,
  82. config.bucket_name,
  83. cos_filepath,
  84. local_filepath,
  85. ""
  86. )
  87. if result == "" then
  88. if auto_remove then
  89. utils.delete_file(local_filepath)
  90. end
  91. return true,cos_filepath
  92. end
  93. return false,result
  94. end
  95. function M.upload_data(config,remote_dir,data,ext)
  96. if #data == 0 then
  97. return false,"数据为空"
  98. end
  99. local filename = fw.make_software_guid().."."..ext
  100. local dirpath = ""
  101. for dir, ext_list in pairs(M.ext_dirpath) do
  102. for _, ext_type_ext in ipairs(ext_list) do
  103. if ext_type_ext == ext then
  104. dirpath = dir
  105. break
  106. end
  107. end
  108. if dirpath ~= "" then
  109. break
  110. end
  111. end
  112. if dirpath == "" then
  113. return false,"不支持的文件格式,ext: "..ext
  114. end
  115. if remote_dir == nil or remote_dir == "" then
  116. remote_dir = ""
  117. else
  118. remote_dir = remote_dir.."/"
  119. end
  120. local cos_filepath = remote_dir .. dirpath.."/"..filename
  121. local oss = aliyun_oss.new()
  122. local result = oss:updata(
  123. config.endpoint,
  124. config.access_key_id,
  125. config.access_key_secret,
  126. config.bucket_name,
  127. cos_filepath,
  128. data
  129. )
  130. if result == "" then
  131. return true,cos_filepath
  132. end
  133. return false,result
  134. end
  135. return M