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.

991 lines
32 KiB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 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
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 = "cosgosdktest-1259654469"
  51. kTargetBatchBucket = "cosgosdktest-1259654469" //复用了存储桶
  52. kBatchRegion = "ap-guangzhou"
  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. pi := strings.LastIndex(p[0], "-")
  71. s.Bucket = p[0][:pi]
  72. s.Appid = p[0][pi+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) TestGetObjectVersions() {
  156. opt := &cos.BucketGetObjectVersionsOptions{
  157. Prefix: "中文",
  158. MaxKeys: 3,
  159. }
  160. _, _, err := s.Client.Bucket.GetObjectVersions(context.Background(), opt)
  161. assert.Nil(s.T(), err, "GetObjectVersions Failed")
  162. }
  163. func (s *CosTestSuite) TestGetBucketLocation() {
  164. v, _, err := s.Client.Bucket.GetLocation(context.Background())
  165. assert.Nil(s.T(), err, "GetLocation Failed")
  166. assert.Equal(s.T(), s.Region, v.Location, "GetLocation wrong region")
  167. }
  168. func (s *CosTestSuite) TestPutGetDeleteCORS() {
  169. opt := &cos.BucketPutCORSOptions{
  170. Rules: []cos.BucketCORSRule{
  171. {
  172. AllowedOrigins: []string{"http://www.qq.com"},
  173. AllowedMethods: []string{"PUT", "GET"},
  174. AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
  175. MaxAgeSeconds: 500,
  176. ExposeHeaders: []string{"x-cos-meta-test1"},
  177. },
  178. },
  179. }
  180. _, err := s.Client.Bucket.PutCORS(context.Background(), opt)
  181. assert.Nil(s.T(), err, "PutBucketCORS Failed")
  182. v, _, err := s.Client.Bucket.GetCORS(context.Background())
  183. assert.Nil(s.T(), err, "GetBucketCORS Failed")
  184. assert.Equal(s.T(), 1, len(v.Rules), "GetBucketCORS wrong number rules")
  185. }
  186. func (s *CosTestSuite) TestVersionAndReplication() {
  187. opt := &cos.BucketPutVersionOptions{
  188. // Enabled or Suspended, the versioning once opened can not close.
  189. Status: "Enabled",
  190. }
  191. _, err := s.Client.Bucket.PutVersioning(context.Background(), opt)
  192. assert.Nil(s.T(), err, "PutVersioning Failed")
  193. time.Sleep(time.Second)
  194. v, _, err := s.Client.Bucket.GetVersioning(context.Background())
  195. assert.Nil(s.T(), err, "GetVersioning Failed")
  196. assert.Equal(s.T(), "Enabled", v.Status, "Get Wrong Version status")
  197. repOpt := &cos.PutBucketReplicationOptions{
  198. // qcs::cam::uin/[UIN]:uin/[Subaccount]
  199. Role: "qcs::cam::uin/" + kUin + ":uin/" + kUin,
  200. Rule: []cos.BucketReplicationRule{
  201. {
  202. ID: "1",
  203. // Enabled or Disabled
  204. Status: "Enabled",
  205. Destination: &cos.ReplicationDestination{
  206. // qcs::cos:[Region]::[Bucketname-Appid]
  207. Bucket: "qcs::cos:" + kRepRegion + "::" + kRepBucket + "-" + s.Appid,
  208. },
  209. },
  210. },
  211. }
  212. _, err = s.Client.Bucket.PutBucketReplication(context.Background(), repOpt)
  213. assert.Nil(s.T(), err, "PutBucketReplication Failed")
  214. time.Sleep(time.Second)
  215. vr, _, err := s.Client.Bucket.GetBucketReplication(context.Background())
  216. assert.Nil(s.T(), err, "GetBucketReplication Failed")
  217. for _, r := range vr.Rule {
  218. assert.Equal(s.T(), "Enabled", r.Status, "Get Wrong Version status")
  219. assert.Equal(s.T(), "qcs::cos:"+kRepRegion+"::"+kRepBucket+"-"+s.Appid, r.Destination.Bucket, "Get Wrong Version status")
  220. }
  221. _, err = s.Client.Bucket.DeleteBucketReplication(context.Background())
  222. assert.Nil(s.T(), err, "DeleteBucketReplication Failed")
  223. }
  224. func (s *CosTestSuite) TestBucketInventory() {
  225. id := "test1"
  226. dBucket := "qcs::cos:" + s.Region + "::" + s.Bucket + "-" + s.Appid
  227. opt := &cos.BucketPutInventoryOptions{
  228. ID: id,
  229. // True or False
  230. IsEnabled: "True",
  231. IncludedObjectVersions: "All",
  232. Filter: &cos.BucketInventoryFilter{
  233. Prefix: "test",
  234. },
  235. OptionalFields: &cos.BucketInventoryOptionalFields{
  236. BucketInventoryFields: []string{
  237. "Size", "LastModifiedDate",
  238. },
  239. },
  240. Schedule: &cos.BucketInventorySchedule{
  241. // Weekly or Daily
  242. Frequency: "Daily",
  243. },
  244. Destination: &cos.BucketInventoryDestination{
  245. Bucket: dBucket,
  246. Format: "CSV",
  247. },
  248. }
  249. _, err := s.Client.Bucket.PutInventory(context.Background(), id, opt)
  250. assert.Nil(s.T(), err, "PutBucketInventory Failed")
  251. v, _, err := s.Client.Bucket.GetInventory(context.Background(), id)
  252. assert.Nil(s.T(), err, "GetBucketInventory Failed")
  253. assert.Equal(s.T(), "test1", v.ID, "Get Wrong inventory id")
  254. assert.Equal(s.T(), "true", v.IsEnabled, "Get Wrong inventory isenabled")
  255. assert.Equal(s.T(), dBucket, v.Destination.Bucket, "Get Wrong inventory isenabled")
  256. _, err = s.Client.Bucket.DeleteInventory(context.Background(), id)
  257. assert.Nil(s.T(), err, "DeleteBucketInventory Failed")
  258. }
  259. func (s *CosTestSuite) TestBucketLogging() {
  260. tBucket := s.Bucket + "-" + s.Appid
  261. opt := &cos.BucketPutLoggingOptions{
  262. LoggingEnabled: &cos.BucketLoggingEnabled{
  263. TargetBucket: tBucket,
  264. },
  265. }
  266. _, err := s.Client.Bucket.PutLogging(context.Background(), opt)
  267. assert.Nil(s.T(), err, "PutLogging Failed")
  268. v, _, err := s.Client.Bucket.GetLogging(context.Background())
  269. assert.Nil(s.T(), err, "GetLogging Failed")
  270. assert.Equal(s.T(), tBucket, v.LoggingEnabled.TargetBucket, "Get Wrong Version status")
  271. }
  272. func (s *CosTestSuite) TestBucketTagging() {
  273. opt := &cos.BucketPutTaggingOptions{
  274. TagSet: []cos.BucketTaggingTag{
  275. {
  276. Key: "testk1",
  277. Value: "testv1",
  278. },
  279. {
  280. Key: "testk2",
  281. Value: "testv2",
  282. },
  283. },
  284. }
  285. _, err := s.Client.Bucket.PutTagging(context.Background(), opt)
  286. assert.Nil(s.T(), err, "Put Tagging Failed")
  287. v, _, err := s.Client.Bucket.GetTagging(context.Background())
  288. assert.Nil(s.T(), err, "Get Tagging Failed")
  289. assert.Equal(s.T(), v.TagSet[0].Key, opt.TagSet[0].Key, "Get Wrong Tag key")
  290. assert.Equal(s.T(), v.TagSet[0].Value, opt.TagSet[0].Value, "Get Wrong Tag value")
  291. assert.Equal(s.T(), v.TagSet[1].Key, opt.TagSet[1].Key, "Get Wrong Tag key")
  292. assert.Equal(s.T(), v.TagSet[1].Value, opt.TagSet[1].Value, "Get Wrong Tag value")
  293. }
  294. func (s *CosTestSuite) TestPutGetDeleteLifeCycle() {
  295. lc := &cos.BucketPutLifecycleOptions{
  296. Rules: []cos.BucketLifecycleRule{
  297. {
  298. ID: "1234",
  299. Filter: &cos.BucketLifecycleFilter{Prefix: "test"},
  300. Status: "Enabled",
  301. Transition: &cos.BucketLifecycleTransition{
  302. Days: 10,
  303. StorageClass: "Standard",
  304. },
  305. },
  306. },
  307. }
  308. _, err := s.Client.Bucket.PutLifecycle(context.Background(), lc)
  309. assert.Nil(s.T(), err, "PutBucketLifecycle Failed")
  310. _, r, err := s.Client.Bucket.GetLifecycle(context.Background())
  311. // Might cleaned by other case concrrent
  312. if err != nil && 404 != r.StatusCode {
  313. assert.Nil(s.T(), err, "GetBucketLifecycle Failed")
  314. }
  315. _, err = s.Client.Bucket.DeleteLifecycle(context.Background())
  316. assert.Nil(s.T(), err, "DeleteBucketLifecycle Failed")
  317. }
  318. func (s *CosTestSuite) TestPutGetDeleteWebsite() {
  319. opt := &cos.BucketPutWebsiteOptions{
  320. Index: "index.html",
  321. Error: &cos.ErrorDocument{"index_backup.html"},
  322. RoutingRules: &cos.WebsiteRoutingRules{
  323. []cos.WebsiteRoutingRule{
  324. {
  325. ConditionErrorCode: "404",
  326. RedirectProtocol: "https",
  327. RedirectReplaceKey: "404.html",
  328. },
  329. {
  330. ConditionPrefix: "docs/",
  331. RedirectProtocol: "https",
  332. RedirectReplaceKeyPrefix: "documents/",
  333. },
  334. },
  335. },
  336. }
  337. _, err := s.Client.Bucket.PutWebsite(context.Background(), opt)
  338. assert.Nil(s.T(), err, "PutBucketWebsite Failed")
  339. res, rsp, err := s.Client.Bucket.GetWebsite(context.Background())
  340. if err != nil && 404 != rsp.StatusCode {
  341. assert.Nil(s.T(), err, "GetBucketWebsite Failed")
  342. }
  343. assert.Equal(s.T(), opt.Index, res.Index, "GetBucketWebsite Failed")
  344. assert.Equal(s.T(), opt.Error, res.Error, "GetBucketWebsite Failed")
  345. assert.Equal(s.T(), opt.RedirectProtocol, res.RedirectProtocol, "GetBucketWebsite Failed")
  346. _, err = s.Client.Bucket.DeleteWebsite(context.Background())
  347. assert.Nil(s.T(), err, "DeleteBucketWebsite Failed")
  348. }
  349. func (s *CosTestSuite) TestListMultipartUploads() {
  350. // Create new upload
  351. name := "test_multipart" + time.Now().Format(time.RFC3339)
  352. flag := false
  353. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  354. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  355. id := v.UploadID
  356. // List
  357. r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  358. assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  359. for _, p := range r.Uploads {
  360. if p.Key == name {
  361. assert.Equal(s.T(), id, p.UploadID, "ListMultipartUploads wrong uploadid")
  362. flag = true
  363. }
  364. }
  365. assert.Equal(s.T(), true, flag, "ListMultipartUploads wrong key")
  366. // Abort
  367. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, id)
  368. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  369. }
  370. // Object API
  371. func (s *CosTestSuite) TestPutHeadGetDeleteObject_10MB() {
  372. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  373. b := make([]byte, 1024*1024*10)
  374. _, err := rand.Read(b)
  375. content := fmt.Sprintf("%X", b)
  376. f := strings.NewReader(content)
  377. _, err = s.Client.Object.Put(context.Background(), name, f, nil)
  378. assert.Nil(s.T(), err, "PutObject Failed")
  379. _, err = s.Client.Object.Head(context.Background(), name, nil)
  380. assert.Nil(s.T(), err, "HeadObject Failed")
  381. _, err = s.Client.Object.Delete(context.Background(), name)
  382. assert.Nil(s.T(), err, "DeleteObject Failed")
  383. }
  384. func (s *CosTestSuite) TestPutGetDeleteObjectByFile_10MB() {
  385. // Create tmp file
  386. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  387. newfile, err := os.Create(filePath)
  388. assert.Nil(s.T(), err, "create tmp file Failed")
  389. defer newfile.Close()
  390. name := "test/objectPutByFile" + time.Now().Format(time.RFC3339)
  391. b := make([]byte, 1024*1024*10)
  392. _, err = rand.Read(b)
  393. newfile.Write(b)
  394. _, err = s.Client.Object.PutFromFile(context.Background(), name, filePath, nil)
  395. assert.Nil(s.T(), err, "PutObject Failed")
  396. // Over write tmp file
  397. _, err = s.Client.Object.GetToFile(context.Background(), name, filePath, nil)
  398. assert.Nil(s.T(), err, "HeadObject Failed")
  399. _, err = s.Client.Object.Delete(context.Background(), name)
  400. assert.Nil(s.T(), err, "DeleteObject Failed")
  401. // remove the local tmp file
  402. err = os.Remove(filePath)
  403. assert.Nil(s.T(), err, "remove local file Failed")
  404. }
  405. func (s *CosTestSuite) TestPutGetDeleteObjectSpecialName() {
  406. f := strings.NewReader("test")
  407. name := s.SepFileName + time.Now().Format(time.RFC3339)
  408. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  409. assert.Nil(s.T(), err, "PutObject Failed")
  410. resp, err := s.Client.Object.Get(context.Background(), name, nil)
  411. assert.Nil(s.T(), err, "GetObject Failed")
  412. defer resp.Body.Close()
  413. bs, _ := ioutil.ReadAll(resp.Body)
  414. assert.Equal(s.T(), "test", string(bs), "GetObject failed content wrong")
  415. _, err = s.Client.Object.Delete(context.Background(), name)
  416. assert.Nil(s.T(), err, "DeleteObject Failed")
  417. }
  418. func (s *CosTestSuite) TestPutObjectToNonExistBucket() {
  419. u := "http://gosdknonexistbucket-" + s.Appid + ".cos." + s.Region + ".myqcloud.com"
  420. iu, _ := url.Parse(u)
  421. ib := &cos.BaseURL{BucketURL: iu}
  422. client := cos.NewClient(ib, &http.Client{
  423. Transport: &cos.AuthorizationTransport{
  424. SecretID: os.Getenv("COS_SECRETID"),
  425. SecretKey: os.Getenv("COS_SECRETKEY"),
  426. },
  427. })
  428. name := "test/objectPut.go"
  429. f := strings.NewReader("test")
  430. r, err := client.Object.Put(context.Background(), name, f, nil)
  431. assert.NotNil(s.T(), err, "PutObject ToNonExistBucket Failed")
  432. assert.Equal(s.T(), 404, r.StatusCode, "PutObject ToNonExistBucket, not 404")
  433. }
  434. func (s *CosTestSuite) TestPutGetObjectACL() {
  435. name := "test/objectACL.go" + time.Now().Format(time.RFC3339)
  436. f := strings.NewReader("test")
  437. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  438. assert.Nil(s.T(), err, "PutObject Failed")
  439. // Put acl
  440. opt := &cos.ObjectPutACLOptions{
  441. Header: &cos.ACLHeaderOptions{
  442. XCosACL: "public-read",
  443. },
  444. }
  445. _, err = s.Client.Object.PutACL(context.Background(), name, opt)
  446. assert.Nil(s.T(), err, "PutObjectACL Failed")
  447. v, _, err := s.Client.Object.GetACL(context.Background(), name)
  448. assert.Nil(s.T(), err, "GetObjectACL Failed")
  449. assert.Equal(s.T(), 2, len(v.AccessControlList), "GetLifecycle wrong number rules")
  450. _, err = s.Client.Object.Delete(context.Background(), name)
  451. assert.Nil(s.T(), err, "DeleteObject Failed")
  452. }
  453. func (s *CosTestSuite) TestPutObjectRestore() {
  454. name := "archivetest"
  455. putOpt := &cos.ObjectPutOptions{
  456. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  457. XCosStorageClass: "ARCHIVE",
  458. },
  459. }
  460. f := strings.NewReader("test")
  461. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  462. assert.Nil(s.T(), err, "PutObject Archive faild")
  463. opt := &cos.ObjectRestoreOptions{
  464. Days: 2,
  465. Tier: &cos.CASJobParameters{
  466. // Standard, Exepdited and Bulk
  467. Tier: "Expedited",
  468. },
  469. }
  470. resp, _ := s.Client.Object.PostRestore(context.Background(), name, opt)
  471. retCode := resp.StatusCode
  472. if retCode != 200 && retCode != 202 && retCode != 409 {
  473. right := false
  474. fmt.Println("PutObjectRestore get code is:", retCode)
  475. assert.Equal(s.T(), true, right, "PutObjectRestore Failed")
  476. }
  477. }
  478. func (s *CosTestSuite) TestCopyObject() {
  479. u := "http://" + kRepBucket + "-" + s.Appid + ".cos." + kRepRegion + ".myqcloud.com"
  480. iu, _ := url.Parse(u)
  481. ib := &cos.BaseURL{BucketURL: iu}
  482. c := cos.NewClient(ib, &http.Client{
  483. Transport: &cos.AuthorizationTransport{
  484. SecretID: os.Getenv("COS_SECRETID"),
  485. SecretKey: os.Getenv("COS_SECRETKEY"),
  486. },
  487. })
  488. opt := &cos.BucketPutOptions{
  489. XCosACL: "public-read",
  490. }
  491. // Notice in intranet the bucket host sometimes has i/o timeout problem
  492. r, err := c.Bucket.Put(context.Background(), opt)
  493. if err != nil && r.StatusCode == 409 {
  494. fmt.Println("BucketAlreadyOwnedByYou")
  495. } else if err != nil {
  496. assert.Nil(s.T(), err, "PutBucket Failed")
  497. }
  498. source := "test/objectMove1" + time.Now().Format(time.RFC3339)
  499. expected := "test"
  500. f := strings.NewReader(expected)
  501. r, err = c.Object.Put(context.Background(), source, f, nil)
  502. assert.Nil(s.T(), err, "PutObject Failed")
  503. var version_id string
  504. if r.Header["X-Cos-Version-Id"] != nil {
  505. version_id = r.Header.Get("X-Cos-Version-Id")
  506. }
  507. time.Sleep(3 * time.Second)
  508. // Copy file
  509. soruceURL := fmt.Sprintf("%s/%s", iu.Host, source)
  510. dest := "test/objectMove1" + time.Now().Format(time.RFC3339)
  511. //opt := &cos.ObjectCopyOptions{}
  512. if version_id == "" {
  513. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil)
  514. } else {
  515. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil, version_id)
  516. }
  517. assert.Nil(s.T(), err, "PutObjectCopy Failed")
  518. // Check content
  519. resp, err := s.Client.Object.Get(context.Background(), dest, nil)
  520. assert.Nil(s.T(), err, "GetObject Failed")
  521. bs, _ := ioutil.ReadAll(resp.Body)
  522. resp.Body.Close()
  523. result := string(bs)
  524. assert.Equal(s.T(), expected, result, "PutObjectCopy Failed, wrong content")
  525. }
  526. func (s *CosTestSuite) TestCreateAbortMultipartUpload() {
  527. name := "test_multipart" + time.Now().Format(time.RFC3339)
  528. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  529. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  530. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
  531. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  532. }
  533. func (s *CosTestSuite) TestCreateCompleteMultipartUpload() {
  534. name := "test/test_complete_upload" + time.Now().Format(time.RFC3339)
  535. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  536. uploadID := v.UploadID
  537. blockSize := 1024 * 1024 * 3
  538. opt := &cos.CompleteMultipartUploadOptions{}
  539. for i := 1; i < 3; i++ {
  540. b := make([]byte, blockSize)
  541. _, err := rand.Read(b)
  542. content := fmt.Sprintf("%X", b)
  543. f := strings.NewReader(content)
  544. resp, err := s.Client.Object.UploadPart(
  545. context.Background(), name, uploadID, i, f, nil,
  546. )
  547. assert.Nil(s.T(), err, "UploadPart Failed")
  548. etag := resp.Header.Get("Etag")
  549. opt.Parts = append(opt.Parts, cos.Object{
  550. PartNumber: i, ETag: etag},
  551. )
  552. }
  553. _, _, err = s.Client.Object.CompleteMultipartUpload(
  554. context.Background(), name, uploadID, opt,
  555. )
  556. assert.Nil(s.T(), err, "CompleteMultipartUpload Failed")
  557. }
  558. func (s *CosTestSuite) TestSSE_C() {
  559. name := "test/TestSSE_C"
  560. content := "test sse-c " + time.Now().Format(time.RFC3339)
  561. f := strings.NewReader(content)
  562. putOpt := &cos.ObjectPutOptions{
  563. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  564. ContentType: "text/html",
  565. //XCosServerSideEncryption: "AES256",
  566. XCosSSECustomerAglo: "AES256",
  567. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  568. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  569. },
  570. ACLHeaderOptions: &cos.ACLHeaderOptions{
  571. XCosACL: "public-read",
  572. //XCosACL: "private",
  573. },
  574. }
  575. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  576. assert.Nil(s.T(), err, "PutObject with SSE failed")
  577. headOpt := &cos.ObjectHeadOptions{
  578. XCosSSECustomerAglo: "AES256",
  579. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  580. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  581. }
  582. _, err = s.Client.Object.Head(context.Background(), name, headOpt)
  583. assert.Nil(s.T(), err, "HeadObject with SSE failed")
  584. getOpt := &cos.ObjectGetOptions{
  585. XCosSSECustomerAglo: "AES256",
  586. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  587. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  588. }
  589. var resp *cos.Response
  590. resp, err = s.Client.Object.Get(context.Background(), name, getOpt)
  591. assert.Nil(s.T(), err, "GetObject with SSE failed")
  592. bodyBytes, _ := ioutil.ReadAll(resp.Body)
  593. bodyContent := string(bodyBytes)
  594. assert.Equal(s.T(), content, bodyContent, "GetObject with SSE failed, want: %+v, res: %+v", content, bodyContent)
  595. copyOpt := &cos.ObjectCopyOptions{
  596. &cos.ObjectCopyHeaderOptions{
  597. XCosCopySourceSSECustomerAglo: "AES256",
  598. XCosCopySourceSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  599. XCosCopySourceSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  600. },
  601. &cos.ACLHeaderOptions{},
  602. }
  603. copySource := s.Bucket + "-" + s.Appid + ".cos." + s.Region + ".myqcloud.com/" + name
  604. _, _, err = s.Client.Object.Copy(context.Background(), "test/TestSSE_C_Copy", copySource, copyOpt)
  605. assert.Nil(s.T(), err, "CopyObject with SSE failed")
  606. partIni := &cos.MultiUploadOptions{
  607. OptIni: &cos.InitiateMultipartUploadOptions{
  608. &cos.ACLHeaderOptions{},
  609. &cos.ObjectPutHeaderOptions{
  610. XCosSSECustomerAglo: "AES256",
  611. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  612. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  613. },
  614. },
  615. PartSize: 1,
  616. }
  617. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  618. newFile, err := os.Create(filePath)
  619. assert.Nil(s.T(), err, "create tmp file Failed")
  620. defer newFile.Close()
  621. b := make([]byte, 1024*10)
  622. _, err = rand.Read(b)
  623. newFile.Write(b)
  624. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/TestSSE_C_MultiUpload", filePath, partIni)
  625. assert.Nil(s.T(), err, "MultiUpload with SSE failed")
  626. err = os.Remove(filePath)
  627. assert.Nil(s.T(), err, "remove local file Failed")
  628. }
  629. func (s *CosTestSuite) TestMultiUpload() {
  630. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  631. newFile, err := os.Create(filePath)
  632. assert.Nil(s.T(), err, "create tmp file Failed")
  633. defer newFile.Close()
  634. b := make([]byte, 1024*1024*10)
  635. _, err = rand.Read(b)
  636. newFile.Write(b)
  637. partIni := &cos.MultiUploadOptions{}
  638. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/Test_MultiUpload", filePath, partIni)
  639. err = os.Remove(filePath)
  640. assert.Nil(s.T(), err, "remove tmp file failed")
  641. }
  642. func (s *CosTestSuite) TestBatch() {
  643. client := cos.NewClient(s.Client.BaseURL, &http.Client{
  644. Transport: &cos.AuthorizationTransport{
  645. SecretID: os.Getenv("COS_SECRETID"),
  646. SecretKey: os.Getenv("COS_SECRETKEY"),
  647. },
  648. })
  649. source_name := "test/1.txt"
  650. sf := strings.NewReader("batch test content")
  651. _, err := client.Object.Put(context.Background(), source_name, sf, nil)
  652. assert.Nil(s.T(), err, "object put Failed")
  653. manifest_name := "test/manifest.csv"
  654. f := strings.NewReader(kBatchBucket + "," + source_name)
  655. resp, err := client.Object.Put(context.Background(), manifest_name, f, nil)
  656. assert.Nil(s.T(), err, "object put Failed")
  657. etag := resp.Header.Get("ETag")
  658. uuid_str := uuid.New().String()
  659. opt := &cos.BatchCreateJobOptions{
  660. ClientRequestToken: uuid_str,
  661. ConfirmationRequired: "true",
  662. Description: "test batch",
  663. Manifest: &cos.BatchJobManifest{
  664. Location: &cos.BatchJobManifestLocation{
  665. ETag: etag,
  666. ObjectArn: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kBatchBucket + "/" + manifest_name,
  667. },
  668. Spec: &cos.BatchJobManifestSpec{
  669. Fields: []string{"Bucket", "Key"},
  670. Format: "COSBatchOperations_CSV_V1",
  671. },
  672. },
  673. Operation: &cos.BatchJobOperation{
  674. PutObjectCopy: &cos.BatchJobOperationCopy{
  675. TargetResource: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kTargetBatchBucket,
  676. },
  677. },
  678. Priority: 1,
  679. Report: &cos.BatchJobReport{
  680. Bucket: "qcs::cos:" + kBatchRegion + ":uid/" + s.Appid + ":" + kBatchBucket,
  681. Enabled: "true",
  682. Format: "Report_CSV_V1",
  683. Prefix: "job-result",
  684. ReportScope: "AllTasks",
  685. },
  686. RoleArn: "qcs::cam::uin/" + kUin + ":roleName/COSBatch_QcsRole",
  687. }
  688. headers := &cos.BatchRequestHeaders{
  689. XCosAppid: kAppid,
  690. }
  691. res1, _, err := client.Batch.CreateJob(context.Background(), opt, headers)
  692. assert.Nil(s.T(), err, "create job Failed")
  693. jobid := res1.JobId
  694. res2, _, err := client.Batch.DescribeJob(context.Background(), jobid, headers)
  695. assert.Nil(s.T(), err, "describe job Failed")
  696. assert.Equal(s.T(), res2.Job.ConfirmationRequired, "true", "ConfirmationRequired not right")
  697. assert.Equal(s.T(), res2.Job.Description, "test batch", "Description not right")
  698. assert.Equal(s.T(), res2.Job.JobId, jobid, "jobid not right")
  699. assert.Equal(s.T(), res2.Job.Priority, 1, "priority not right")
  700. assert.Equal(s.T(), res2.Job.RoleArn, "qcs::cam::uin/"+kUin+":roleName/COSBatch_QcsRole", "priority not right")
  701. _, _, err = client.Batch.ListJobs(context.Background(), nil, headers)
  702. assert.Nil(s.T(), err, "list jobs failed")
  703. up_opt := &cos.BatchUpdatePriorityOptions{
  704. JobId: jobid,
  705. Priority: 3,
  706. }
  707. res3, _, err := client.Batch.UpdateJobPriority(context.Background(), up_opt, headers)
  708. assert.Nil(s.T(), err, "list jobs failed")
  709. assert.Equal(s.T(), res3.JobId, jobid, "jobid failed")
  710. assert.Equal(s.T(), res3.Priority, 3, "priority not right")
  711. // 等待状态变成Suspended
  712. for i := 0; i < 50; i = i + 1 {
  713. res, _, err := client.Batch.DescribeJob(context.Background(), jobid, headers)
  714. assert.Nil(s.T(), err, "describe job Failed")
  715. assert.Equal(s.T(), res2.Job.ConfirmationRequired, "true", "ConfirmationRequired not right")
  716. assert.Equal(s.T(), res2.Job.Description, "test batch", "Description not right")
  717. assert.Equal(s.T(), res2.Job.JobId, jobid, "jobid not right")
  718. assert.Equal(s.T(), res2.Job.Priority, 1, "priority not right")
  719. assert.Equal(s.T(), res2.Job.RoleArn, "qcs::cam::uin/"+kUin+":roleName/COSBatch_QcsRole", "priority not right")
  720. if res.Job.Status == "Suspended" {
  721. break
  722. }
  723. if i == 9 {
  724. assert.Error(s.T(), errors.New("Job status is not Suspended or timeout"))
  725. }
  726. time.Sleep(time.Second * 2)
  727. }
  728. us_opt := &cos.BatchUpdateStatusOptions{
  729. JobId: jobid,
  730. RequestedJobStatus: "Ready", // 允许状态转换见 https://cloud.tencent.com/document/product/436/38604
  731. StatusUpdateReason: "to test",
  732. }
  733. res4, _, err := client.Batch.UpdateJobStatus(context.Background(), us_opt, headers)
  734. assert.Nil(s.T(), err, "list jobs failed")
  735. assert.Equal(s.T(), res4.JobId, jobid, "jobid failed")
  736. assert.Equal(s.T(), res4.Status, "Ready", "status failed")
  737. assert.Equal(s.T(), res4.StatusUpdateReason, "to test", "StatusUpdateReason failed")
  738. }
  739. func (s *CosTestSuite) TestEncryption() {
  740. opt := &cos.BucketPutEncryptionOptions{
  741. Rule: &cos.BucketEncryptionConfiguration{
  742. SSEAlgorithm: "AES256",
  743. },
  744. }
  745. _, err := s.Client.Bucket.PutEncryption(context.Background(), opt)
  746. assert.Nil(s.T(), err, "PutEncryption Failed")
  747. res, _, err := s.Client.Bucket.GetEncryption(context.Background())
  748. assert.Nil(s.T(), err, "GetEncryption Failed")
  749. assert.Equal(s.T(), opt.Rule.SSEAlgorithm, res.Rule.SSEAlgorithm, "GetEncryption Failed")
  750. _, err = s.Client.Bucket.DeleteEncryption(context.Background())
  751. assert.Nil(s.T(), err, "DeleteEncryption Failed")
  752. }
  753. func (s *CosTestSuite) TestReferer() {
  754. opt := &cos.BucketPutRefererOptions{
  755. Status: "Enabled",
  756. RefererType: "White-List",
  757. DomainList: []string{
  758. "*.qq.com",
  759. "*.qcloud.com",
  760. },
  761. EmptyReferConfiguration: "Allow",
  762. }
  763. _, err := s.Client.Bucket.PutReferer(context.Background(), opt)
  764. assert.Nil(s.T(), err, "PutReferer Failed")
  765. res, _, err := s.Client.Bucket.GetReferer(context.Background())
  766. assert.Nil(s.T(), err, "GetReferer Failed")
  767. assert.Equal(s.T(), opt.Status, res.Status, "GetReferer Failed")
  768. assert.Equal(s.T(), opt.RefererType, res.RefererType, "GetReferer Failed")
  769. assert.Equal(s.T(), opt.DomainList, res.DomainList, "GetReferer Failed")
  770. assert.Equal(s.T(), opt.EmptyReferConfiguration, res.EmptyReferConfiguration, "GetReferer Failed")
  771. }
  772. func (s *CosTestSuite) TestAccelerate() {
  773. opt := &cos.BucketPutAccelerateOptions{
  774. Status: "Enabled",
  775. Type: "COS",
  776. }
  777. _, err := s.Client.Bucket.PutAccelerate(context.Background(), opt)
  778. assert.Nil(s.T(), err, "PutAccelerate Failed")
  779. time.Sleep(time.Second)
  780. res, _, err := s.Client.Bucket.GetAccelerate(context.Background())
  781. assert.Nil(s.T(), err, "GetAccelerate Failed")
  782. assert.Equal(s.T(), opt.Status, res.Status, "GetAccelerate Failed")
  783. assert.Equal(s.T(), opt.Type, res.Type, "GetAccelerate Failed")
  784. opt.Status = "Suspended"
  785. _, err = s.Client.Bucket.PutAccelerate(context.Background(), opt)
  786. assert.Nil(s.T(), err, "PutAccelerate Failed")
  787. time.Sleep(time.Second)
  788. res, _, err = s.Client.Bucket.GetAccelerate(context.Background())
  789. assert.Nil(s.T(), err, "GetAccelerate Failed")
  790. assert.Equal(s.T(), opt.Status, res.Status, "GetAccelerate Failed")
  791. assert.Equal(s.T(), opt.Type, res.Type, "GetAccelerate Failed")
  792. }
  793. func (s *CosTestSuite) TestMultiCopy() {
  794. u := "http://" + kRepBucket + "-" + s.Appid + ".cos." + kRepRegion + ".myqcloud.com"
  795. iu, _ := url.Parse(u)
  796. ib := &cos.BaseURL{BucketURL: iu}
  797. c := cos.NewClient(ib, &http.Client{
  798. Transport: &cos.AuthorizationTransport{
  799. SecretID: os.Getenv("COS_SECRETID"),
  800. SecretKey: os.Getenv("COS_SECRETKEY"),
  801. },
  802. })
  803. opt := &cos.BucketPutOptions{
  804. XCosACL: "public-read",
  805. }
  806. // Notice in intranet the bucket host sometimes has i/o timeout problem
  807. r, err := c.Bucket.Put(context.Background(), opt)
  808. if err != nil && r.StatusCode == 409 {
  809. fmt.Println("BucketAlreadyOwnedByYou")
  810. } else if err != nil {
  811. assert.Nil(s.T(), err, "PutBucket Failed")
  812. }
  813. source := "test/objectMove1" + time.Now().Format(time.RFC3339)
  814. expected := "test"
  815. f := strings.NewReader(expected)
  816. r, err = c.Object.Put(context.Background(), source, f, nil)
  817. assert.Nil(s.T(), err, "PutObject Failed")
  818. time.Sleep(3 * time.Second)
  819. // Copy file
  820. soruceURL := fmt.Sprintf("%s/%s", iu.Host, source)
  821. dest := "test/objectMove1" + time.Now().Format(time.RFC3339)
  822. _, _, err = s.Client.Object.MultiCopy(context.Background(), dest, soruceURL, nil)
  823. assert.Nil(s.T(), err, "MultiCopy Failed")
  824. // Check content
  825. resp, err := s.Client.Object.Get(context.Background(), dest, nil)
  826. assert.Nil(s.T(), err, "GetObject Failed")
  827. bs, _ := ioutil.ReadAll(resp.Body)
  828. resp.Body.Close()
  829. result := string(bs)
  830. assert.Equal(s.T(), expected, result, "MultiCopy Failed, wrong content")
  831. }
  832. // End of api test
  833. // All methods that begin with "Test" are run as tests within a
  834. // suite.
  835. // In order for 'go test' to run this suite, we need to create
  836. // a normal test function and pass our suite to suite.Run
  837. func TestCosTestSuite(t *testing.T) {
  838. suite.Run(t, new(CosTestSuite))
  839. }
  840. func (s *CosTestSuite) TearDownSuite() {
  841. // Clean the file in bucket
  842. // r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  843. // assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  844. // for _, p := range r.Uploads {
  845. // // Abort
  846. // _, err = s.Client.Object.AbortMultipartUpload(context.Background(), p.Key, p.UploadID)
  847. // assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  848. // }
  849. // // Delete objects
  850. // opt := &cos.BucketGetOptions{
  851. // MaxKeys: 500,
  852. // }
  853. // v, _, err := s.Client.Bucket.Get(context.Background(), opt)
  854. // assert.Nil(s.T(), err, "GetBucket Failed")
  855. // for _, c := range v.Contents {
  856. // _, err := s.Client.Object.Delete(context.Background(), c.Key)
  857. // assert.Nil(s.T(), err, "DeleteObject Failed")
  858. // }
  859. // When clean up these infos, can not solve the concurrent test problem
  860. fmt.Println("tear down~")
  861. }