bucket_credential_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. // Credentials test
  2. package oss
  3. import (
  4. "bytes"
  5. "io/ioutil"
  6. "math/rand"
  7. "os"
  8. "strconv"
  9. "strings"
  10. . "gopkg.in/check.v1"
  11. )
  12. type OssCredentialBucketSuite struct {
  13. client *Client
  14. creClient *Client
  15. bucket *Bucket
  16. creBucket *Bucket
  17. }
  18. var _ = Suite(&OssCredentialBucketSuite{})
  19. func (cs *OssCredentialBucketSuite) credentialSubUser(c *C) {
  20. client, err := New(endpoint, accessID, accessKey)
  21. c.Assert(err, IsNil)
  22. err = client.CreateBucket(credentialBucketName)
  23. c.Assert(err, IsNil)
  24. cs.client = client
  25. policyInfo := `
  26. {
  27. "Version":"1",
  28. "Statement":[
  29. {
  30. "Action":[
  31. "oss:*"
  32. ],
  33. "Effect":"Allow",
  34. "Principal":["` + credentialUID + `"],
  35. "Resource":["acs:oss:*:*:` + credentialBucketName + `", "acs:oss:*:*:` + credentialBucketName + `/*"]
  36. }
  37. ]
  38. }`
  39. err = client.SetBucketPolicy(credentialBucketName, policyInfo)
  40. c.Assert(err, IsNil)
  41. bucket, err := cs.client.Bucket(credentialBucketName)
  42. c.Assert(err, IsNil)
  43. cs.bucket = bucket
  44. }
  45. // SetUpSuite runs once when the suite starts running.
  46. func (cs *OssCredentialBucketSuite) SetUpSuite(c *C) {
  47. if credentialUID == "" {
  48. testLogger.Println("the cerdential UID is NULL, skip the credential test")
  49. c.Skip("the credential Uid is null")
  50. }
  51. cs.credentialSubUser(c)
  52. client, err := New(endpoint, credentialAccessID, credentialAccessKey)
  53. c.Assert(err, IsNil)
  54. cs.creClient = client
  55. bucket, err := cs.creClient.Bucket(credentialBucketName)
  56. c.Assert(err, IsNil)
  57. cs.creBucket = bucket
  58. testLogger.Println("test credetial bucket started")
  59. }
  60. func (cs *OssCredentialBucketSuite) TearDownSuite(c *C) {
  61. if credentialUID == "" {
  62. c.Skip("the credential Uid is null")
  63. }
  64. for _, bucket := range []*Bucket{cs.bucket} {
  65. // Delete multipart
  66. keyMarker := KeyMarker("")
  67. uploadIDMarker := UploadIDMarker("")
  68. for {
  69. lmu, err := bucket.ListMultipartUploads(keyMarker, uploadIDMarker)
  70. c.Assert(err, IsNil)
  71. for _, upload := range lmu.Uploads {
  72. imur := InitiateMultipartUploadResult{Bucket: credentialBucketName, Key: upload.Key, UploadID: upload.UploadID}
  73. err = bucket.AbortMultipartUpload(imur)
  74. c.Assert(err, IsNil)
  75. }
  76. keyMarker = KeyMarker(lmu.NextKeyMarker)
  77. uploadIDMarker = UploadIDMarker(lmu.NextUploadIDMarker)
  78. if !lmu.IsTruncated {
  79. break
  80. }
  81. }
  82. // Delete objects
  83. marker := Marker("")
  84. for {
  85. lor, err := bucket.ListObjects(marker)
  86. c.Assert(err, IsNil)
  87. for _, object := range lor.Objects {
  88. err = bucket.DeleteObject(object.Key)
  89. c.Assert(err, IsNil)
  90. }
  91. marker = Marker(lor.NextMarker)
  92. if !lor.IsTruncated {
  93. break
  94. }
  95. }
  96. }
  97. err := cs.client.DeleteBucket(credentialBucketName)
  98. c.Assert(err, IsNil)
  99. testLogger.Println("test credential bucket completed")
  100. }
  101. // Test put/get/list/delte object
  102. func (cs *OssCredentialBucketSuite) TestReqerPaymentNoRequester(c *C) {
  103. // Set bucket is requester who send the request
  104. reqPayConf := RequestPaymentConfiguration{
  105. Payer: string(Requester),
  106. }
  107. err := cs.client.SetBucketRequestPayment(credentialBucketName, reqPayConf)
  108. c.Assert(err, IsNil)
  109. key := objectNamePrefix + RandStr(8)
  110. objectValue := RandStr(18)
  111. // Put object
  112. err = cs.creBucket.PutObject(key, strings.NewReader(objectValue))
  113. c.Assert(err, NotNil)
  114. // Get object
  115. _, err = cs.creBucket.GetObject(key)
  116. c.Assert(err, NotNil)
  117. // List object
  118. _, err = cs.creBucket.ListObjects()
  119. c.Assert(err, NotNil)
  120. err = cs.creBucket.DeleteObject(key)
  121. c.Assert(err, NotNil)
  122. // Set bucket is BucketOwner
  123. reqPayConf.Payer = string(BucketOwner)
  124. err = cs.client.SetBucketRequestPayment(credentialBucketName, reqPayConf)
  125. c.Assert(err, IsNil)
  126. }
  127. // Test put/get/list/delte object
  128. func (cs *OssCredentialBucketSuite) TestReqerPaymentWithRequester(c *C) {
  129. // Set bucket is requester who send the request
  130. reqPayConf := RequestPaymentConfiguration{
  131. Payer: string(Requester),
  132. }
  133. err := cs.client.SetBucketRequestPayment(credentialBucketName, reqPayConf)
  134. c.Assert(err, IsNil)
  135. key := objectNamePrefix + RandStr(8)
  136. objectValue := RandStr(18)
  137. // Put object with a bucketowner
  138. err = cs.creBucket.PutObject(key, strings.NewReader(objectValue), RequestPayer(BucketOwner))
  139. c.Assert(err, NotNil)
  140. // Put object
  141. err = cs.creBucket.PutObject(key, strings.NewReader(objectValue), RequestPayer(Requester))
  142. c.Assert(err, IsNil)
  143. // Get object
  144. body, err := cs.creBucket.GetObject(key, RequestPayer(Requester))
  145. c.Assert(err, IsNil)
  146. defer body.Close()
  147. data, err := ioutil.ReadAll(body)
  148. c.Assert(err, IsNil)
  149. c.Assert(string(data), Equals, objectValue)
  150. // List object
  151. lor, err := cs.creBucket.ListObjects(RequestPayer(Requester))
  152. c.Assert(err, IsNil)
  153. c.Assert(len(lor.Objects), Equals, 1)
  154. err = cs.creBucket.DeleteObject(key, RequestPayer(Requester))
  155. c.Assert(err, IsNil)
  156. // Set bucket is BucketOwner
  157. reqPayConf.Payer = string(BucketOwner)
  158. err = cs.client.SetBucketRequestPayment(credentialBucketName, reqPayConf)
  159. c.Assert(err, IsNil)
  160. }
  161. // Test put/get/list/delte object
  162. func (cs *OssCredentialBucketSuite) TestOwnerPaymentNoRequester(c *C) {
  163. // Set bucket is requester who send the request
  164. reqPayConf := RequestPaymentConfiguration{
  165. Payer: string(BucketOwner),
  166. }
  167. err := cs.client.SetBucketRequestPayment(credentialBucketName, reqPayConf)
  168. c.Assert(err, IsNil)
  169. key := objectNamePrefix + RandStr(8)
  170. objectValue := RandStr(18)
  171. // Put object
  172. err = cs.creBucket.PutObject(key, strings.NewReader(objectValue))
  173. c.Assert(err, IsNil)
  174. // Get object
  175. body, err := cs.creBucket.GetObject(key)
  176. c.Assert(err, IsNil)
  177. defer body.Close()
  178. data, err := ioutil.ReadAll(body)
  179. c.Assert(err, IsNil)
  180. c.Assert(string(data), Equals, objectValue)
  181. // List object
  182. lor, err := cs.creBucket.ListObjects()
  183. c.Assert(err, IsNil)
  184. c.Assert(len(lor.Objects), Equals, 1)
  185. err = cs.creBucket.DeleteObject(key)
  186. c.Assert(err, IsNil)
  187. }
  188. // Test put/get/list/delte object
  189. func (cs *OssCredentialBucketSuite) TestOwnerPaymentWithRequester(c *C) {
  190. // Set bucket is BucketOwner payer
  191. reqPayConf := RequestPaymentConfiguration{
  192. Payer: string(BucketOwner),
  193. }
  194. err := cs.client.SetBucketRequestPayment(credentialBucketName, reqPayConf)
  195. c.Assert(err, IsNil)
  196. key := objectNamePrefix + RandStr(8)
  197. objectValue := RandStr(18)
  198. // Put object
  199. err = cs.creBucket.PutObject(key, strings.NewReader(objectValue), RequestPayer(BucketOwner))
  200. c.Assert(err, IsNil)
  201. // Put object
  202. err = cs.creBucket.PutObject(key, strings.NewReader(objectValue), RequestPayer(Requester))
  203. c.Assert(err, IsNil)
  204. // Get object
  205. body, err := cs.creBucket.GetObject(key, RequestPayer(Requester))
  206. c.Assert(err, IsNil)
  207. defer body.Close()
  208. data, err := ioutil.ReadAll(body)
  209. c.Assert(err, IsNil)
  210. c.Assert(string(data), Equals, objectValue)
  211. // List object
  212. lor, err := cs.creBucket.ListObjects(RequestPayer(Requester))
  213. c.Assert(err, IsNil)
  214. c.Assert(len(lor.Objects), Equals, 1)
  215. err = cs.creBucket.DeleteObject(key, RequestPayer(Requester))
  216. c.Assert(err, IsNil)
  217. }
  218. // TestPutObjectFromFile
  219. func (cs *OssCredentialBucketSuite) TestPutObjectFromFile(c *C) {
  220. objectName := objectNamePrefix + RandStr(8)
  221. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  222. newFile := RandStr(8) + ".jpg"
  223. // Put
  224. err := cs.creBucket.PutObjectFromFile(objectName, localFile, RequestPayer(Requester))
  225. c.Assert(err, IsNil)
  226. // Check
  227. err = cs.creBucket.GetObjectToFile(objectName, newFile, RequestPayer(Requester))
  228. c.Assert(err, IsNil)
  229. eq, err := compareFiles(localFile, newFile)
  230. c.Assert(err, IsNil)
  231. c.Assert(eq, Equals, true)
  232. meta, err := cs.creBucket.GetObjectDetailedMeta(objectName, RequestPayer(Requester))
  233. c.Assert(err, IsNil)
  234. c.Assert(meta.Get("Content-Type"), Equals, "image/jpeg")
  235. acl, err := cs.creBucket.GetObjectACL(objectName, RequestPayer(Requester))
  236. c.Assert(err, IsNil)
  237. testLogger.Println("aclRes:", acl)
  238. c.Assert(acl.ACL, Equals, "default")
  239. err = cs.creBucket.DeleteObject(objectName, RequestPayer(Requester))
  240. c.Assert(err, IsNil)
  241. // Put with properties
  242. options := []Option{
  243. Expires(futureDate),
  244. ObjectACL(ACLPublicRead),
  245. Meta("myprop", "mypropval"),
  246. RequestPayer(Requester),
  247. }
  248. err = cs.creBucket.PutObjectFromFile(objectName, localFile, options...)
  249. c.Assert(err, IsNil)
  250. // Check
  251. err = cs.creBucket.GetObjectToFile(objectName, newFile, RequestPayer(Requester))
  252. c.Assert(err, IsNil)
  253. eq, err = compareFiles(localFile, newFile)
  254. c.Assert(err, IsNil)
  255. c.Assert(eq, Equals, true)
  256. acl, err = cs.creBucket.GetObjectACL(objectName, RequestPayer(Requester))
  257. c.Assert(err, IsNil)
  258. testLogger.Println("GetObjectACL:", acl)
  259. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  260. meta, err = cs.creBucket.GetObjectDetailedMeta(objectName, RequestPayer(Requester))
  261. c.Assert(err, IsNil)
  262. testLogger.Println("GetObjectDetailedMeta:", meta)
  263. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  264. err = cs.creBucket.DeleteObject(objectName, RequestPayer(Requester))
  265. c.Assert(err, IsNil)
  266. os.Remove(newFile)
  267. }
  268. // TestCopyObject
  269. func (cs *OssCredentialBucketSuite) TestCopyObject(c *C) {
  270. objectName := objectNamePrefix + RandStr(8)
  271. objectValue := RandStr(18)
  272. err := cs.creBucket.PutObject(objectName, strings.NewReader(objectValue),
  273. ACL(ACLPublicRead), Meta("my", "myprop"), RequestPayer(Requester))
  274. c.Assert(err, IsNil)
  275. // Copy
  276. var objectNameDest = objectName + "dest"
  277. _, err = cs.creBucket.CopyObject(objectName, objectNameDest, RequestPayer(Requester))
  278. c.Assert(err, IsNil)
  279. // Check
  280. lor, err := cs.creBucket.ListObjects(Prefix(objectName), RequestPayer(Requester))
  281. c.Assert(err, IsNil)
  282. testLogger.Println("objects:", lor.Objects)
  283. c.Assert(len(lor.Objects), Equals, 2)
  284. body, err := cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  285. c.Assert(err, IsNil)
  286. str, err := readBody(body)
  287. c.Assert(err, IsNil)
  288. c.Assert(str, Equals, objectValue)
  289. err = cs.creBucket.DeleteObject(objectNameDest, RequestPayer(Requester))
  290. c.Assert(err, IsNil)
  291. // Copy with constraints x-oss-copy-source-if-modified-since
  292. _, err = cs.creBucket.CopyObject(objectName, objectNameDest, CopySourceIfModifiedSince(futureDate), RequestPayer(Requester))
  293. c.Assert(err, NotNil)
  294. testLogger.Println("CopyObject:", err)
  295. // Copy with constraints x-oss-copy-source-if-unmodified-since
  296. _, err = cs.creBucket.CopyObject(objectName, objectNameDest, CopySourceIfUnmodifiedSince(futureDate), RequestPayer(Requester))
  297. c.Assert(err, IsNil)
  298. // Check
  299. lor, err = cs.creBucket.ListObjects(Prefix(objectName), RequestPayer(Requester))
  300. c.Assert(err, IsNil)
  301. testLogger.Println("objects:", lor.Objects)
  302. c.Assert(len(lor.Objects), Equals, 2)
  303. body, err = cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  304. c.Assert(err, IsNil)
  305. str, err = readBody(body)
  306. c.Assert(err, IsNil)
  307. c.Assert(str, Equals, objectValue)
  308. err = cs.creBucket.DeleteObject(objectNameDest, RequestPayer(Requester))
  309. c.Assert(err, IsNil)
  310. // Copy with constraints x-oss-copy-source-if-match
  311. meta, err := cs.creBucket.GetObjectDetailedMeta(objectName, RequestPayer(Requester))
  312. c.Assert(err, IsNil)
  313. testLogger.Println("GetObjectDetailedMeta:", meta)
  314. _, err = cs.creBucket.CopyObject(objectName, objectNameDest, CopySourceIfMatch(meta.Get("Etag")), RequestPayer(Requester))
  315. c.Assert(err, IsNil)
  316. // Check
  317. body, err = cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  318. c.Assert(err, IsNil)
  319. str, err = readBody(body)
  320. c.Assert(err, IsNil)
  321. c.Assert(str, Equals, objectValue)
  322. err = cs.creBucket.DeleteObject(objectNameDest, RequestPayer(Requester))
  323. c.Assert(err, IsNil)
  324. // Copy with constraints x-oss-copy-source-if-none-match
  325. _, err = cs.creBucket.CopyObject(objectName, objectNameDest, CopySourceIfNoneMatch(meta.Get("Etag")), RequestPayer(Requester))
  326. c.Assert(err, NotNil)
  327. // Copy with constraints x-oss-metadata-directive
  328. _, err = cs.creBucket.CopyObject(objectName, objectNameDest, Meta("my", "mydestprop"),
  329. MetadataDirective(MetaCopy), RequestPayer(Requester))
  330. c.Assert(err, IsNil)
  331. // Check
  332. body, err = cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  333. c.Assert(err, IsNil)
  334. str, err = readBody(body)
  335. c.Assert(err, IsNil)
  336. c.Assert(str, Equals, objectValue)
  337. destMeta, err := cs.creBucket.GetObjectDetailedMeta(objectNameDest, RequestPayer(Requester))
  338. c.Assert(err, IsNil)
  339. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  340. acl, err := cs.creBucket.GetObjectACL(objectNameDest, RequestPayer(Requester))
  341. c.Assert(err, IsNil)
  342. c.Assert(acl.ACL, Equals, "default")
  343. err = cs.creBucket.DeleteObject(objectNameDest, RequestPayer(Requester))
  344. c.Assert(err, IsNil)
  345. // Copy with constraints x-oss-metadata-directive and self defined dest object meta
  346. options := []Option{
  347. ObjectACL(ACLPublicReadWrite),
  348. Meta("my", "mydestprop"),
  349. MetadataDirective(MetaReplace),
  350. RequestPayer(Requester),
  351. }
  352. _, err = cs.creBucket.CopyObject(objectName, objectNameDest, options...)
  353. c.Assert(err, IsNil)
  354. // Check
  355. body, err = cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  356. c.Assert(err, IsNil)
  357. str, err = readBody(body)
  358. c.Assert(err, IsNil)
  359. c.Assert(str, Equals, objectValue)
  360. destMeta, err = cs.creBucket.GetObjectDetailedMeta(objectNameDest, RequestPayer(Requester))
  361. c.Assert(err, IsNil)
  362. c.Assert(destMeta.Get("X-Oss-Meta-My"), Equals, "mydestprop")
  363. acl, err = cs.creBucket.GetObjectACL(objectNameDest, RequestPayer(Requester))
  364. c.Assert(err, IsNil)
  365. c.Assert(acl.ACL, Equals, string(ACLPublicReadWrite))
  366. err = cs.creBucket.DeleteObject(objectNameDest, RequestPayer(Requester))
  367. c.Assert(err, IsNil)
  368. err = cs.creBucket.DeleteObject(objectName, RequestPayer(Requester))
  369. c.Assert(err, IsNil)
  370. }
  371. // TestCopyObjectToOrFrom
  372. func (cs *OssCredentialBucketSuite) TestCopyObjectToOrFrom(c *C) {
  373. objectName := objectNamePrefix + RandStr(8)
  374. objectValue := RandStr(18)
  375. sorBucketName := credentialBucketName + "-sor"
  376. objectNameDest := objectName + "-Dest"
  377. err := cs.client.CreateBucket(sorBucketName)
  378. c.Assert(err, IsNil)
  379. // Set ACL_PUBLIC_R
  380. err = cs.client.SetBucketACL(sorBucketName, ACLPublicRead)
  381. c.Assert(err, IsNil)
  382. sorBucket, err := cs.client.Bucket(sorBucketName)
  383. c.Assert(err, IsNil)
  384. err = sorBucket.PutObject(objectName, strings.NewReader(objectValue))
  385. c.Assert(err, IsNil)
  386. // Copy from
  387. _, err = cs.creBucket.CopyObjectFrom(sorBucketName, objectName, objectNameDest, RequestPayer(Requester))
  388. c.Assert(err, IsNil)
  389. // Check
  390. body, err := cs.creBucket.GetObject(objectNameDest, RequestPayer(Requester))
  391. c.Assert(err, IsNil)
  392. str, err := readBody(body)
  393. c.Assert(err, IsNil)
  394. c.Assert(str, Equals, objectValue)
  395. err = cs.creBucket.DeleteObject(objectNameDest, RequestPayer(Requester))
  396. c.Assert(err, IsNil)
  397. // Copy to
  398. _, err = sorBucket.CopyObjectTo(credentialBucketName, objectName, objectName)
  399. c.Assert(err, IsNil)
  400. // Check
  401. body, err = cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  402. c.Assert(err, IsNil)
  403. str, err = readBody(body)
  404. c.Assert(err, IsNil)
  405. c.Assert(str, Equals, objectValue)
  406. // Clean
  407. err = sorBucket.DeleteObject(objectName)
  408. c.Assert(err, IsNil)
  409. err = cs.creBucket.DeleteObject(objectName, RequestPayer(Requester))
  410. c.Assert(err, IsNil)
  411. err = cs.client.DeleteBucket(sorBucketName)
  412. c.Assert(err, IsNil)
  413. }
  414. // TestAppendObject
  415. func (cs *OssCredentialBucketSuite) TestAppendObject(c *C) {
  416. objectName := objectNamePrefix + RandStr(8)
  417. objectValue1 := RandStr(18)
  418. objectValue2 := RandStr(18)
  419. objectValue := objectValue1 + objectValue2
  420. var val = []byte(objectValue)
  421. var localFile = RandStr(8) + ".txt"
  422. var nextPos int64
  423. var midPos = 1 + rand.Intn(len(val)-1)
  424. var err = CreateFileAndWrite(localFile+"1", val[0:midPos])
  425. c.Assert(err, IsNil)
  426. err = CreateFileAndWrite(localFile+"2", val[midPos:])
  427. c.Assert(err, IsNil)
  428. // String append
  429. nextPos, err = cs.creBucket.AppendObject(objectName, strings.NewReader(objectValue1), nextPos, RequestPayer(Requester))
  430. c.Assert(err, IsNil)
  431. nextPos, err = cs.creBucket.AppendObject(objectName, strings.NewReader(objectValue2), nextPos, RequestPayer(Requester))
  432. c.Assert(err, IsNil)
  433. body, err := cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  434. c.Assert(err, IsNil)
  435. str, err := readBody(body)
  436. c.Assert(err, IsNil)
  437. c.Assert(str, Equals, objectValue)
  438. err = cs.creBucket.DeleteObject(objectName, RequestPayer(Requester))
  439. c.Assert(err, IsNil)
  440. // Byte append
  441. nextPos = 0
  442. nextPos, err = cs.creBucket.AppendObject(objectName, bytes.NewReader(val[0:midPos]), nextPos, RequestPayer(Requester))
  443. c.Assert(err, IsNil)
  444. nextPos, err = cs.creBucket.AppendObject(objectName, bytes.NewReader(val[midPos:]), nextPos, RequestPayer(Requester))
  445. c.Assert(err, IsNil)
  446. body, err = cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  447. c.Assert(err, IsNil)
  448. str, err = readBody(body)
  449. c.Assert(err, IsNil)
  450. c.Assert(str, Equals, objectValue)
  451. err = cs.creBucket.DeleteObject(objectName, RequestPayer(Requester))
  452. c.Assert(err, IsNil)
  453. // File append
  454. options := []Option{
  455. ObjectACL(ACLPublicReadWrite),
  456. Meta("my", "myprop"),
  457. RequestPayer(Requester),
  458. }
  459. fd, err := os.Open(localFile + "1")
  460. c.Assert(err, IsNil)
  461. defer fd.Close()
  462. nextPos = 0
  463. nextPos, err = cs.creBucket.AppendObject(objectName, fd, nextPos, options...)
  464. c.Assert(err, IsNil)
  465. meta, err := cs.creBucket.GetObjectDetailedMeta(objectName, RequestPayer(Requester))
  466. c.Assert(err, IsNil)
  467. testLogger.Println("GetObjectDetailedMeta:", meta, ",", nextPos)
  468. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Appendable")
  469. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  470. c.Assert(meta.Get("x-oss-Meta-Mine"), Equals, "")
  471. c.Assert(meta.Get("X-Oss-Next-Append-Position"), Equals, strconv.FormatInt(nextPos, 10))
  472. acl, err := cs.creBucket.GetObjectACL(objectName, RequestPayer(Requester))
  473. c.Assert(err, IsNil)
  474. testLogger.Println("GetObjectACL:", acl)
  475. c.Assert(acl.ACL, Equals, string(ACLPublicReadWrite))
  476. // Second append
  477. options = []Option{
  478. ObjectACL(ACLPublicRead),
  479. Meta("my", "myproptwo"),
  480. Meta("mine", "mypropmine"),
  481. RequestPayer(Requester),
  482. }
  483. fd, err = os.Open(localFile + "2")
  484. c.Assert(err, IsNil)
  485. defer fd.Close()
  486. nextPos, err = cs.creBucket.AppendObject(objectName, fd, nextPos, options...)
  487. c.Assert(err, IsNil)
  488. body, err = cs.creBucket.GetObject(objectName, RequestPayer(Requester))
  489. c.Assert(err, IsNil)
  490. str, err = readBody(body)
  491. c.Assert(err, IsNil)
  492. c.Assert(str, Equals, objectValue)
  493. meta, err = cs.creBucket.GetObjectDetailedMeta(objectName, RequestPayer(Requester))
  494. c.Assert(err, IsNil)
  495. testLogger.Println("GetObjectDetailedMeta xxx:", meta)
  496. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Appendable")
  497. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  498. c.Assert(meta.Get("x-Oss-Meta-Mine"), Equals, "")
  499. c.Assert(meta.Get("X-Oss-Next-Append-Position"), Equals, strconv.FormatInt(nextPos, 10))
  500. acl, err = cs.creBucket.GetObjectACL(objectName, RequestPayer(Requester))
  501. c.Assert(err, IsNil)
  502. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  503. err = cs.creBucket.DeleteObject(objectName, RequestPayer(Requester))
  504. c.Assert(err, IsNil)
  505. }