You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

868 lines
28 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package cos
  2. // Basic imports
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "math/rand"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "strings"
  13. "testing"
  14. "time"
  15. "github.com/google/uuid"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/suite"
  18. "github.com/tencentyun/cos-go-sdk-v5"
  19. )
  20. // Define the suite, and absorb the built-in basic suite
  21. // functionality from testify - including a T() method which
  22. // returns the current testing context
  23. type CosTestSuite struct {
  24. suite.Suite
  25. VariableThatShouldStartAtFive int
  26. // CI client
  27. Client *cos.Client
  28. // Copy source client
  29. CClient *cos.Client
  30. Region string
  31. Bucket string
  32. Appid string
  33. // test_object
  34. TestObject string
  35. // special_file_name
  36. SepFileName string
  37. }
  38. // 请替换成您的账号及存储桶信息
  39. const (
  40. //uin
  41. kUin = "100010805041"
  42. kAppid = 1259654469
  43. // 常规测试需要的存储桶
  44. kBucket = "cosgosdktest-1259654469"
  45. kRegion = "ap-guangzhou"
  46. // 跨区域复制需要的目标存储桶,地域不能与kBucket存储桶相同。
  47. kRepBucket = "cosgosdkreptest"
  48. kRepRegion = "ap-chengdu"
  49. // Batch测试需要的源存储桶和目标存储桶,目前只在成都、重庆地域公测
  50. kBatchBucket = "testcd-1259654469"
  51. kTargetBatchBucket = "cosgosdkreptest-1259654469" //复用了存储桶
  52. kBatchRegion = "ap-chengdu"
  53. )
  54. func (s *CosTestSuite) SetupSuite() {
  55. fmt.Println("Set up test")
  56. // init
  57. s.TestObject = "test.txt"
  58. s.SepFileName = "中文" + "→↓←→↖↗↙↘! \"#$%&'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  59. // CI client for test interface
  60. // URL like this http://test-1253846586.cos.ap-guangzhou.myqcloud.com
  61. u := "https://" + kBucket + ".cos." + kRegion + ".myqcloud.com"
  62. u2 := "https://" + kUin + ".cos-control." + kBatchRegion + ".myqcloud.com"
  63. // Get the region
  64. bucketurl, _ := url.Parse(u)
  65. batchurl, _ := url.Parse(u2)
  66. p := strings.Split(bucketurl.Host, ".")
  67. assert.Equal(s.T(), 5, len(p), "Bucket host is not right")
  68. s.Region = p[2]
  69. // Bucket name
  70. pp := strings.Split(p[0], "-")
  71. s.Bucket = pp[0]
  72. s.Appid = pp[1]
  73. ib := &cos.BaseURL{BucketURL: bucketurl, BatchURL: batchurl}
  74. s.Client = cos.NewClient(ib, &http.Client{
  75. Transport: &cos.AuthorizationTransport{
  76. SecretID: os.Getenv("COS_SECRETID"),
  77. SecretKey: os.Getenv("COS_SECRETKEY"),
  78. },
  79. })
  80. opt := &cos.BucketPutOptions{
  81. XCosACL: "public-read",
  82. }
  83. r, err := s.Client.Bucket.Put(context.Background(), opt)
  84. if err != nil && r.StatusCode == 409 {
  85. fmt.Println("BucketAlreadyOwnedByYou")
  86. } else if err != nil {
  87. assert.Nil(s.T(), err, "PutBucket Failed")
  88. }
  89. }
  90. // Begin of api test
  91. // Service API
  92. func (s *CosTestSuite) TestGetService() {
  93. _, _, err := s.Client.Service.Get(context.Background())
  94. assert.Nil(s.T(), err, "GetService Failed")
  95. }
  96. // Bucket API
  97. func (s *CosTestSuite) TestPutHeadDeleteBucket() {
  98. // Notic sometimes the bucket host can not analyis, may has i/o timeout problem
  99. u := "http://" + "testgosdkbucket-create-head-del-" + s.Appid + ".cos." + kRegion + ".myqcloud.com"
  100. iu, _ := url.Parse(u)
  101. ib := &cos.BaseURL{BucketURL: iu}
  102. client := cos.NewClient(ib, &http.Client{
  103. Transport: &cos.AuthorizationTransport{
  104. SecretID: os.Getenv("COS_SECRETID"),
  105. SecretKey: os.Getenv("COS_SECRETKEY"),
  106. },
  107. })
  108. r, err := client.Bucket.Put(context.Background(), nil)
  109. if err != nil && r.StatusCode == 409 {
  110. fmt.Println("BucketAlreadyOwnedByYou")
  111. } else if err != nil {
  112. assert.Nil(s.T(), err, "PutBucket Failed")
  113. }
  114. if err != nil {
  115. panic(err)
  116. }
  117. time.Sleep(3 * time.Second)
  118. _, err = client.Bucket.Head(context.Background())
  119. assert.Nil(s.T(), err, "HeadBucket Failed")
  120. if err == nil {
  121. _, err = client.Bucket.Delete(context.Background())
  122. assert.Nil(s.T(), err, "DeleteBucket Failed")
  123. }
  124. }
  125. func (s *CosTestSuite) TestPutBucketACLIllegal() {
  126. opt := &cos.BucketPutACLOptions{
  127. Header: &cos.ACLHeaderOptions{
  128. XCosACL: "public-read-writ",
  129. },
  130. }
  131. _, err := s.Client.Bucket.PutACL(context.Background(), opt)
  132. assert.NotNil(s.T(), err, "PutBucketACL illegal Failed")
  133. }
  134. func (s *CosTestSuite) TestPutGetBucketACLNormal() {
  135. // with header
  136. opt := &cos.BucketPutACLOptions{
  137. Header: &cos.ACLHeaderOptions{
  138. XCosACL: "private",
  139. },
  140. }
  141. _, err := s.Client.Bucket.PutACL(context.Background(), opt)
  142. assert.Nil(s.T(), err, "PutBucketACL normal Failed")
  143. v, _, err := s.Client.Bucket.GetACL(context.Background())
  144. assert.Nil(s.T(), err, "GetBucketACL normal Failed")
  145. assert.Equal(s.T(), 1, len(v.AccessControlList), "GetBucketACL normal Failed, must be private")
  146. }
  147. func (s *CosTestSuite) TestGetBucket() {
  148. opt := &cos.BucketGetOptions{
  149. Prefix: "中文",
  150. MaxKeys: 3,
  151. }
  152. _, _, err := s.Client.Bucket.Get(context.Background(), opt)
  153. assert.Nil(s.T(), err, "GetBucket Failed")
  154. }
  155. func (s *CosTestSuite) TestGetBucketLocation() {
  156. v, _, err := s.Client.Bucket.GetLocation(context.Background())
  157. assert.Nil(s.T(), err, "GetLocation Failed")
  158. assert.Equal(s.T(), s.Region, v.Location, "GetLocation wrong region")
  159. }
  160. func (s *CosTestSuite) TestPutGetDeleteCORS() {
  161. opt := &cos.BucketPutCORSOptions{
  162. Rules: []cos.BucketCORSRule{
  163. {
  164. AllowedOrigins: []string{"http://www.qq.com"},
  165. AllowedMethods: []string{"PUT", "GET"},
  166. AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
  167. MaxAgeSeconds: 500,
  168. ExposeHeaders: []string{"x-cos-meta-test1"},
  169. },
  170. },
  171. }
  172. _, err := s.Client.Bucket.PutCORS(context.Background(), opt)
  173. assert.Nil(s.T(), err, "PutBucketCORS Failed")
  174. v, _, err := s.Client.Bucket.GetCORS(context.Background())
  175. assert.Nil(s.T(), err, "GetBucketCORS Failed")
  176. assert.Equal(s.T(), 1, len(v.Rules), "GetBucketCORS wrong number rules")
  177. }
  178. func (s *CosTestSuite) TestVersionAndReplication() {
  179. opt := &cos.BucketPutVersionOptions{
  180. // Enabled or Suspended, the versioning once opened can not close.
  181. Status: "Enabled",
  182. }
  183. _, err := s.Client.Bucket.PutVersioning(context.Background(), opt)
  184. assert.Nil(s.T(), err, "PutVersioning Failed")
  185. v, _, err := s.Client.Bucket.GetVersioning(context.Background())
  186. assert.Nil(s.T(), err, "GetVersioning Failed")
  187. assert.Equal(s.T(), "Enabled", v.Status, "Get Wrong Version status")
  188. repOpt := &cos.PutBucketReplicationOptions{
  189. // qcs::cam::uin/[UIN]:uin/[Subaccount]
  190. Role: "qcs::cam::uin/" + kUin + ":uin/" + kUin,
  191. Rule: []cos.BucketReplicationRule{
  192. {
  193. ID: "1",
  194. // Enabled or Disabled
  195. Status: "Enabled",
  196. Destination: &cos.ReplicationDestination{
  197. // qcs::cos:[Region]::[Bucketname-Appid]
  198. Bucket: "qcs::cos:" + kRepRegion + "::" + kRepBucket + "-" + s.Appid,
  199. },
  200. },
  201. },
  202. }
  203. _, err = s.Client.Bucket.PutBucketReplication(context.Background(), repOpt)
  204. assert.Nil(s.T(), err, "PutBucketReplication Failed")
  205. vr, _, err := s.Client.Bucket.GetBucketReplication(context.Background())
  206. assert.Nil(s.T(), err, "GetBucketReplication Failed")
  207. for _, r := range vr.Rule {
  208. assert.Equal(s.T(), "Enabled", r.Status, "Get Wrong Version status")
  209. assert.Equal(s.T(), "qcs::cos:"+kRepRegion+"::"+kRepBucket+"-"+s.Appid, r.Destination.Bucket, "Get Wrong Version status")
  210. }
  211. _, err = s.Client.Bucket.DeleteBucketReplication(context.Background())
  212. assert.Nil(s.T(), err, "DeleteBucketReplication Failed")
  213. }
  214. func (s *CosTestSuite) TestBucketInventory() {
  215. id := "test1"
  216. dBucket := "qcs::cos:" + s.Region + "::" + s.Bucket + "-" + s.Appid
  217. opt := &cos.BucketPutInventoryOptions{
  218. ID: id,
  219. // True or False
  220. IsEnabled: "True",
  221. IncludedObjectVersions: "All",
  222. Filter: &cos.BucketInventoryFilter{
  223. Prefix: "test",
  224. },
  225. OptionalFields: &cos.BucketInventoryOptionalFields{
  226. BucketInventoryFields: []string{
  227. "Size", "LastModifiedDate",
  228. },
  229. },
  230. Schedule: &cos.BucketInventorySchedule{
  231. // Weekly or Daily
  232. Frequency: "Daily",
  233. },
  234. Destination: &cos.BucketInventoryDestination{
  235. Bucket: dBucket,
  236. Format: "CSV",
  237. },
  238. }
  239. _, err := s.Client.Bucket.PutInventory(context.Background(), id, opt)
  240. assert.Nil(s.T(), err, "PutBucketInventory Failed")
  241. v, _, err := s.Client.Bucket.GetInventory(context.Background(), id)
  242. assert.Nil(s.T(), err, "GetBucketInventory Failed")
  243. assert.Equal(s.T(), "test1", v.ID, "Get Wrong inventory id")
  244. assert.Equal(s.T(), "true", v.IsEnabled, "Get Wrong inventory isenabled")
  245. assert.Equal(s.T(), dBucket, v.Destination.Bucket, "Get Wrong inventory isenabled")
  246. _, err = s.Client.Bucket.DeleteInventory(context.Background(), id)
  247. assert.Nil(s.T(), err, "DeleteBucketInventory Failed")
  248. }
  249. func (s *CosTestSuite) TestBucketLogging() {
  250. tBucket := s.Bucket + "-" + s.Appid
  251. opt := &cos.BucketPutLoggingOptions{
  252. LoggingEnabled: &cos.BucketLoggingEnabled{
  253. TargetBucket: tBucket,
  254. },
  255. }
  256. _, err := s.Client.Bucket.PutLogging(context.Background(), opt)
  257. assert.Nil(s.T(), err, "PutLogging Failed")
  258. v, _, err := s.Client.Bucket.GetLogging(context.Background())
  259. assert.Nil(s.T(), err, "GetLogging Failed")
  260. assert.Equal(s.T(), tBucket, v.LoggingEnabled.TargetBucket, "Get Wrong Version status")
  261. }
  262. func (s *CosTestSuite) TestBucketTagging() {
  263. opt := &cos.BucketPutTaggingOptions{
  264. TagSet: []cos.BucketTaggingTag{
  265. {
  266. Key: "testk1",
  267. Value: "testv1",
  268. },
  269. {
  270. Key: "testk2",
  271. Value: "testv2",
  272. },
  273. },
  274. }
  275. _, err := s.Client.Bucket.PutTagging(context.Background(), opt)
  276. assert.Nil(s.T(), err, "Put Tagging Failed")
  277. v, _, err := s.Client.Bucket.GetTagging(context.Background())
  278. assert.Nil(s.T(), err, "Get Tagging Failed")
  279. assert.Equal(s.T(), v.TagSet[0].Key, opt.TagSet[0].Key, "Get Wrong Tag key")
  280. assert.Equal(s.T(), v.TagSet[0].Value, opt.TagSet[0].Value, "Get Wrong Tag value")
  281. assert.Equal(s.T(), v.TagSet[1].Key, opt.TagSet[1].Key, "Get Wrong Tag key")
  282. assert.Equal(s.T(), v.TagSet[1].Value, opt.TagSet[1].Value, "Get Wrong Tag value")
  283. }
  284. func (s *CosTestSuite) TestPutGetDeleteLifeCycle() {
  285. lc := &cos.BucketPutLifecycleOptions{
  286. Rules: []cos.BucketLifecycleRule{
  287. {
  288. ID: "1234",
  289. Filter: &cos.BucketLifecycleFilter{Prefix: "test"},
  290. Status: "Enabled",
  291. Transition: &cos.BucketLifecycleTransition{
  292. Days: 10,
  293. StorageClass: "Standard",
  294. },
  295. },
  296. },
  297. }
  298. _, err := s.Client.Bucket.PutLifecycle(context.Background(), lc)
  299. assert.Nil(s.T(), err, "PutBucketLifecycle Failed")
  300. _, r, err := s.Client.Bucket.GetLifecycle(context.Background())
  301. // Might cleaned by other case concrrent
  302. if err != nil && 404 != r.StatusCode {
  303. assert.Nil(s.T(), err, "GetBucketLifecycle Failed")
  304. }
  305. _, err = s.Client.Bucket.DeleteLifecycle(context.Background())
  306. assert.Nil(s.T(), err, "DeleteBucketLifecycle Failed")
  307. }
  308. func (s *CosTestSuite) TestPutGetDeleteWebsite() {
  309. opt := &cos.BucketPutWebsiteOptions{
  310. Index: "index.html",
  311. Error: &cos.ErrorDocument{"index_backup.html"},
  312. RoutingRules: &cos.WebsiteRoutingRules{
  313. []cos.WebsiteRoutingRule{
  314. {
  315. ConditionErrorCode: "404",
  316. RedirectProtocol: "https",
  317. RedirectReplaceKey: "404.html",
  318. },
  319. {
  320. ConditionPrefix: "docs/",
  321. RedirectProtocol: "https",
  322. RedirectReplaceKeyPrefix: "documents/",
  323. },
  324. },
  325. },
  326. }
  327. _, err := s.Client.Bucket.PutWebsite(context.Background(), opt)
  328. assert.Nil(s.T(), err, "PutBucketWebsite Failed")
  329. res, rsp, err := s.Client.Bucket.GetWebsite(context.Background())
  330. if err != nil && 404 != rsp.StatusCode {
  331. assert.Nil(s.T(), err, "GetBucketWebsite Failed")
  332. }
  333. assert.Equal(s.T(), opt.Index, res.Index, "GetBucketWebsite Failed")
  334. assert.Equal(s.T(), opt.Error, res.Error, "GetBucketWebsite Failed")
  335. assert.Equal(s.T(), opt.RedirectProtocol, res.RedirectProtocol, "GetBucketWebsite Failed")
  336. _, err = s.Client.Bucket.DeleteWebsite(context.Background())
  337. assert.Nil(s.T(), err, "DeleteBucketWebsite Failed")
  338. }
  339. func (s *CosTestSuite) TestListMultipartUploads() {
  340. // Create new upload
  341. name := "test_multipart" + time.Now().Format(time.RFC3339)
  342. flag := false
  343. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  344. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  345. id := v.UploadID
  346. // List
  347. r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  348. assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  349. for _, p := range r.Uploads {
  350. if p.Key == name {
  351. assert.Equal(s.T(), id, p.UploadID, "ListMultipartUploads wrong uploadid")
  352. flag = true
  353. }
  354. }
  355. assert.Equal(s.T(), true, flag, "ListMultipartUploads wrong key")
  356. // Abort
  357. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, id)
  358. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  359. }
  360. // Object API
  361. func (s *CosTestSuite) TestPutHeadGetDeleteObject_10MB() {
  362. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  363. b := make([]byte, 1024*1024*10)
  364. _, err := rand.Read(b)
  365. content := fmt.Sprintf("%X", b)
  366. f := strings.NewReader(content)
  367. _, err = s.Client.Object.Put(context.Background(), name, f, nil)
  368. assert.Nil(s.T(), err, "PutObject Failed")
  369. _, err = s.Client.Object.Head(context.Background(), name, nil)
  370. assert.Nil(s.T(), err, "HeadObject Failed")
  371. _, err = s.Client.Object.Delete(context.Background(), name)
  372. assert.Nil(s.T(), err, "DeleteObject Failed")
  373. }
  374. func (s *CosTestSuite) TestPutGetDeleteObjectByFile_10MB() {
  375. // Create tmp file
  376. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  377. newfile, err := os.Create(filePath)
  378. assert.Nil(s.T(), err, "create tmp file Failed")
  379. defer newfile.Close()
  380. name := "test/objectPutByFile" + time.Now().Format(time.RFC3339)
  381. b := make([]byte, 1024*1024*10)
  382. _, err = rand.Read(b)
  383. newfile.Write(b)
  384. _, err = s.Client.Object.PutFromFile(context.Background(), name, filePath, nil)
  385. assert.Nil(s.T(), err, "PutObject Failed")
  386. // Over write tmp file
  387. _, err = s.Client.Object.GetToFile(context.Background(), name, filePath, nil)
  388. assert.Nil(s.T(), err, "HeadObject Failed")
  389. _, err = s.Client.Object.Delete(context.Background(), name)
  390. assert.Nil(s.T(), err, "DeleteObject Failed")
  391. // remove the local tmp file
  392. err = os.Remove(filePath)
  393. assert.Nil(s.T(), err, "remove local file Failed")
  394. }
  395. func (s *CosTestSuite) TestPutGetDeleteObjectSpecialName() {
  396. f := strings.NewReader("test")
  397. name := s.SepFileName + time.Now().Format(time.RFC3339)
  398. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  399. assert.Nil(s.T(), err, "PutObject Failed")
  400. resp, err := s.Client.Object.Get(context.Background(), name, nil)
  401. assert.Nil(s.T(), err, "GetObject Failed")
  402. defer resp.Body.Close()
  403. bs, _ := ioutil.ReadAll(resp.Body)
  404. assert.Equal(s.T(), "test", string(bs), "GetObject failed content wrong")
  405. _, err = s.Client.Object.Delete(context.Background(), name)
  406. assert.Nil(s.T(), err, "DeleteObject Failed")
  407. }
  408. func (s *CosTestSuite) TestPutObjectToNonExistBucket() {
  409. u := "http://gosdknonexistbucket-" + s.Appid + ".cos." + s.Region + ".myqcloud.com"
  410. iu, _ := url.Parse(u)
  411. ib := &cos.BaseURL{BucketURL: iu}
  412. client := cos.NewClient(ib, &http.Client{
  413. Transport: &cos.AuthorizationTransport{
  414. SecretID: os.Getenv("COS_SECRETID"),
  415. SecretKey: os.Getenv("COS_SECRETKEY"),
  416. },
  417. })
  418. name := "test/objectPut.go"
  419. f := strings.NewReader("test")
  420. r, err := client.Object.Put(context.Background(), name, f, nil)
  421. assert.NotNil(s.T(), err, "PutObject ToNonExistBucket Failed")
  422. assert.Equal(s.T(), 404, r.StatusCode, "PutObject ToNonExistBucket, not 404")
  423. }
  424. func (s *CosTestSuite) TestPutGetObjectACL() {
  425. name := "test/objectACL.go" + time.Now().Format(time.RFC3339)
  426. f := strings.NewReader("test")
  427. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  428. assert.Nil(s.T(), err, "PutObject Failed")
  429. // Put acl
  430. opt := &cos.ObjectPutACLOptions{
  431. Header: &cos.ACLHeaderOptions{
  432. XCosACL: "public-read",
  433. },
  434. }
  435. _, err = s.Client.Object.PutACL(context.Background(), name, opt)
  436. assert.Nil(s.T(), err, "PutObjectACL Failed")
  437. v, _, err := s.Client.Object.GetACL(context.Background(), name)
  438. assert.Nil(s.T(), err, "GetObjectACL Failed")
  439. assert.Equal(s.T(), 2, len(v.AccessControlList), "GetLifecycle wrong number rules")
  440. _, err = s.Client.Object.Delete(context.Background(), name)
  441. assert.Nil(s.T(), err, "DeleteObject Failed")
  442. }
  443. func (s *CosTestSuite) TestPutObjectRestore() {
  444. name := "archivetest"
  445. putOpt := &cos.ObjectPutOptions{
  446. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  447. XCosStorageClass: "ARCHIVE",
  448. },
  449. }
  450. f := strings.NewReader("test")
  451. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  452. assert.Nil(s.T(), err, "PutObject Archive faild")
  453. opt := &cos.ObjectRestoreOptions{
  454. Days: 2,
  455. Tier: &cos.CASJobParameters{
  456. // Standard, Exepdited and Bulk
  457. Tier: "Expedited",
  458. },
  459. }
  460. resp, _ := s.Client.Object.PostRestore(context.Background(), name, opt)
  461. retCode := resp.StatusCode
  462. if retCode != 200 && retCode != 202 && retCode != 409 {
  463. right := false
  464. fmt.Println("PutObjectRestore get code is:", retCode)
  465. assert.Equal(s.T(), true, right, "PutObjectRestore Failed")
  466. }
  467. }
  468. func (s *CosTestSuite) TestCopyObject() {
  469. u := "http://" + kRepBucket + "-" + s.Appid + ".cos." + kRepRegion + ".myqcloud.com"
  470. iu, _ := url.Parse(u)
  471. ib := &cos.BaseURL{BucketURL: iu}
  472. c := cos.NewClient(ib, &http.Client{
  473. Transport: &cos.AuthorizationTransport{
  474. SecretID: os.Getenv("COS_SECRETID"),
  475. SecretKey: os.Getenv("COS_SECRETKEY"),
  476. },
  477. })
  478. opt := &cos.BucketPutOptions{
  479. XCosACL: "public-read",
  480. }
  481. // Notice in intranet the bucket host sometimes has i/o timeout problem
  482. r, err := c.Bucket.Put(context.Background(), opt)
  483. if err != nil && r.StatusCode == 409 {
  484. fmt.Println("BucketAlreadyOwnedByYou")
  485. } else if err != nil {
  486. assert.Nil(s.T(), err, "PutBucket Failed")
  487. }
  488. source := "test/objectMove1" + time.Now().Format(time.RFC3339)
  489. expected := "test"
  490. f := strings.NewReader(expected)
  491. r, err = c.Object.Put(context.Background(), source, f, nil)
  492. assert.Nil(s.T(), err, "PutObject Failed")
  493. var version_id string
  494. if r.Header["X-Cos-Version-Id"] != nil {
  495. version_id = r.Header.Get("X-Cos-Version-Id")
  496. }
  497. time.Sleep(3 * time.Second)
  498. // Copy file
  499. soruceURL := fmt.Sprintf("%s/%s", iu.Host, source)
  500. dest := "test/objectMove1" + time.Now().Format(time.RFC3339)
  501. //opt := &cos.ObjectCopyOptions{}
  502. if version_id == "" {
  503. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil)
  504. } else {
  505. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil, version_id)
  506. }
  507. assert.Nil(s.T(), err, "PutObjectCopy Failed")
  508. // Check content
  509. resp, err := s.Client.Object.Get(context.Background(), dest, nil)
  510. assert.Nil(s.T(), err, "GetObject Failed")
  511. bs, _ := ioutil.ReadAll(resp.Body)
  512. resp.Body.Close()
  513. result := string(bs)
  514. assert.Equal(s.T(), expected, result, "PutObjectCopy Failed, wrong content")
  515. }
  516. func (s *CosTestSuite) TestCreateAbortMultipartUpload() {
  517. name := "test_multipart" + time.Now().Format(time.RFC3339)
  518. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  519. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  520. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
  521. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  522. }
  523. func (s *CosTestSuite) TestCreateCompleteMultipartUpload() {
  524. name := "test/test_complete_upload" + time.Now().Format(time.RFC3339)
  525. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  526. uploadID := v.UploadID
  527. blockSize := 1024 * 1024 * 3
  528. opt := &cos.CompleteMultipartUploadOptions{}
  529. for i := 1; i < 3; i++ {
  530. b := make([]byte, blockSize)
  531. _, err := rand.Read(b)
  532. content := fmt.Sprintf("%X", b)
  533. f := strings.NewReader(content)
  534. resp, err := s.Client.Object.UploadPart(
  535. context.Background(), name, uploadID, i, f, nil,
  536. )
  537. assert.Nil(s.T(), err, "UploadPart Failed")
  538. etag := resp.Header.Get("Etag")
  539. opt.Parts = append(opt.Parts, cos.Object{
  540. PartNumber: i, ETag: etag},
  541. )
  542. }
  543. _, _, err = s.Client.Object.CompleteMultipartUpload(
  544. context.Background(), name, uploadID, opt,
  545. )
  546. assert.Nil(s.T(), err, "CompleteMultipartUpload Failed")
  547. }
  548. func (s *CosTestSuite) TestSSE_C() {
  549. name := "test/TestSSE_C"
  550. content := "test sse-c " + time.Now().Format(time.RFC3339)
  551. f := strings.NewReader(content)
  552. putOpt := &cos.ObjectPutOptions{
  553. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  554. ContentType: "text/html",
  555. //XCosServerSideEncryption: "AES256",
  556. XCosSSECustomerAglo: "AES256",
  557. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  558. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  559. },
  560. ACLHeaderOptions: &cos.ACLHeaderOptions{
  561. XCosACL: "public-read",
  562. //XCosACL: "private",
  563. },
  564. }
  565. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  566. assert.Nil(s.T(), err, "PutObject with SSE failed")
  567. headOpt := &cos.ObjectHeadOptions{
  568. XCosSSECustomerAglo: "AES256",
  569. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  570. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  571. }
  572. _, err = s.Client.Object.Head(context.Background(), name, headOpt)
  573. assert.Nil(s.T(), err, "HeadObject with SSE failed")
  574. getOpt := &cos.ObjectGetOptions{
  575. XCosSSECustomerAglo: "AES256",
  576. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  577. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  578. }
  579. var resp *cos.Response
  580. resp, err = s.Client.Object.Get(context.Background(), name, getOpt)
  581. assert.Nil(s.T(), err, "GetObject with SSE failed")
  582. bodyBytes, _ := ioutil.ReadAll(resp.Body)
  583. bodyContent := string(bodyBytes)
  584. assert.Equal(s.T(), content, bodyContent, "GetObject with SSE failed, want: %+v, res: %+v", content, bodyContent)
  585. copyOpt := &cos.ObjectCopyOptions{
  586. &cos.ObjectCopyHeaderOptions{
  587. XCosCopySourceSSECustomerAglo: "AES256",
  588. XCosCopySourceSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  589. XCosCopySourceSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  590. },
  591. &cos.ACLHeaderOptions{},
  592. }
  593. copySource := s.Bucket + "-" + s.Appid + ".cos." + s.Region + ".myqcloud.com/" + name
  594. _, _, err = s.Client.Object.Copy(context.Background(), "test/TestSSE_C_Copy", copySource, copyOpt)
  595. assert.Nil(s.T(), err, "CopyObject with SSE failed")
  596. partIni := &cos.MultiUploadOptions{
  597. OptIni: &cos.InitiateMultipartUploadOptions{
  598. &cos.ACLHeaderOptions{},
  599. &cos.ObjectPutHeaderOptions{
  600. XCosSSECustomerAglo: "AES256",
  601. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  602. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  603. },
  604. },
  605. PartSize: 1,
  606. }
  607. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  608. newFile, err := os.Create(filePath)
  609. assert.Nil(s.T(), err, "create tmp file Failed")
  610. defer newFile.Close()
  611. b := make([]byte, 1024*10)
  612. _, err = rand.Read(b)
  613. newFile.Write(b)
  614. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/TestSSE_C_MultiUpload", filePath, partIni)
  615. assert.Nil(s.T(), err, "MultiUpload with SSE failed")
  616. err = os.Remove(filePath)
  617. assert.Nil(s.T(), err, "remove local file Failed")
  618. }
  619. func (s *CosTestSuite) TestMultiUpload() {
  620. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  621. newFile, err := os.Create(filePath)
  622. assert.Nil(s.T(), err, "create tmp file Failed")
  623. defer newFile.Close()
  624. b := make([]byte, 1024*1024*10)
  625. _, err = rand.Read(b)
  626. newFile.Write(b)
  627. partIni := &cos.MultiUploadOptions{}
  628. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/Test_MultiUpload", filePath, partIni)
  629. err = os.Remove(filePath)
  630. assert.Nil(s.T(), err, "remove tmp file failed")
  631. }
  632. func (s *CosTestSuite) TestBatch() {
  633. client := cos.NewClient(s.Client.BaseURL, &http.Client{
  634. Transport: &cos.AuthorizationTransport{
  635. SecretID: os.Getenv("COS_SECRETID"),
  636. SecretKey: os.Getenv("COS_SECRETKEY"),
  637. },
  638. })
  639. source_name := "test/1.txt"
  640. sf := strings.NewReader("batch test content")
  641. _, err := client.Object.Put(context.Background(), source_name, sf, nil)
  642. assert.Nil(s.T(), err, "object put Failed")
  643. manifest_name := "test/manifest.csv"
  644. f := strings.NewReader(kBatchBucket + "," + source_name)
  645. resp, err := client.Object.Put(context.Background(), manifest_name, f, nil)
  646. assert.Nil(s.T(), err, "object put Failed")
  647. etag := resp.Header.Get("ETag")
  648. uuid_str := uuid.New().String()
  649. opt := &cos.BatchCreateJobOptions{
  650. ClientRequestToken: uuid_str,
  651. ConfirmationRequired: "true",
  652. Description: "test batch",
  653. Manifest: &cos.BatchJobManifest{
  654. Location: &cos.BatchJobManifestLocation{
  655. ETag: etag,
  656. ObjectArn: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kBatchBucket + "/" + manifest_name,
  657. },
  658. Spec: &cos.BatchJobManifestSpec{
  659. Fields: []string{"Bucket", "Key"},
  660. Format: "COSBatchOperations_CSV_V1",
  661. },
  662. },
  663. Operation: &cos.BatchJobOperation{
  664. PutObjectCopy: &cos.BatchJobOperationCopy{
  665. TargetResource: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kTargetBatchBucket,
  666. },
  667. },
  668. Priority: 1,
  669. Report: &cos.BatchJobReport{
  670. Bucket: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kBatchBucket,
  671. Enabled: "true",
  672. Format: "Report_CSV_V1",
  673. Prefix: "job-result",
  674. ReportScope: "AllTasks",
  675. },
  676. RoleArn: "qcs::cam::uin/" + kUin + ":roleName/COSBatch_QcsRole",
  677. }
  678. headers := &cos.BatchRequestHeaders{
  679. XCosAppid: kAppid,
  680. }
  681. res1, _, err := client.Batch.CreateJob(context.Background(), opt, headers)
  682. assert.Nil(s.T(), err, "create job Failed")
  683. jobid := res1.JobId
  684. res2, _, err := client.Batch.DescribeJob(context.Background(), jobid, headers)
  685. assert.Nil(s.T(), err, "describe job Failed")
  686. assert.Equal(s.T(), res2.Job.ConfirmationRequired, "true", "ConfirmationRequired not right")
  687. assert.Equal(s.T(), res2.Job.Description, "test batch", "Description not right")
  688. assert.Equal(s.T(), res2.Job.JobId, jobid, "jobid not right")
  689. assert.Equal(s.T(), res2.Job.Priority, 1, "priority not right")
  690. assert.Equal(s.T(), res2.Job.RoleArn, "qcs::cam::uin/"+kUin+":roleName/COSBatch_QcsRole", "priority not right")
  691. _, _, err = client.Batch.ListJobs(context.Background(), nil, headers)
  692. assert.Nil(s.T(), err, "list jobs failed")
  693. up_opt := &cos.BatchUpdatePriorityOptions{
  694. JobId: jobid,
  695. Priority: 3,
  696. }
  697. res3, _, err := client.Batch.UpdateJobPriority(context.Background(), up_opt, headers)
  698. assert.Nil(s.T(), err, "list jobs failed")
  699. assert.Equal(s.T(), res3.JobId, jobid, "jobid failed")
  700. assert.Equal(s.T(), res3.Priority, 3, "priority not right")
  701. for i := 0; i < 10; i = i + 1 {
  702. res, _, err := client.Batch.DescribeJob(context.Background(), jobid, headers)
  703. assert.Nil(s.T(), err, "describe job Failed")
  704. assert.Equal(s.T(), res2.Job.ConfirmationRequired, "true", "ConfirmationRequired not right")
  705. assert.Equal(s.T(), res2.Job.Description, "test batch", "Description not right")
  706. assert.Equal(s.T(), res2.Job.JobId, jobid, "jobid not right")
  707. assert.Equal(s.T(), res2.Job.Priority, 1, "priority not right")
  708. assert.Equal(s.T(), res2.Job.RoleArn, "qcs::cam::uin/"+kUin+":roleName/COSBatch_QcsRole", "priority not right")
  709. if res.Job.Status == "Suspended" {
  710. break
  711. }
  712. if i == 9 {
  713. assert.Error(s.T(), errors.New("Job status is not Suspended or timeout"))
  714. }
  715. time.Sleep(time.Second * 2)
  716. }
  717. us_opt := &cos.BatchUpdateStatusOptions{
  718. JobId: jobid,
  719. RequestedJobStatus: "Ready", // 允许状态转换见 https://cloud.tencent.com/document/product/436/38604
  720. StatusUpdateReason: "to test",
  721. }
  722. res4, _, err := client.Batch.UpdateJobStatus(context.Background(), us_opt, headers)
  723. assert.Nil(s.T(), err, "list jobs failed")
  724. assert.Equal(s.T(), res4.JobId, jobid, "jobid failed")
  725. assert.Equal(s.T(), res4.Status, "Ready", "status failed")
  726. assert.Equal(s.T(), res4.StatusUpdateReason, "to test", "StatusUpdateReason failed")
  727. }
  728. // End of api test
  729. // All methods that begin with "Test" are run as tests within a
  730. // suite.
  731. // In order for 'go test' to run this suite, we need to create
  732. // a normal test function and pass our suite to suite.Run
  733. func TestCosTestSuite(t *testing.T) {
  734. suite.Run(t, new(CosTestSuite))
  735. }
  736. func (s *CosTestSuite) TearDownSuite() {
  737. // Clean the file in bucket
  738. // r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  739. // assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  740. // for _, p := range r.Uploads {
  741. // // Abort
  742. // _, err = s.Client.Object.AbortMultipartUpload(context.Background(), p.Key, p.UploadID)
  743. // assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  744. // }
  745. // // Delete objects
  746. // opt := &cos.BucketGetOptions{
  747. // MaxKeys: 500,
  748. // }
  749. // v, _, err := s.Client.Bucket.Get(context.Background(), opt)
  750. // assert.Nil(s.T(), err, "GetBucket Failed")
  751. // for _, c := range v.Contents {
  752. // _, err := s.Client.Object.Delete(context.Background(), c.Key)
  753. // assert.Nil(s.T(), err, "DeleteObject Failed")
  754. // }
  755. // When clean up these infos, can not solve the concurrent test problem
  756. fmt.Println("tear down~")
  757. }