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.

718 lines
22 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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/stretchr/testify/assert"
  15. "github.com/stretchr/testify/suite"
  16. "github.com/tencentyun/cos-go-sdk-v5"
  17. )
  18. // Define the suite, and absorb the built-in basic suite
  19. // functionality from testify - including a T() method which
  20. // returns the current testing context
  21. type CosTestSuite struct {
  22. suite.Suite
  23. VariableThatShouldStartAtFive int
  24. // CI client
  25. Client *cos.Client
  26. // Copy source client
  27. CClient *cos.Client
  28. Region string
  29. Bucket string
  30. Appid string
  31. // test_object
  32. TestObject string
  33. // special_file_name
  34. SepFileName string
  35. }
  36. func (s *CosTestSuite) SetupSuite() {
  37. fmt.Println("Set up test")
  38. // init
  39. s.TestObject = "test.txt"
  40. s.SepFileName = "中文" + "→↓←→↖↗↙↘! \"#$%&'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  41. // CI client for test interface
  42. // URL like this http://test-1253846586.cos.ap-guangzhou.myqcloud.com
  43. u := "http://cosgosdktest-1251668577.cos.ap-guangzhou.myqcloud.com"
  44. // Get the region
  45. iu, _ := url.Parse(u)
  46. p := strings.Split(iu.Host, ".")
  47. assert.Equal(s.T(), 5, len(p), "Bucket host is not right")
  48. s.Region = p[2]
  49. // Bucket name
  50. pp := strings.Split(p[0], "-")
  51. s.Bucket = pp[0]
  52. s.Appid = pp[1]
  53. ib := &cos.BaseURL{BucketURL: iu}
  54. s.Client = cos.NewClient(ib, &http.Client{
  55. Transport: &cos.AuthorizationTransport{
  56. SecretID: os.Getenv("COS_SECRETID"),
  57. SecretKey: os.Getenv("COS_SECRETKEY"),
  58. },
  59. })
  60. opt := &cos.BucketPutOptions{
  61. XCosACL: "public-read",
  62. }
  63. r, err := s.Client.Bucket.Put(context.Background(), opt)
  64. if err != nil && r.StatusCode == 409 {
  65. fmt.Println("BucketAlreadyOwnedByYou")
  66. } else if err != nil {
  67. assert.Nil(s.T(), err, "PutBucket Failed")
  68. }
  69. }
  70. // Begin of api test
  71. // Service API
  72. func (s *CosTestSuite) TestGetService() {
  73. _, _, err := s.Client.Service.Get(context.Background())
  74. assert.Nil(s.T(), err, "GetService Failed")
  75. }
  76. // Bucket API
  77. func (s *CosTestSuite) TestPutHeadDeleteBucket() {
  78. // Notic sometimes the bucket host can not analyis, may has i/o timeout problem
  79. u := "http://gosdkbuckettest-" + s.Appid + ".cos.ap-beijing-1.myqcloud.com"
  80. iu, _ := url.Parse(u)
  81. ib := &cos.BaseURL{BucketURL: iu}
  82. client := cos.NewClient(ib, &http.Client{
  83. Transport: &cos.AuthorizationTransport{
  84. SecretID: os.Getenv("COS_SECRETID"),
  85. SecretKey: os.Getenv("COS_SECRETKEY"),
  86. },
  87. })
  88. r, err := client.Bucket.Put(context.Background(), nil)
  89. if err != nil && r.StatusCode == 409 {
  90. fmt.Println("BucketAlreadyOwnedByYou")
  91. } else if err != nil {
  92. assert.Nil(s.T(), err, "PutBucket Failed")
  93. }
  94. if err != nil {
  95. panic(err)
  96. }
  97. time.Sleep(3 * time.Second)
  98. _, err = client.Bucket.Head(context.Background())
  99. assert.Nil(s.T(), err, "HeadBucket Failed")
  100. if err == nil {
  101. _, err = client.Bucket.Delete(context.Background())
  102. assert.Nil(s.T(), err, "DeleteBucket Failed")
  103. }
  104. }
  105. func (s *CosTestSuite) TestPutBucketACLIllegal() {
  106. opt := &cos.BucketPutACLOptions{
  107. Header: &cos.ACLHeaderOptions{
  108. XCosACL: "public-read-writ",
  109. },
  110. }
  111. _, err := s.Client.Bucket.PutACL(context.Background(), opt)
  112. assert.NotNil(s.T(), err, "PutBucketACL illegal Failed")
  113. }
  114. func (s *CosTestSuite) TestPutGetBucketACLNormal() {
  115. // with header
  116. opt := &cos.BucketPutACLOptions{
  117. Header: &cos.ACLHeaderOptions{
  118. XCosACL: "private",
  119. },
  120. }
  121. _, err := s.Client.Bucket.PutACL(context.Background(), opt)
  122. assert.Nil(s.T(), err, "PutBucketACL normal Failed")
  123. v, _, err := s.Client.Bucket.GetACL(context.Background())
  124. assert.Nil(s.T(), err, "GetBucketACL normal Failed")
  125. assert.Equal(s.T(), 1, len(v.AccessControlList), "GetBucketACL normal Failed, must be private")
  126. }
  127. func (s *CosTestSuite) TestGetBucket() {
  128. opt := &cos.BucketGetOptions{
  129. Prefix: "中文",
  130. MaxKeys: 3,
  131. }
  132. _, _, err := s.Client.Bucket.Get(context.Background(), opt)
  133. assert.Nil(s.T(), err, "GetBucket Failed")
  134. }
  135. func (s *CosTestSuite) TestGetBucketLocation() {
  136. v, _, err := s.Client.Bucket.GetLocation(context.Background())
  137. assert.Nil(s.T(), err, "GetLocation Failed")
  138. assert.Equal(s.T(), s.Region, v.Location, "GetLocation wrong region")
  139. }
  140. func (s *CosTestSuite) TestPutGetDeleteCORS() {
  141. opt := &cos.BucketPutCORSOptions{
  142. Rules: []cos.BucketCORSRule{
  143. {
  144. AllowedOrigins: []string{"http://www.qq.com"},
  145. AllowedMethods: []string{"PUT", "GET"},
  146. AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
  147. MaxAgeSeconds: 500,
  148. ExposeHeaders: []string{"x-cos-meta-test1"},
  149. },
  150. },
  151. }
  152. _, err := s.Client.Bucket.PutCORS(context.Background(), opt)
  153. assert.Nil(s.T(), err, "PutBucketCORS Failed")
  154. v, _, err := s.Client.Bucket.GetCORS(context.Background())
  155. assert.Nil(s.T(), err, "GetBucketCORS Failed")
  156. assert.Equal(s.T(), 1, len(v.Rules), "GetBucketCORS wrong number rules")
  157. }
  158. func (s *CosTestSuite) TestVersionAndReplication() {
  159. opt := &cos.BucketPutVersionOptions{
  160. // Enabled or Suspended, the versioning once opened can not close.
  161. Status: "Enabled",
  162. }
  163. _, err := s.Client.Bucket.PutVersioning(context.Background(), opt)
  164. assert.Nil(s.T(), err, "PutVersioning Failed")
  165. v, _, err := s.Client.Bucket.GetVersioning(context.Background())
  166. assert.Nil(s.T(), err, "GetVersioning Failed")
  167. assert.Equal(s.T(), "Enabled", v.Status, "Get Wrong Version status")
  168. repOpt := &cos.PutBucketReplicationOptions{
  169. // qcs::cam::uin/[UIN]:uin/[Subaccount]
  170. Role: "qcs::cam::uin/2779643970:uin/2779643970",
  171. Rule: []cos.BucketReplicationRule{
  172. {
  173. ID: "1",
  174. // Enabled or Disabled
  175. Status: "Enabled",
  176. Destination: &cos.ReplicationDestination{
  177. // qcs::cos:[Region]::[Bucketname-Appid]
  178. Bucket: "qcs::cos:ap-beijing::alanbj-1251668577",
  179. },
  180. },
  181. },
  182. }
  183. _, err = s.Client.Bucket.PutBucketReplication(context.Background(), repOpt)
  184. assert.Nil(s.T(), err, "PutBucketReplication Failed")
  185. vr, _, err := s.Client.Bucket.GetBucketReplication(context.Background())
  186. assert.Nil(s.T(), err, "GetBucketReplication Failed")
  187. for _, r := range vr.Rule {
  188. assert.Equal(s.T(), "Enabled", r.Status, "Get Wrong Version status")
  189. assert.Equal(s.T(), "qcs::cos:ap-beijing::alanbj-1251668577", r.Destination.Bucket, "Get Wrong Version status")
  190. }
  191. _, err = s.Client.Bucket.DeleteBucketReplication(context.Background())
  192. assert.Nil(s.T(), err, "DeleteBucketReplication Failed")
  193. }
  194. func (s *CosTestSuite) TestBucketInventory() {
  195. id := "test1"
  196. opt := &cos.BucketPutInventoryOptions{
  197. ID: id,
  198. // True or False
  199. IsEnabled: "True",
  200. IncludedObjectVersions: "All",
  201. Filter: &cos.BucketInventoryFilter{
  202. Prefix: "test",
  203. },
  204. OptionalFields: &cos.BucketInventoryOptionalFields{
  205. BucketInventoryFields: []string{
  206. "Size", "LastModifiedDate",
  207. },
  208. },
  209. Schedule: &cos.BucketInventorySchedule{
  210. // Weekly or Daily
  211. Frequency: "Daily",
  212. },
  213. Destination: &cos.BucketInventoryDestination{
  214. BucketDestination: &cos.BucketInventoryDestinationContent{
  215. Bucket: "qcs::cos:ap-guangzhou::alangz-1251668577",
  216. Format: "CSV",
  217. },
  218. },
  219. }
  220. _, err := s.Client.Bucket.PutBucketInventoryTest(context.Background(), id, opt)
  221. assert.Nil(s.T(), err, "PutBucketInventory Failed")
  222. v, _, err := s.Client.Bucket.GetBucketInventoryTest(context.Background(), id)
  223. assert.Nil(s.T(), err, "GetBucketInventory Failed")
  224. assert.Equal(s.T(), "test1", v.ID, "Get Wrong inventory id")
  225. assert.Equal(s.T(), "true", v.IsEnabled, "Get Wrong inventory isenabled")
  226. assert.Equal(s.T(), "qcs::cos:ap-guangzhou::alangz-1251668577", v.Destination.BucketDestination.Bucket, "Get Wrong inventory isenabled")
  227. _, err = s.Client.Bucket.DeleteBucketInventoryTest(context.Background(), id)
  228. assert.Nil(s.T(), err, "DeleteBucketInventory Failed")
  229. }
  230. func (s *CosTestSuite) TestBucketLogging() {
  231. opt := &cos.BucketPutLoggingOptions{
  232. LoggingEnabled: &cos.BucketLoggingEnabled{
  233. // The bucket must same region.
  234. TargetBucket: "alangz-1251668577",
  235. },
  236. }
  237. _, err := s.Client.Bucket.PutBucketLoggingTest(context.Background(), opt)
  238. assert.Nil(s.T(), err, "PutBucketLogging Failed")
  239. v, _, err := s.Client.Bucket.GetBucketLoggingTest(context.Background())
  240. assert.Nil(s.T(), err, "GetBucketLogging Failed")
  241. assert.Equal(s.T(), "alangz-1251668577", v.LoggingEnabled.TargetBucket, "Get Wrong Version status")
  242. }
  243. func (s *CosTestSuite) TestPutGetDeleteLifeCycle() {
  244. lc := &cos.BucketPutLifecycleOptions{
  245. Rules: []cos.BucketLifecycleRule{
  246. {
  247. ID: "1234",
  248. Filter: &cos.BucketLifecycleFilter{Prefix: "test"},
  249. Status: "Enabled",
  250. Transition: &cos.BucketLifecycleTransition{
  251. Days: 10,
  252. StorageClass: "Standard",
  253. },
  254. },
  255. },
  256. }
  257. _, err := s.Client.Bucket.PutLifecycle(context.Background(), lc)
  258. assert.Nil(s.T(), err, "PutBucketLifecycle Failed")
  259. _, r, err := s.Client.Bucket.GetLifecycle(context.Background())
  260. // Might cleaned by other case concrrent
  261. if err != nil && 404 != r.StatusCode {
  262. assert.Nil(s.T(), err, "GetBucketLifecycle Failed")
  263. }
  264. _, err = s.Client.Bucket.DeleteLifecycle(context.Background())
  265. assert.Nil(s.T(), err, "DeleteBucketLifecycle Failed")
  266. }
  267. func (s *CosTestSuite) TestPutGetDeleteWebsite() {
  268. opt := &cos.BucketPutWebsiteOptions{
  269. Index: "index.html",
  270. Error: &cos.ErrorDocument{"index_backup.html"},
  271. RoutingRules: &cos.WebsiteRoutingRules{
  272. []cos.WebsiteRoutingRule{
  273. {
  274. ConditionErrorCode: "404",
  275. RedirectProtocol: "https",
  276. RedirectReplaceKey: "404.html",
  277. },
  278. {
  279. ConditionPrefix: "docs/",
  280. RedirectProtocol: "https",
  281. RedirectReplaceKeyPrefix: "documents/",
  282. },
  283. },
  284. },
  285. }
  286. _, err := s.Client.Bucket.PutWebsite(context.Background(), opt)
  287. assert.Nil(s.T(), err, "PutBucketWebsite Failed")
  288. res, rsp, err := s.Client.Bucket.GetWebsite(context.Background())
  289. if err != nil && 404 != rsp.StatusCode {
  290. assert.Nil(s.T(), err, "GetBucketWebsite Failed")
  291. }
  292. assert.Equal(s.T(), opt.Index, res.Index, "GetBucketWebsite Failed")
  293. assert.Equal(s.T(), opt.Error, res.Error, "GetBucketWebsite Failed")
  294. assert.Equal(s.T(), opt.RedirectProtocol, res.RedirectProtocol, "GetBucketWebsite Failed")
  295. _, err = s.Client.Bucket.DeleteWebsite(context.Background())
  296. assert.Nil(s.T(), err, "DeleteBucketWebsite Failed")
  297. }
  298. func (s *CosTestSuite) TestListMultipartUploads() {
  299. // Create new upload
  300. name := "test_multipart" + time.Now().Format(time.RFC3339)
  301. flag := false
  302. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  303. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  304. id := v.UploadID
  305. // List
  306. r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  307. assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  308. for _, p := range r.Uploads {
  309. if p.Key == name {
  310. assert.Equal(s.T(), id, p.UploadID, "ListMultipartUploads wrong uploadid")
  311. flag = true
  312. }
  313. }
  314. assert.Equal(s.T(), true, flag, "ListMultipartUploads wrong key")
  315. // Abort
  316. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, id)
  317. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  318. }
  319. // Object API
  320. func (s *CosTestSuite) TestPutHeadGetDeleteObject_10MB() {
  321. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  322. b := make([]byte, 1024*1024*10)
  323. _, err := rand.Read(b)
  324. content := fmt.Sprintf("%X", b)
  325. f := strings.NewReader(content)
  326. _, err = s.Client.Object.Put(context.Background(), name, f, nil)
  327. assert.Nil(s.T(), err, "PutObject Failed")
  328. _, err = s.Client.Object.Head(context.Background(), name, nil)
  329. assert.Nil(s.T(), err, "HeadObject Failed")
  330. _, err = s.Client.Object.Delete(context.Background(), name)
  331. assert.Nil(s.T(), err, "DeleteObject Failed")
  332. }
  333. func (s *CosTestSuite) TestPutGetDeleteObjectByFile_10MB() {
  334. // Create tmp file
  335. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  336. newfile, err := os.Create(filePath)
  337. assert.Nil(s.T(), err, "create tmp file Failed")
  338. defer newfile.Close()
  339. name := "test/objectPutByFile" + time.Now().Format(time.RFC3339)
  340. b := make([]byte, 1024*1024*10)
  341. _, err = rand.Read(b)
  342. newfile.Write(b)
  343. _, err = s.Client.Object.PutFromFile(context.Background(), name, filePath, nil)
  344. assert.Nil(s.T(), err, "PutObject Failed")
  345. // Over write tmp file
  346. _, err = s.Client.Object.GetToFile(context.Background(), name, filePath, nil)
  347. assert.Nil(s.T(), err, "HeadObject Failed")
  348. _, err = s.Client.Object.Delete(context.Background(), name)
  349. assert.Nil(s.T(), err, "DeleteObject Failed")
  350. // remove the local tmp file
  351. err = os.Remove(filePath)
  352. assert.Nil(s.T(), err, "remove local file Failed")
  353. }
  354. func (s *CosTestSuite) TestPutGetDeleteObjectSpecialName() {
  355. f := strings.NewReader("test")
  356. name := s.SepFileName + time.Now().Format(time.RFC3339)
  357. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  358. assert.Nil(s.T(), err, "PutObject Failed")
  359. resp, err := s.Client.Object.Get(context.Background(), name, nil)
  360. assert.Nil(s.T(), err, "GetObject Failed")
  361. defer resp.Body.Close()
  362. bs, _ := ioutil.ReadAll(resp.Body)
  363. assert.Equal(s.T(), "test", string(bs), "GetObject failed content wrong")
  364. _, err = s.Client.Object.Delete(context.Background(), name)
  365. assert.Nil(s.T(), err, "DeleteObject Failed")
  366. }
  367. func (s *CosTestSuite) TestPutObjectToNonExistBucket() {
  368. u := "http://gosdknonexistbucket-" + s.Appid + ".cos." + s.Region + ".myqcloud.com"
  369. iu, _ := url.Parse(u)
  370. ib := &cos.BaseURL{BucketURL: iu}
  371. client := cos.NewClient(ib, &http.Client{
  372. Transport: &cos.AuthorizationTransport{
  373. SecretID: os.Getenv("COS_SECRETID"),
  374. SecretKey: os.Getenv("COS_SECRETKEY"),
  375. },
  376. })
  377. name := "test/objectPut.go"
  378. f := strings.NewReader("test")
  379. r, err := client.Object.Put(context.Background(), name, f, nil)
  380. assert.NotNil(s.T(), err, "PutObject ToNonExistBucket Failed")
  381. assert.Equal(s.T(), 404, r.StatusCode, "PutObject ToNonExistBucket, not 404")
  382. }
  383. func (s *CosTestSuite) TestPutGetObjectACL() {
  384. name := "test/objectACL.go" + time.Now().Format(time.RFC3339)
  385. f := strings.NewReader("test")
  386. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  387. assert.Nil(s.T(), err, "PutObject Failed")
  388. // Put acl
  389. opt := &cos.ObjectPutACLOptions{
  390. Header: &cos.ACLHeaderOptions{
  391. XCosACL: "public-read",
  392. },
  393. }
  394. _, err = s.Client.Object.PutACL(context.Background(), name, opt)
  395. assert.Nil(s.T(), err, "PutObjectACL Failed")
  396. v, _, err := s.Client.Object.GetACL(context.Background(), name)
  397. assert.Nil(s.T(), err, "GetObjectACL Failed")
  398. assert.Equal(s.T(), 2, len(v.AccessControlList), "GetLifecycle wrong number rules")
  399. _, err = s.Client.Object.Delete(context.Background(), name)
  400. assert.Nil(s.T(), err, "DeleteObject Failed")
  401. }
  402. func (s *CosTestSuite) TestPutObjectRestore() {
  403. name := "archivetest"
  404. putOpt := &cos.ObjectPutOptions{
  405. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  406. XCosStorageClass: "ARCHIVE",
  407. },
  408. }
  409. f := strings.NewReader("test")
  410. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  411. assert.Nil(s.T(), err, "PutObject Archive faild")
  412. opt := &cos.ObjectRestoreOptions{
  413. Days: 2,
  414. Tier: &cos.CASJobParameters{
  415. // Standard, Exepdited and Bulk
  416. Tier: "Expedited",
  417. },
  418. }
  419. resp, _ := s.Client.Object.PostRestore(context.Background(), name, opt)
  420. retCode := resp.StatusCode
  421. if retCode != 200 && retCode != 202 && retCode != 409 {
  422. right := false
  423. fmt.Println("PutObjectRestore get code is:", retCode)
  424. assert.Equal(s.T(), true, right, "PutObjectRestore Failed")
  425. }
  426. }
  427. func (s *CosTestSuite) TestCopyObject() {
  428. u := "http://gosdkcopytest-" + s.Appid + ".cos.ap-beijing-1.myqcloud.com"
  429. iu, _ := url.Parse(u)
  430. ib := &cos.BaseURL{BucketURL: iu}
  431. c := cos.NewClient(ib, &http.Client{
  432. Transport: &cos.AuthorizationTransport{
  433. SecretID: os.Getenv("COS_SECRETID"),
  434. SecretKey: os.Getenv("COS_SECRETKEY"),
  435. },
  436. })
  437. opt := &cos.BucketPutOptions{
  438. XCosACL: "public-read",
  439. }
  440. // Notice in intranet the bucket host sometimes has i/o timeout problem
  441. r, err := c.Bucket.Put(context.Background(), opt)
  442. if err != nil && r.StatusCode == 409 {
  443. fmt.Println("BucketAlreadyOwnedByYou")
  444. } else if err != nil {
  445. assert.Nil(s.T(), err, "PutBucket Failed")
  446. }
  447. source := "test/objectMove1" + time.Now().Format(time.RFC3339)
  448. expected := "test"
  449. f := strings.NewReader(expected)
  450. r, err = c.Object.Put(context.Background(), source, f, nil)
  451. assert.Nil(s.T(), err, "PutObject Failed")
  452. var version_id string
  453. if r.Header["X-Cos-Version-Id"] != nil {
  454. version_id = r.Header.Get("X-Cos-Version-Id")
  455. }
  456. time.Sleep(3 * time.Second)
  457. // Copy file
  458. soruceURL := fmt.Sprintf("%s/%s", iu.Host, source)
  459. dest := "test/objectMove1" + time.Now().Format(time.RFC3339)
  460. //opt := &cos.ObjectCopyOptions{}
  461. if version_id == "" {
  462. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil)
  463. } else {
  464. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil, version_id)
  465. }
  466. assert.Nil(s.T(), err, "PutObjectCopy Failed")
  467. // Check content
  468. resp, err := s.Client.Object.Get(context.Background(), dest, nil)
  469. assert.Nil(s.T(), err, "GetObject Failed")
  470. bs, _ := ioutil.ReadAll(resp.Body)
  471. resp.Body.Close()
  472. result := string(bs)
  473. assert.Equal(s.T(), expected, result, "PutObjectCopy Failed, wrong content")
  474. }
  475. func (s *CosTestSuite) TestCreateAbortMultipartUpload() {
  476. name := "test_multipart" + time.Now().Format(time.RFC3339)
  477. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  478. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  479. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
  480. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  481. }
  482. func (s *CosTestSuite) TestCreateCompleteMultipartUpload() {
  483. name := "test/test_complete_upload" + time.Now().Format(time.RFC3339)
  484. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  485. uploadID := v.UploadID
  486. blockSize := 1024 * 1024 * 3
  487. opt := &cos.CompleteMultipartUploadOptions{}
  488. for i := 1; i < 3; i++ {
  489. b := make([]byte, blockSize)
  490. _, err := rand.Read(b)
  491. content := fmt.Sprintf("%X", b)
  492. f := strings.NewReader(content)
  493. resp, err := s.Client.Object.UploadPart(
  494. context.Background(), name, uploadID, i, f, nil,
  495. )
  496. assert.Nil(s.T(), err, "UploadPart Failed")
  497. etag := resp.Header.Get("Etag")
  498. opt.Parts = append(opt.Parts, cos.Object{
  499. PartNumber: i, ETag: etag},
  500. )
  501. }
  502. _, _, err = s.Client.Object.CompleteMultipartUpload(
  503. context.Background(), name, uploadID, opt,
  504. )
  505. assert.Nil(s.T(), err, "CompleteMultipartUpload Failed")
  506. }
  507. func (s *CosTestSuite) TestSSE_C() {
  508. name := "test/TestSSE_C"
  509. content := "test sse-c " + time.Now().Format(time.RFC3339)
  510. f := strings.NewReader(content)
  511. putOpt := &cos.ObjectPutOptions{
  512. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  513. ContentType: "text/html",
  514. //XCosServerSideEncryption: "AES256",
  515. XCosSSECustomerAglo: "AES256",
  516. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  517. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  518. },
  519. ACLHeaderOptions: &cos.ACLHeaderOptions{
  520. XCosACL: "public-read",
  521. //XCosACL: "private",
  522. },
  523. }
  524. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  525. assert.Nil(s.T(), err, "PutObject with SSE failed")
  526. headOpt := &cos.ObjectHeadOptions{
  527. XCosSSECustomerAglo: "AES256",
  528. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  529. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  530. }
  531. _, err = s.Client.Object.Head(context.Background(), name, headOpt)
  532. assert.Nil(s.T(), err, "HeadObject with SSE failed")
  533. getOpt := &cos.ObjectGetOptions{
  534. XCosSSECustomerAglo: "AES256",
  535. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  536. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  537. }
  538. var resp *cos.Response
  539. resp, err = s.Client.Object.Get(context.Background(), name, getOpt)
  540. assert.Nil(s.T(), err, "GetObject with SSE failed")
  541. bodyBytes, _ := ioutil.ReadAll(resp.Body)
  542. bodyContent := string(bodyBytes)
  543. assert.Equal(s.T(), content, bodyContent, "GetObject with SSE failed, want: %+v, res: %+v", content, bodyContent)
  544. copyOpt := &cos.ObjectCopyOptions{
  545. &cos.ObjectCopyHeaderOptions{
  546. XCosCopySourceSSECustomerAglo: "AES256",
  547. XCosCopySourceSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  548. XCosCopySourceSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  549. },
  550. &cos.ACLHeaderOptions{},
  551. }
  552. copySource := s.Bucket + "-" + s.Appid + ".cos." + s.Region + ".myqcloud.com/" + name
  553. _, _, err = s.Client.Object.Copy(context.Background(), "test/TestSSE_C_Copy", copySource, copyOpt)
  554. assert.Nil(s.T(), err, "CopyObject with SSE failed")
  555. partIni := &cos.MultiUploadOptions{
  556. OptIni: &cos.InitiateMultipartUploadOptions{
  557. &cos.ACLHeaderOptions{},
  558. &cos.ObjectPutHeaderOptions{
  559. XCosSSECustomerAglo: "AES256",
  560. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  561. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  562. },
  563. },
  564. PartSize: 1,
  565. }
  566. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  567. newFile, err := os.Create(filePath)
  568. assert.Nil(s.T(), err, "create tmp file Failed")
  569. defer newFile.Close()
  570. b := make([]byte, 1024*10)
  571. _, err = rand.Read(b)
  572. newFile.Write(b)
  573. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/TestSSE_C_MultiUpload", filePath, partIni)
  574. assert.Nil(s.T(), err, "MultiUpload with SSE failed")
  575. err = os.Remove(filePath)
  576. assert.Nil(s.T(), err, "remove local file Failed")
  577. }
  578. func (s *CosTestSuite) TestMultiUpload() {
  579. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  580. newFile, err := os.Create(filePath)
  581. assert.Nil(s.T(), err, "create tmp file Failed")
  582. defer newFile.Close()
  583. b := make([]byte, 1024*1024*10)
  584. _, err = rand.Read(b)
  585. newFile.Write(b)
  586. partIni := &cos.MultiUploadOptions{}
  587. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/Test_MultiUpload", filePath, partIni)
  588. err = os.Remove(filePath)
  589. assert.Nil(s.T(), err, "remove tmp file failed")
  590. }
  591. // End of api test
  592. // All methods that begin with "Test" are run as tests within a
  593. // suite.
  594. // In order for 'go test' to run this suite, we need to create
  595. // a normal test function and pass our suite to suite.Run
  596. func TestCosTestSuite(t *testing.T) {
  597. suite.Run(t, new(CosTestSuite))
  598. }
  599. func (s *CosTestSuite) TearDownSuite() {
  600. // Clean the file in bucket
  601. // r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  602. // assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  603. // for _, p := range r.Uploads {
  604. // // Abort
  605. // _, err = s.Client.Object.AbortMultipartUpload(context.Background(), p.Key, p.UploadID)
  606. // assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  607. // }
  608. // // Delete objects
  609. // opt := &cos.BucketGetOptions{
  610. // MaxKeys: 500,
  611. // }
  612. // v, _, err := s.Client.Bucket.Get(context.Background(), opt)
  613. // assert.Nil(s.T(), err, "GetBucket Failed")
  614. // for _, c := range v.Contents {
  615. // _, err := s.Client.Object.Delete(context.Background(), c.Key)
  616. // assert.Nil(s.T(), err, "DeleteObject Failed")
  617. // }
  618. // When clean up these infos, can not solve the concurrent test problem
  619. fmt.Println("tear down~")
  620. }