utils.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. package oss
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "hash/crc32"
  7. "hash/crc64"
  8. "net/http"
  9. "os"
  10. "os/exec"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // userAgent gets user agent
  17. // It has the SDK version information, OS information and GO version
  18. func userAgent() string {
  19. sys := getSysInfo()
  20. return fmt.Sprintf("aliyun-sdk-go/%s (%s/%s/%s;%s)", Version, sys.name,
  21. sys.release, sys.machine, runtime.Version())
  22. }
  23. type sysInfo struct {
  24. name string // OS name such as windows/Linux
  25. release string // OS version 2.6.32-220.23.2.ali1089.el5.x86_64 etc
  26. machine string // CPU type amd64/x86_64
  27. }
  28. // getSysInfo gets system info
  29. // gets the OS information and CPU type
  30. func getSysInfo() sysInfo {
  31. name := runtime.GOOS
  32. release := "-"
  33. machine := runtime.GOARCH
  34. if out, err := exec.Command("uname", "-s").CombinedOutput(); err == nil {
  35. name = string(bytes.TrimSpace(out))
  36. }
  37. if out, err := exec.Command("uname", "-r").CombinedOutput(); err == nil {
  38. release = string(bytes.TrimSpace(out))
  39. }
  40. if out, err := exec.Command("uname", "-m").CombinedOutput(); err == nil {
  41. machine = string(bytes.TrimSpace(out))
  42. }
  43. return sysInfo{name: name, release: release, machine: machine}
  44. }
  45. // unpackedRange
  46. type unpackedRange struct {
  47. hasStart bool // Flag indicates if the start point is specified
  48. hasEnd bool // Flag indicates if the end point is specified
  49. start int64 // Start point
  50. end int64 // End point
  51. }
  52. // invalidRangeError returns invalid range error
  53. func invalidRangeError(r string) error {
  54. return fmt.Errorf("InvalidRange %s", r)
  55. }
  56. // parseRange parse various styles of range such as bytes=M-N
  57. func parseRange(normalizedRange string) (*unpackedRange, error) {
  58. var err error
  59. hasStart := false
  60. hasEnd := false
  61. var start int64
  62. var end int64
  63. // Bytes==M-N or ranges=M-N
  64. nrSlice := strings.Split(normalizedRange, "=")
  65. if len(nrSlice) != 2 || nrSlice[0] != "bytes" {
  66. return nil, invalidRangeError(normalizedRange)
  67. }
  68. // Bytes=M-N,X-Y
  69. rSlice := strings.Split(nrSlice[1], ",")
  70. rStr := rSlice[0]
  71. if strings.HasSuffix(rStr, "-") { // M-
  72. startStr := rStr[:len(rStr)-1]
  73. start, err = strconv.ParseInt(startStr, 10, 64)
  74. if err != nil {
  75. return nil, invalidRangeError(normalizedRange)
  76. }
  77. hasStart = true
  78. } else if strings.HasPrefix(rStr, "-") { // -N
  79. len := rStr[1:]
  80. end, err = strconv.ParseInt(len, 10, 64)
  81. if err != nil {
  82. return nil, invalidRangeError(normalizedRange)
  83. }
  84. if end == 0 { // -0
  85. return nil, invalidRangeError(normalizedRange)
  86. }
  87. hasEnd = true
  88. } else { // M-N
  89. valSlice := strings.Split(rStr, "-")
  90. if len(valSlice) != 2 {
  91. return nil, invalidRangeError(normalizedRange)
  92. }
  93. start, err = strconv.ParseInt(valSlice[0], 10, 64)
  94. if err != nil {
  95. return nil, invalidRangeError(normalizedRange)
  96. }
  97. hasStart = true
  98. end, err = strconv.ParseInt(valSlice[1], 10, 64)
  99. if err != nil {
  100. return nil, invalidRangeError(normalizedRange)
  101. }
  102. hasEnd = true
  103. }
  104. return &unpackedRange{hasStart, hasEnd, start, end}, nil
  105. }
  106. // adjustRange returns adjusted range, adjust the range according to the length of the file
  107. func adjustRange(ur *unpackedRange, size int64) (start, end int64) {
  108. if ur == nil {
  109. return 0, size
  110. }
  111. if ur.hasStart && ur.hasEnd {
  112. start = ur.start
  113. end = ur.end + 1
  114. if ur.start < 0 || ur.start >= size || ur.end > size || ur.start > ur.end {
  115. start = 0
  116. end = size
  117. }
  118. } else if ur.hasStart {
  119. start = ur.start
  120. end = size
  121. if ur.start < 0 || ur.start >= size {
  122. start = 0
  123. }
  124. } else if ur.hasEnd {
  125. start = size - ur.end
  126. end = size
  127. if ur.end < 0 || ur.end > size {
  128. start = 0
  129. end = size
  130. }
  131. }
  132. return
  133. }
  134. // GetNowSec returns Unix time, the number of seconds elapsed since January 1, 1970 UTC.
  135. // gets the current time in Unix time, in seconds.
  136. func GetNowSec() int64 {
  137. return time.Now().Unix()
  138. }
  139. // GetNowNanoSec returns t as a Unix time, the number of nanoseconds elapsed
  140. // since January 1, 1970 UTC. The result is undefined if the Unix time
  141. // in nanoseconds cannot be represented by an int64. Note that this
  142. // means the result of calling UnixNano on the zero Time is undefined.
  143. // gets the current time in Unix time, in nanoseconds.
  144. func GetNowNanoSec() int64 {
  145. return time.Now().UnixNano()
  146. }
  147. // GetNowGMT gets the current time in GMT format.
  148. func GetNowGMT() string {
  149. return time.Now().UTC().Format(http.TimeFormat)
  150. }
  151. // FileChunk is the file chunk definition
  152. type FileChunk struct {
  153. Number int // Chunk number
  154. Offset int64 // Chunk offset
  155. Size int64 // Chunk size.
  156. }
  157. // SplitFileByPartNum splits big file into parts by the num of parts.
  158. // Split the file with specified parts count, returns the split result when error is nil.
  159. func SplitFileByPartNum(fileName string, chunkNum int) ([]FileChunk, error) {
  160. if chunkNum <= 0 || chunkNum > 10000 {
  161. return nil, errors.New("chunkNum invalid")
  162. }
  163. file, err := os.Open(fileName)
  164. if err != nil {
  165. return nil, err
  166. }
  167. defer file.Close()
  168. stat, err := file.Stat()
  169. if err != nil {
  170. return nil, err
  171. }
  172. if int64(chunkNum) > stat.Size() {
  173. return nil, errors.New("oss: chunkNum invalid")
  174. }
  175. var chunks []FileChunk
  176. var chunk = FileChunk{}
  177. var chunkN = (int64)(chunkNum)
  178. for i := int64(0); i < chunkN; i++ {
  179. chunk.Number = int(i + 1)
  180. chunk.Offset = i * (stat.Size() / chunkN)
  181. if i == chunkN-1 {
  182. chunk.Size = stat.Size()/chunkN + stat.Size()%chunkN
  183. } else {
  184. chunk.Size = stat.Size() / chunkN
  185. }
  186. chunks = append(chunks, chunk)
  187. }
  188. return chunks, nil
  189. }
  190. // SplitFileByPartSize splits big file into parts by the size of parts.
  191. // Splits the file by the part size. Returns the FileChunk when error is nil.
  192. func SplitFileByPartSize(fileName string, chunkSize int64) ([]FileChunk, error) {
  193. if chunkSize <= 0 {
  194. return nil, errors.New("chunkSize invalid")
  195. }
  196. file, err := os.Open(fileName)
  197. if err != nil {
  198. return nil, err
  199. }
  200. defer file.Close()
  201. stat, err := file.Stat()
  202. if err != nil {
  203. return nil, err
  204. }
  205. var chunkN = stat.Size() / chunkSize
  206. if chunkN >= 10000 {
  207. return nil, errors.New("Too many parts, please increase part size")
  208. }
  209. var chunks []FileChunk
  210. var chunk = FileChunk{}
  211. for i := int64(0); i < chunkN; i++ {
  212. chunk.Number = int(i + 1)
  213. chunk.Offset = i * chunkSize
  214. chunk.Size = chunkSize
  215. chunks = append(chunks, chunk)
  216. }
  217. if stat.Size()%chunkSize > 0 {
  218. chunk.Number = len(chunks) + 1
  219. chunk.Offset = int64(len(chunks)) * chunkSize
  220. chunk.Size = stat.Size() % chunkSize
  221. chunks = append(chunks, chunk)
  222. }
  223. return chunks, nil
  224. }
  225. // GetPartEnd calculates the end position
  226. func GetPartEnd(begin int64, total int64, per int64) int64 {
  227. if begin+per > total {
  228. return total - 1
  229. }
  230. return begin + per - 1
  231. }
  232. // crcTable returns the table constructed from the specified polynomial
  233. var crcTable = func() *crc64.Table {
  234. return crc64.MakeTable(crc64.ECMA)
  235. }
  236. // crcTable returns the table constructed from the specified polynomial
  237. var crc32Table = func() *crc32.Table {
  238. return crc32.MakeTable(crc32.IEEE)
  239. }
  240. // choiceTransferPartOption choices valid option supported by Uploadpart or DownloadPart
  241. func ChoiceTransferPartOption(options []Option) []Option {
  242. var outOption []Option
  243. listener, _ := findOption(options, progressListener, nil)
  244. if listener != nil {
  245. outOption = append(outOption, Progress(listener.(ProgressListener)))
  246. }
  247. payer, _ := findOption(options, HTTPHeaderOssRequester, nil)
  248. if payer != nil {
  249. outOption = append(outOption, RequestPayer(PayerType(payer.(string))))
  250. }
  251. versionId, _ := findOption(options, "versionId", nil)
  252. if versionId != nil {
  253. outOption = append(outOption, VersionId(versionId.(string)))
  254. }
  255. trafficLimit, _ := findOption(options, HTTPHeaderOssTrafficLimit, nil)
  256. if trafficLimit != nil {
  257. speed, _ := strconv.ParseInt(trafficLimit.(string), 10, 64)
  258. outOption = append(outOption, TrafficLimitHeader(speed))
  259. }
  260. respHeader, _ := findOption(options, responseHeader, nil)
  261. if respHeader != nil {
  262. outOption = append(outOption, GetResponseHeader(respHeader.(*http.Header)))
  263. }
  264. return outOption
  265. }
  266. // ChoiceCompletePartOption choices valid option supported by CompleteMulitiPart
  267. func ChoiceCompletePartOption(options []Option) []Option {
  268. var outOption []Option
  269. listener, _ := findOption(options, progressListener, nil)
  270. if listener != nil {
  271. outOption = append(outOption, Progress(listener.(ProgressListener)))
  272. }
  273. payer, _ := findOption(options, HTTPHeaderOssRequester, nil)
  274. if payer != nil {
  275. outOption = append(outOption, RequestPayer(PayerType(payer.(string))))
  276. }
  277. acl, _ := findOption(options, HTTPHeaderOssObjectACL, nil)
  278. if acl != nil {
  279. outOption = append(outOption, ObjectACL(ACLType(acl.(string))))
  280. }
  281. callback, _ := findOption(options, HTTPHeaderOssCallback, nil)
  282. if callback != nil {
  283. outOption = append(outOption, Callback(callback.(string)))
  284. }
  285. callbackVar, _ := findOption(options, HTTPHeaderOssCallbackVar, nil)
  286. if callbackVar != nil {
  287. outOption = append(outOption, CallbackVar(callbackVar.(string)))
  288. }
  289. respHeader, _ := findOption(options, responseHeader, nil)
  290. if respHeader != nil {
  291. outOption = append(outOption, GetResponseHeader(respHeader.(*http.Header)))
  292. }
  293. forbidOverWrite, _ := findOption(options, HTTPHeaderOssForbidOverWrite, nil)
  294. if forbidOverWrite != nil {
  295. if forbidOverWrite.(string) == "true" {
  296. outOption = append(outOption, ForbidOverWrite(true))
  297. } else {
  298. outOption = append(outOption, ForbidOverWrite(false))
  299. }
  300. }
  301. return outOption
  302. }
  303. // ChoiceAbortPartOption choices valid option supported by AbortMultipartUpload
  304. func ChoiceAbortPartOption(options []Option) []Option {
  305. var outOption []Option
  306. payer, _ := findOption(options, HTTPHeaderOssRequester, nil)
  307. if payer != nil {
  308. outOption = append(outOption, RequestPayer(PayerType(payer.(string))))
  309. }
  310. respHeader, _ := findOption(options, responseHeader, nil)
  311. if respHeader != nil {
  312. outOption = append(outOption, GetResponseHeader(respHeader.(*http.Header)))
  313. }
  314. return outOption
  315. }
  316. // ChoiceHeadObjectOption choices valid option supported by HeadObject
  317. func ChoiceHeadObjectOption(options []Option) []Option {
  318. var outOption []Option
  319. // not select HTTPHeaderRange to get whole object length
  320. payer, _ := findOption(options, HTTPHeaderOssRequester, nil)
  321. if payer != nil {
  322. outOption = append(outOption, RequestPayer(PayerType(payer.(string))))
  323. }
  324. versionId, _ := findOption(options, "versionId", nil)
  325. if versionId != nil {
  326. outOption = append(outOption, VersionId(versionId.(string)))
  327. }
  328. respHeader, _ := findOption(options, responseHeader, nil)
  329. if respHeader != nil {
  330. outOption = append(outOption, GetResponseHeader(respHeader.(*http.Header)))
  331. }
  332. return outOption
  333. }
  334. func CheckBucketName(bucketName string) error {
  335. nameLen := len(bucketName)
  336. if nameLen < 3 || nameLen > 63 {
  337. return fmt.Errorf("bucket name %s len is between [3-63],now is %d", bucketName, nameLen)
  338. }
  339. for _, v := range bucketName {
  340. if !(('a' <= v && v <= 'z') || ('0' <= v && v <= '9') || v == '-') {
  341. return fmt.Errorf("bucket name %s can only include lowercase letters, numbers, and -", bucketName)
  342. }
  343. }
  344. if bucketName[0] == '-' || bucketName[nameLen-1] == '-' {
  345. return fmt.Errorf("bucket name %s must start and end with a lowercase letter or number", bucketName)
  346. }
  347. return nil
  348. }