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.

683 lines
21 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. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/suite"
  8. "github.com/tencentyun/cos-go-sdk-v5"
  9. "io/ioutil"
  10. "math/rand"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "strings"
  15. "testing"
  16. "time"
  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) TestListMultipartUploads() {
  268. // Create new upload
  269. name := "test_multipart" + time.Now().Format(time.RFC3339)
  270. flag := false
  271. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  272. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  273. id := v.UploadID
  274. // List
  275. r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  276. assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  277. for _, p := range r.Uploads {
  278. if p.Key == name {
  279. assert.Equal(s.T(), id, p.UploadID, "ListMultipartUploads wrong uploadid")
  280. flag = true
  281. }
  282. }
  283. assert.Equal(s.T(), true, flag, "ListMultipartUploads wrong key")
  284. // Abort
  285. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, id)
  286. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  287. }
  288. // Object API
  289. func (s *CosTestSuite) TestPutHeadGetDeleteObject_10MB() {
  290. name := "test/objectPut" + time.Now().Format(time.RFC3339)
  291. b := make([]byte, 1024*1024*10)
  292. _, err := rand.Read(b)
  293. content := fmt.Sprintf("%X", b)
  294. f := strings.NewReader(content)
  295. _, err = s.Client.Object.Put(context.Background(), name, f, nil)
  296. assert.Nil(s.T(), err, "PutObject Failed")
  297. _, err = s.Client.Object.Head(context.Background(), name, nil)
  298. assert.Nil(s.T(), err, "HeadObject Failed")
  299. _, err = s.Client.Object.Delete(context.Background(), name)
  300. assert.Nil(s.T(), err, "DeleteObject Failed")
  301. }
  302. func (s *CosTestSuite) TestPutGetDeleteObjectByFile_10MB() {
  303. // Create tmp file
  304. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  305. newfile, err := os.Create(filePath)
  306. assert.Nil(s.T(), err, "create tmp file Failed")
  307. defer newfile.Close()
  308. name := "test/objectPutByFile" + time.Now().Format(time.RFC3339)
  309. b := make([]byte, 1024*1024*10)
  310. _, err = rand.Read(b)
  311. newfile.Write(b)
  312. _, err = s.Client.Object.PutFromFile(context.Background(), name, filePath, nil)
  313. assert.Nil(s.T(), err, "PutObject Failed")
  314. // Over write tmp file
  315. _, err = s.Client.Object.GetToFile(context.Background(), name, filePath, nil)
  316. assert.Nil(s.T(), err, "HeadObject Failed")
  317. _, err = s.Client.Object.Delete(context.Background(), name)
  318. assert.Nil(s.T(), err, "DeleteObject Failed")
  319. // remove the local tmp file
  320. err = os.Remove(filePath)
  321. assert.Nil(s.T(), err, "remove local file Failed")
  322. }
  323. func (s *CosTestSuite) TestPutGetDeleteObjectSpecialName() {
  324. f := strings.NewReader("test")
  325. name := s.SepFileName + time.Now().Format(time.RFC3339)
  326. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  327. assert.Nil(s.T(), err, "PutObject Failed")
  328. resp, err := s.Client.Object.Get(context.Background(), name, nil)
  329. assert.Nil(s.T(), err, "GetObject Failed")
  330. defer resp.Body.Close()
  331. bs, _ := ioutil.ReadAll(resp.Body)
  332. assert.Equal(s.T(), "test", string(bs), "GetObject failed content wrong")
  333. _, err = s.Client.Object.Delete(context.Background(), name)
  334. assert.Nil(s.T(), err, "DeleteObject Failed")
  335. }
  336. func (s *CosTestSuite) TestPutObjectToNonExistBucket() {
  337. u := "http://gosdknonexistbucket-" + s.Appid + ".cos." + s.Region + ".myqcloud.com"
  338. iu, _ := url.Parse(u)
  339. ib := &cos.BaseURL{BucketURL: iu}
  340. client := cos.NewClient(ib, &http.Client{
  341. Transport: &cos.AuthorizationTransport{
  342. SecretID: os.Getenv("COS_SECRETID"),
  343. SecretKey: os.Getenv("COS_SECRETKEY"),
  344. },
  345. })
  346. name := "test/objectPut.go"
  347. f := strings.NewReader("test")
  348. r, err := client.Object.Put(context.Background(), name, f, nil)
  349. assert.NotNil(s.T(), err, "PutObject ToNonExistBucket Failed")
  350. assert.Equal(s.T(), 404, r.StatusCode, "PutObject ToNonExistBucket, not 404")
  351. }
  352. func (s *CosTestSuite) TestPutGetObjectACL() {
  353. name := "test/objectACL.go" + time.Now().Format(time.RFC3339)
  354. f := strings.NewReader("test")
  355. _, err := s.Client.Object.Put(context.Background(), name, f, nil)
  356. assert.Nil(s.T(), err, "PutObject Failed")
  357. // Put acl
  358. opt := &cos.ObjectPutACLOptions{
  359. Header: &cos.ACLHeaderOptions{
  360. XCosACL: "public-read",
  361. },
  362. }
  363. _, err = s.Client.Object.PutACL(context.Background(), name, opt)
  364. assert.Nil(s.T(), err, "PutObjectACL Failed")
  365. v, _, err := s.Client.Object.GetACL(context.Background(), name)
  366. assert.Nil(s.T(), err, "GetObjectACL Failed")
  367. assert.Equal(s.T(), 2, len(v.AccessControlList), "GetLifecycle wrong number rules")
  368. _, err = s.Client.Object.Delete(context.Background(), name)
  369. assert.Nil(s.T(), err, "DeleteObject Failed")
  370. }
  371. func (s *CosTestSuite) TestPutObjectRestore() {
  372. name := "archivetest"
  373. putOpt := &cos.ObjectPutOptions{
  374. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  375. XCosStorageClass: "ARCHIVE",
  376. },
  377. }
  378. f := strings.NewReader("test")
  379. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  380. assert.Nil(s.T(), err, "PutObject Archive faild")
  381. opt := &cos.ObjectRestoreOptions{
  382. Days: 2,
  383. Tier: &cos.CASJobParameters{
  384. // Standard, Exepdited and Bulk
  385. Tier: "Expedited",
  386. },
  387. }
  388. resp, _ := s.Client.Object.PostRestore(context.Background(), name, opt)
  389. retCode := resp.StatusCode
  390. if retCode != 200 && retCode != 202 && retCode != 409 {
  391. right := false
  392. fmt.Println("PutObjectRestore get code is:", retCode)
  393. assert.Equal(s.T(), true, right, "PutObjectRestore Failed")
  394. }
  395. }
  396. func (s *CosTestSuite) TestCopyObject() {
  397. u := "http://gosdkcopytest-" + s.Appid + ".cos.ap-beijing-1.myqcloud.com"
  398. iu, _ := url.Parse(u)
  399. ib := &cos.BaseURL{BucketURL: iu}
  400. c := cos.NewClient(ib, &http.Client{
  401. Transport: &cos.AuthorizationTransport{
  402. SecretID: os.Getenv("COS_SECRETID"),
  403. SecretKey: os.Getenv("COS_SECRETKEY"),
  404. },
  405. })
  406. opt := &cos.BucketPutOptions{
  407. XCosACL: "public-read",
  408. }
  409. // Notice in intranet the bucket host sometimes has i/o timeout problem
  410. r, err := c.Bucket.Put(context.Background(), opt)
  411. if err != nil && r.StatusCode == 409 {
  412. fmt.Println("BucketAlreadyOwnedByYou")
  413. } else if err != nil {
  414. assert.Nil(s.T(), err, "PutBucket Failed")
  415. }
  416. source := "test/objectMove1" + time.Now().Format(time.RFC3339)
  417. expected := "test"
  418. f := strings.NewReader(expected)
  419. r, err = c.Object.Put(context.Background(), source, f, nil)
  420. assert.Nil(s.T(), err, "PutObject Failed")
  421. var version_id string
  422. if r.Header["X-Cos-Version-Id"] != nil {
  423. version_id = r.Header.Get("X-Cos-Version-Id")
  424. }
  425. time.Sleep(3 * time.Second)
  426. // Copy file
  427. soruceURL := fmt.Sprintf("%s/%s", iu.Host, source)
  428. dest := "test/objectMove1" + time.Now().Format(time.RFC3339)
  429. //opt := &cos.ObjectCopyOptions{}
  430. if version_id == "" {
  431. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil)
  432. } else {
  433. _, _, err = s.Client.Object.Copy(context.Background(), dest, soruceURL, nil, version_id)
  434. }
  435. assert.Nil(s.T(), err, "PutObjectCopy Failed")
  436. // Check content
  437. resp, err := s.Client.Object.Get(context.Background(), dest, nil)
  438. assert.Nil(s.T(), err, "GetObject Failed")
  439. bs, _ := ioutil.ReadAll(resp.Body)
  440. resp.Body.Close()
  441. result := string(bs)
  442. assert.Equal(s.T(), expected, result, "PutObjectCopy Failed, wrong content")
  443. }
  444. func (s *CosTestSuite) TestCreateAbortMultipartUpload() {
  445. name := "test_multipart" + time.Now().Format(time.RFC3339)
  446. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  447. assert.Nil(s.T(), err, "InitiateMultipartUpload Failed")
  448. _, err = s.Client.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
  449. assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  450. }
  451. func (s *CosTestSuite) TestCreateCompleteMultipartUpload() {
  452. name := "test/test_complete_upload" + time.Now().Format(time.RFC3339)
  453. v, _, err := s.Client.Object.InitiateMultipartUpload(context.Background(), name, nil)
  454. uploadID := v.UploadID
  455. blockSize := 1024 * 1024 * 3
  456. opt := &cos.CompleteMultipartUploadOptions{}
  457. for i := 1; i < 3; i++ {
  458. b := make([]byte, blockSize)
  459. _, err := rand.Read(b)
  460. content := fmt.Sprintf("%X", b)
  461. f := strings.NewReader(content)
  462. resp, err := s.Client.Object.UploadPart(
  463. context.Background(), name, uploadID, i, f, nil,
  464. )
  465. assert.Nil(s.T(), err, "UploadPart Failed")
  466. etag := resp.Header.Get("Etag")
  467. opt.Parts = append(opt.Parts, cos.Object{
  468. PartNumber: i, ETag: etag},
  469. )
  470. }
  471. _, _, err = s.Client.Object.CompleteMultipartUpload(
  472. context.Background(), name, uploadID, opt,
  473. )
  474. assert.Nil(s.T(), err, "CompleteMultipartUpload Failed")
  475. }
  476. func (s *CosTestSuite) TestSSE_C() {
  477. name := "test/TestSSE_C"
  478. content := "test sse-c " + time.Now().Format(time.RFC3339)
  479. f := strings.NewReader(content)
  480. putOpt := &cos.ObjectPutOptions{
  481. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  482. ContentType: "text/html",
  483. //XCosServerSideEncryption: "AES256",
  484. XCosSSECustomerAglo: "AES256",
  485. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  486. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  487. },
  488. ACLHeaderOptions: &cos.ACLHeaderOptions{
  489. XCosACL: "public-read",
  490. //XCosACL: "private",
  491. },
  492. }
  493. _, err := s.Client.Object.Put(context.Background(), name, f, putOpt)
  494. assert.Nil(s.T(), err, "PutObject with SSE failed")
  495. headOpt := &cos.ObjectHeadOptions{
  496. XCosSSECustomerAglo: "AES256",
  497. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  498. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  499. }
  500. _, err = s.Client.Object.Head(context.Background(), name, headOpt)
  501. assert.Nil(s.T(), err, "HeadObject with SSE failed")
  502. getOpt := &cos.ObjectGetOptions{
  503. XCosSSECustomerAglo: "AES256",
  504. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  505. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  506. }
  507. var resp *cos.Response
  508. resp, err = s.Client.Object.Get(context.Background(), name, getOpt)
  509. assert.Nil(s.T(), err, "GetObject with SSE failed")
  510. bodyBytes, _ := ioutil.ReadAll(resp.Body)
  511. bodyContent := string(bodyBytes)
  512. assert.Equal(s.T(), content, bodyContent, "GetObject with SSE failed, want: %+v, res: %+v", content, bodyContent)
  513. copyOpt := &cos.ObjectCopyOptions{
  514. &cos.ObjectCopyHeaderOptions{
  515. XCosCopySourceSSECustomerAglo: "AES256",
  516. XCosCopySourceSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  517. XCosCopySourceSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  518. },
  519. &cos.ACLHeaderOptions{},
  520. }
  521. copySource := s.Bucket + "-" + s.Appid + ".cos." + s.Region + ".myqcloud.com/" + name
  522. _, _, err = s.Client.Object.Copy(context.Background(), "test/TestSSE_C_Copy", copySource, copyOpt)
  523. assert.Nil(s.T(), err, "CopyObject with SSE failed")
  524. partIni := &cos.MultiUploadOptions{
  525. OptIni: &cos.InitiateMultipartUploadOptions{
  526. &cos.ACLHeaderOptions{},
  527. &cos.ObjectPutHeaderOptions{
  528. XCosSSECustomerAglo: "AES256",
  529. XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
  530. XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
  531. },
  532. },
  533. PartSize: 1,
  534. }
  535. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  536. newFile, err := os.Create(filePath)
  537. assert.Nil(s.T(), err, "create tmp file Failed")
  538. defer newFile.Close()
  539. b := make([]byte, 1024*10)
  540. _, err = rand.Read(b)
  541. newFile.Write(b)
  542. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/TestSSE_C_MultiUpload", filePath, partIni)
  543. assert.Nil(s.T(), err, "MultiUpload with SSE failed")
  544. err = os.Remove(filePath)
  545. assert.Nil(s.T(), err, "remove local file Failed")
  546. }
  547. func (s *CosTestSuite) TestMultiUpload() {
  548. filePath := "tmpfile" + time.Now().Format(time.RFC3339)
  549. newFile, err := os.Create(filePath)
  550. assert.Nil(s.T(), err, "create tmp file Failed")
  551. defer newFile.Close()
  552. b := make([]byte, 1024*1024*10)
  553. _, err = rand.Read(b)
  554. newFile.Write(b)
  555. partIni := &cos.MultiUploadOptions{}
  556. _, _, err = s.Client.Object.MultiUpload(context.Background(), "test/Test_MultiUpload", filePath, partIni)
  557. err = os.Remove(filePath)
  558. assert.Nil(s.T(), err, "remove tmp file failed")
  559. }
  560. // End of api test
  561. // All methods that begin with "Test" are run as tests within a
  562. // suite.
  563. // In order for 'go test' to run this suite, we need to create
  564. // a normal test function and pass our suite to suite.Run
  565. func TestCosTestSuite(t *testing.T) {
  566. suite.Run(t, new(CosTestSuite))
  567. }
  568. func (s *CosTestSuite) TearDownSuite() {
  569. // Clean the file in bucket
  570. // r, _, err := s.Client.Bucket.ListMultipartUploads(context.Background(), nil)
  571. // assert.Nil(s.T(), err, "ListMultipartUploads Failed")
  572. // for _, p := range r.Uploads {
  573. // // Abort
  574. // _, err = s.Client.Object.AbortMultipartUpload(context.Background(), p.Key, p.UploadID)
  575. // assert.Nil(s.T(), err, "AbortMultipartUpload Failed")
  576. // }
  577. // // Delete objects
  578. // opt := &cos.BucketGetOptions{
  579. // MaxKeys: 500,
  580. // }
  581. // v, _, err := s.Client.Bucket.Get(context.Background(), opt)
  582. // assert.Nil(s.T(), err, "GetBucket Failed")
  583. // for _, c := range v.Contents {
  584. // _, err := s.Client.Object.Delete(context.Background(), c.Key)
  585. // assert.Nil(s.T(), err, "DeleteObject Failed")
  586. // }
  587. // When clean up these infos, can not solve the concurrent test problem
  588. fmt.Println("tear down~")
  589. }