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.

1038 lines
34 KiB

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