bucket_test.go 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  1. // Bucket test
  2. package oss
  3. import (
  4. "bytes"
  5. "encoding/base64"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "math/rand"
  11. "net/http"
  12. "os"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/baiyubin/aliyun-sts-go-sdk/sts"
  18. . "gopkg.in/check.v1"
  19. )
  20. type OssBucketSuite struct {
  21. client *Client
  22. bucket *Bucket
  23. archiveBucket *Bucket
  24. }
  25. var _ = Suite(&OssBucketSuite{})
  26. var (
  27. pastDate = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
  28. futureDate = time.Date(2049, time.January, 10, 23, 0, 0, 0, time.UTC)
  29. )
  30. // SetUpSuite runs once when the suite starts running.
  31. func (s *OssBucketSuite) SetUpSuite(c *C) {
  32. client, err := New(endpoint, accessID, accessKey)
  33. c.Assert(err, IsNil)
  34. s.client = client
  35. s.client.CreateBucket(bucketName)
  36. err = s.client.CreateBucket(archiveBucketName, StorageClass(StorageArchive))
  37. c.Assert(err, IsNil)
  38. time.Sleep(5 * time.Second)
  39. bucket, err := s.client.Bucket(bucketName)
  40. c.Assert(err, IsNil)
  41. s.bucket = bucket
  42. archiveBucket, err := s.client.Bucket(archiveBucketName)
  43. c.Assert(err, IsNil)
  44. s.archiveBucket = archiveBucket
  45. testLogger.Println("test bucket started")
  46. }
  47. // TearDownSuite runs before each test or benchmark starts running.
  48. func (s *OssBucketSuite) TearDownSuite(c *C) {
  49. for _, bucket := range []*Bucket{s.bucket, s.archiveBucket} {
  50. // Delete multipart
  51. lmu, err := bucket.ListMultipartUploads()
  52. c.Assert(err, IsNil)
  53. for _, upload := range lmu.Uploads {
  54. imur := InitiateMultipartUploadResult{Bucket: bucketName, Key: upload.Key, UploadID: upload.UploadID}
  55. err = bucket.AbortMultipartUpload(imur)
  56. c.Assert(err, IsNil)
  57. }
  58. // Delete objects
  59. lor, err := bucket.ListObjects()
  60. c.Assert(err, IsNil)
  61. for _, object := range lor.Objects {
  62. err = bucket.DeleteObject(object.Key)
  63. c.Assert(err, IsNil)
  64. }
  65. }
  66. testLogger.Println("test bucket completed")
  67. }
  68. // SetUpTest runs after each test or benchmark runs.
  69. func (s *OssBucketSuite) SetUpTest(c *C) {
  70. err := removeTempFiles("../oss", ".jpg")
  71. c.Assert(err, IsNil)
  72. }
  73. // TearDownTest runs once after all tests or benchmarks have finished running.
  74. func (s *OssBucketSuite) TearDownTest(c *C) {
  75. err := removeTempFiles("../oss", ".jpg")
  76. c.Assert(err, IsNil)
  77. err = removeTempFiles("../oss", ".txt")
  78. c.Assert(err, IsNil)
  79. err = removeTempFiles("../oss", ".temp")
  80. c.Assert(err, IsNil)
  81. err = removeTempFiles("../oss", ".txt1")
  82. c.Assert(err, IsNil)
  83. err = removeTempFiles("../oss", ".txt2")
  84. c.Assert(err, IsNil)
  85. }
  86. // TestPutObject
  87. func (s *OssBucketSuite) TestPutObject(c *C) {
  88. objectName := objectNamePrefix + "tpo"
  89. objectValue := "大江东去,浪淘尽,千古风流人物。 故垒西边,人道是、三国周郎赤壁。 乱石穿空,惊涛拍岸,卷起千堆雪。 江山如画,一时多少豪杰。" +
  90. "遥想公谨当年,小乔初嫁了,雄姿英发。 羽扇纶巾,谈笑间、樯橹灰飞烟灭。故国神游,多情应笑我,早生华发,人生如梦,一尊还酹江月。"
  91. // Put string
  92. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  93. c.Assert(err, IsNil)
  94. // Check
  95. body, err := s.bucket.GetObject(objectName)
  96. c.Assert(err, IsNil)
  97. str, err := readBody(body)
  98. c.Assert(err, IsNil)
  99. c.Assert(str, Equals, objectValue)
  100. acl, err := s.bucket.GetObjectACL(objectName)
  101. c.Assert(err, IsNil)
  102. testLogger.Println("aclRes:", acl)
  103. c.Assert(acl.ACL, Equals, "default")
  104. err = s.bucket.DeleteObject(objectName)
  105. c.Assert(err, IsNil)
  106. // Put bytes
  107. err = s.bucket.PutObject(objectName, bytes.NewReader([]byte(objectValue)))
  108. c.Assert(err, IsNil)
  109. // Check
  110. body, err = s.bucket.GetObject(objectName)
  111. c.Assert(err, IsNil)
  112. str, err = readBody(body)
  113. c.Assert(err, IsNil)
  114. c.Assert(str, Equals, objectValue)
  115. err = s.bucket.DeleteObject(objectName)
  116. c.Assert(err, IsNil)
  117. // Put file
  118. err = createFileAndWrite(objectName+".txt", []byte(objectValue))
  119. c.Assert(err, IsNil)
  120. fd, err := os.Open(objectName + ".txt")
  121. c.Assert(err, IsNil)
  122. err = s.bucket.PutObject(objectName, fd)
  123. c.Assert(err, IsNil)
  124. os.Remove(objectName + ".txt")
  125. // Check
  126. body, err = s.bucket.GetObject(objectName)
  127. c.Assert(err, IsNil)
  128. str, err = readBody(body)
  129. c.Assert(err, IsNil)
  130. c.Assert(str, Equals, objectValue)
  131. err = s.bucket.DeleteObject(objectName)
  132. c.Assert(err, IsNil)
  133. // Put with properties
  134. objectName = objectNamePrefix + "tpox"
  135. options := []Option{
  136. Expires(futureDate),
  137. ObjectACL(ACLPublicRead),
  138. Meta("myprop", "mypropval"),
  139. }
  140. err = s.bucket.PutObject(objectName, strings.NewReader(objectValue), options...)
  141. c.Assert(err, IsNil)
  142. // Check
  143. body, err = s.bucket.GetObject(objectName)
  144. c.Assert(err, IsNil)
  145. str, err = readBody(body)
  146. c.Assert(err, IsNil)
  147. c.Assert(str, Equals, objectValue)
  148. acl, err = s.bucket.GetObjectACL(objectName)
  149. c.Assert(err, IsNil)
  150. testLogger.Println("GetObjectACL:", acl)
  151. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  152. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  153. c.Assert(err, IsNil)
  154. testLogger.Println("GetObjectDetailedMeta:", meta)
  155. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  156. err = s.bucket.DeleteObject(objectName)
  157. c.Assert(err, IsNil)
  158. }
  159. func (s *OssBucketSuite) TestSignURL(c *C) {
  160. objectName := objectNamePrefix + randStr(5)
  161. objectValue := randStr(20)
  162. filePath := randLowStr(10)
  163. content := "复写object"
  164. createFile(filePath, content, c)
  165. notExistfilePath := randLowStr(10)
  166. os.Remove(notExistfilePath)
  167. // Sign URL for put
  168. str, err := s.bucket.SignURL(objectName, HTTPPut, 60)
  169. c.Assert(err, IsNil)
  170. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  171. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  172. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  173. // Error put object with URL
  174. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue), ContentType("image/tiff"))
  175. c.Assert(err, NotNil)
  176. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  177. err = s.bucket.PutObjectFromFileWithURL(str, filePath, ContentType("image/tiff"))
  178. c.Assert(err, NotNil)
  179. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  180. // Put object with URL
  181. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  182. c.Assert(err, IsNil)
  183. acl, err := s.bucket.GetObjectACL(objectName)
  184. c.Assert(err, IsNil)
  185. c.Assert(acl.ACL, Equals, "default")
  186. // Get object meta
  187. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  188. c.Assert(err, IsNil)
  189. c.Assert(meta.Get(HTTPHeaderContentType), Equals, "application/octet-stream")
  190. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "")
  191. // Sign URL for function GetObjectWithURL
  192. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  193. c.Assert(err, IsNil)
  194. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  195. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  196. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  197. // Get object with URL
  198. body, err := s.bucket.GetObjectWithURL(str)
  199. c.Assert(err, IsNil)
  200. str, err = readBody(body)
  201. c.Assert(err, IsNil)
  202. c.Assert(str, Equals, objectValue)
  203. // Sign URL for function PutObjectWithURL
  204. options := []Option{
  205. ObjectACL(ACLPublicRead),
  206. Meta("myprop", "mypropval"),
  207. ContentType("image/tiff"),
  208. ResponseContentEncoding("deflate"),
  209. }
  210. str, err = s.bucket.SignURL(objectName, HTTPPut, 60, options...)
  211. c.Assert(err, IsNil)
  212. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  213. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  214. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  215. // Put object with URL from file
  216. // Without option, error
  217. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  218. c.Assert(err, NotNil)
  219. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  220. err = s.bucket.PutObjectFromFileWithURL(str, filePath)
  221. c.Assert(err, NotNil)
  222. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  223. // With option, error file
  224. err = s.bucket.PutObjectFromFileWithURL(str, notExistfilePath, options...)
  225. c.Assert(err, NotNil)
  226. // With option
  227. err = s.bucket.PutObjectFromFileWithURL(str, filePath, options...)
  228. c.Assert(err, IsNil)
  229. // Get object meta
  230. meta, err = s.bucket.GetObjectDetailedMeta(objectName)
  231. c.Assert(err, IsNil)
  232. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  233. c.Assert(meta.Get(HTTPHeaderContentType), Equals, "image/tiff")
  234. acl, err = s.bucket.GetObjectACL(objectName)
  235. c.Assert(err, IsNil)
  236. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  237. // Sign URL for function GetObjectToFileWithURL
  238. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  239. c.Assert(err, IsNil)
  240. // Get object to file with URL
  241. newFile := randStr(10)
  242. err = s.bucket.GetObjectToFileWithURL(str, newFile)
  243. c.Assert(err, IsNil)
  244. eq, err := compareFiles(filePath, newFile)
  245. c.Assert(err, IsNil)
  246. c.Assert(eq, Equals, true)
  247. os.Remove(newFile)
  248. // Get object to file error
  249. err = s.bucket.GetObjectToFileWithURL(str, newFile, options...)
  250. c.Assert(err, NotNil)
  251. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  252. _, err = os.Stat(newFile)
  253. c.Assert(err, NotNil)
  254. // Get object error
  255. body, err = s.bucket.GetObjectWithURL(str, options...)
  256. c.Assert(err, NotNil)
  257. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  258. c.Assert(body, IsNil)
  259. // Sign URL for function GetObjectToFileWithURL
  260. options = []Option{
  261. Expires(futureDate),
  262. ObjectACL(ACLPublicRead),
  263. Meta("myprop", "mypropval"),
  264. ContentType("image/tiff"),
  265. ResponseContentEncoding("deflate"),
  266. }
  267. str, err = s.bucket.SignURL(objectName, HTTPGet, 60, options...)
  268. c.Assert(err, IsNil)
  269. // Get object to file with URL and options
  270. err = s.bucket.GetObjectToFileWithURL(str, newFile, options...)
  271. c.Assert(err, IsNil)
  272. eq, err = compareFiles(filePath, newFile)
  273. c.Assert(err, IsNil)
  274. c.Assert(eq, Equals, true)
  275. os.Remove(newFile)
  276. // Get object to file error
  277. err = s.bucket.GetObjectToFileWithURL(str, newFile)
  278. c.Assert(err, NotNil)
  279. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  280. _, err = os.Stat(newFile)
  281. c.Assert(err, NotNil)
  282. // Get object error
  283. body, err = s.bucket.GetObjectWithURL(str)
  284. c.Assert(err, NotNil)
  285. c.Assert(err.(ServiceError).Code, Equals, "SignatureDoesNotMatch")
  286. c.Assert(body, IsNil)
  287. err = s.bucket.PutObjectFromFile(objectName, "../sample/The Go Programming Language.html")
  288. c.Assert(err, IsNil)
  289. str, err = s.bucket.SignURL(objectName, HTTPGet, 3600, AcceptEncoding("gzip"))
  290. c.Assert(err, IsNil)
  291. s.bucket.GetObjectToFileWithURL(str, newFile)
  292. c.Assert(err, IsNil)
  293. os.Remove(filePath)
  294. os.Remove(newFile)
  295. // Sign URL error
  296. str, err = s.bucket.SignURL(objectName, HTTPGet, -1)
  297. c.Assert(err, NotNil)
  298. err = s.bucket.DeleteObject(objectName)
  299. c.Assert(err, IsNil)
  300. // Invalid URL parse
  301. str = randStr(20)
  302. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  303. c.Assert(err, NotNil)
  304. err = s.bucket.GetObjectToFileWithURL(str, newFile)
  305. c.Assert(err, NotNil)
  306. }
  307. func (s *OssBucketSuite) TestSignURLWithEscapedKey(c *C) {
  308. // Key with '/'
  309. objectName := "zyimg/86/e8/653b5dc97bb0022051a84c632bc4"
  310. objectValue := "弃我去者,昨日之日不可留;乱我心者,今日之日多烦忧。长风万里送秋雁,对此可以酣高楼。蓬莱文章建安骨,中间小谢又清发。" +
  311. "俱怀逸兴壮思飞,欲上青天揽明月。抽刀断水水更流,举杯销愁愁更愁。人生在世不称意,明朝散发弄扁舟。"
  312. // Sign URL for function PutObjectWithURL
  313. str, err := s.bucket.SignURL(objectName, HTTPPut, 60)
  314. c.Assert(err, IsNil)
  315. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  316. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  317. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  318. // Put object with URL
  319. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  320. c.Assert(err, IsNil)
  321. // Sign URL for function GetObjectWithURL
  322. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  323. c.Assert(err, IsNil)
  324. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  325. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  326. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  327. // Get object with URL
  328. body, err := s.bucket.GetObjectWithURL(str)
  329. c.Assert(err, IsNil)
  330. str, err = readBody(body)
  331. c.Assert(err, IsNil)
  332. c.Assert(str, Equals, objectValue)
  333. // Key with escaped chars
  334. objectName = "<>[]()`?.,!@#$%^&'/*-_=+~:;"
  335. // Sign URL for funciton PutObjectWithURL
  336. str, err = s.bucket.SignURL(objectName, HTTPPut, 60)
  337. c.Assert(err, IsNil)
  338. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  339. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  340. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  341. // Put object with URL
  342. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  343. c.Assert(err, IsNil)
  344. // Sign URL for function GetObjectWithURL
  345. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  346. c.Assert(err, IsNil)
  347. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  348. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  349. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  350. // Get object with URL
  351. body, err = s.bucket.GetObjectWithURL(str)
  352. c.Assert(err, IsNil)
  353. str, err = readBody(body)
  354. c.Assert(err, IsNil)
  355. c.Assert(str, Equals, objectValue)
  356. // Key with Chinese chars
  357. objectName = "风吹柳花满店香,吴姬压酒劝客尝。金陵子弟来相送,欲行不行各尽觞。请君试问东流水,别意与之谁短长。"
  358. // Sign URL for function PutObjectWithURL
  359. str, err = s.bucket.SignURL(objectName, HTTPPut, 60)
  360. c.Assert(err, IsNil)
  361. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  362. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  363. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  364. // Put object with URL
  365. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  366. c.Assert(err, IsNil)
  367. // Sign URL for get function GetObjectWithURL
  368. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  369. c.Assert(err, IsNil)
  370. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  371. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  372. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  373. // Get object with URL
  374. body, err = s.bucket.GetObjectWithURL(str)
  375. c.Assert(err, IsNil)
  376. str, err = readBody(body)
  377. c.Assert(err, IsNil)
  378. c.Assert(str, Equals, objectValue)
  379. // Key
  380. objectName = "test/此情无计可消除/才下眉头/却上 心头/。,;:‘’“”?()『』【】《》!@#¥%……&×/test+ =-_*&^%$#@!`~[]{}()<>|\\/?.,;.txt"
  381. // Sign URL for function PutObjectWithURL
  382. str, err = s.bucket.SignURL(objectName, HTTPPut, 60)
  383. c.Assert(err, IsNil)
  384. // Put object with URL
  385. err = s.bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  386. c.Assert(err, IsNil)
  387. // Sign URL for function GetObjectWithURL
  388. str, err = s.bucket.SignURL(objectName, HTTPGet, 60)
  389. c.Assert(err, IsNil)
  390. // Get object with URL
  391. body, err = s.bucket.GetObjectWithURL(str)
  392. c.Assert(err, IsNil)
  393. str, err = readBody(body)
  394. c.Assert(err, IsNil)
  395. c.Assert(str, Equals, objectValue)
  396. // Put object
  397. err = s.bucket.PutObject(objectName, bytes.NewReader([]byte(objectValue)))
  398. c.Assert(err, IsNil)
  399. // Get object
  400. body, err = s.bucket.GetObject(objectName)
  401. c.Assert(err, IsNil)
  402. str, err = readBody(body)
  403. c.Assert(err, IsNil)
  404. c.Assert(str, Equals, objectValue)
  405. // Delete object
  406. err = s.bucket.DeleteObject(objectName)
  407. c.Assert(err, IsNil)
  408. }
  409. func (s *OssBucketSuite) TestSignURLWithEscapedKeyAndPorxy(c *C) {
  410. // Key with '/'
  411. objectName := "zyimg/86/e8/653b5dc97bb0022051a84c632bc4"
  412. objectValue := "弃我去者,昨日之日不可留;乱我心者,今日之日多烦忧。长风万里送秋雁,对此可以酣高楼。蓬莱文章建安骨,中间小谢又清发。" +
  413. "俱怀逸兴壮思飞,欲上青天揽明月。抽刀断水水更流,举杯销愁愁更愁。人生在世不称意,明朝散发弄扁舟。"
  414. client, err := New(endpoint, accessID, accessKey, AuthProxy(proxyHost, proxyUser, proxyPasswd))
  415. bucket, err := client.Bucket(bucketName)
  416. // Sign URL for put
  417. str, err := bucket.SignURL(objectName, HTTPPut, 60)
  418. c.Assert(err, IsNil)
  419. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  420. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  421. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  422. // Put object with URL
  423. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  424. c.Assert(err, IsNil)
  425. // Sign URL for function GetObjectWithURL
  426. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  427. c.Assert(err, IsNil)
  428. c.Assert(strings.Contains(str, HTTPParamExpires+"="), Equals, true)
  429. c.Assert(strings.Contains(str, HTTPParamAccessKeyID+"="), Equals, true)
  430. c.Assert(strings.Contains(str, HTTPParamSignature+"="), Equals, true)
  431. // Get object with URL
  432. body, err := bucket.GetObjectWithURL(str)
  433. c.Assert(err, IsNil)
  434. str, err = readBody(body)
  435. c.Assert(err, IsNil)
  436. c.Assert(str, Equals, objectValue)
  437. // Key with Chinese chars
  438. objectName = "test/此情无计可消除/才下眉头/却上 心头/。,;:‘’“”?()『』【】《》!@#¥%……&×/test+ =-_*&^%$#@!`~[]{}()<>|\\/?.,;.txt"
  439. // Sign URL for function PutObjectWithURL
  440. str, err = bucket.SignURL(objectName, HTTPPut, 60)
  441. c.Assert(err, IsNil)
  442. // Put object with URL
  443. err = bucket.PutObjectWithURL(str, strings.NewReader(objectValue))
  444. c.Assert(err, IsNil)
  445. // Sign URL for function GetObjectWithURL
  446. str, err = bucket.SignURL(objectName, HTTPGet, 60)
  447. c.Assert(err, IsNil)
  448. // Get object with URL
  449. body, err = bucket.GetObjectWithURL(str)
  450. c.Assert(err, IsNil)
  451. str, err = readBody(body)
  452. c.Assert(err, IsNil)
  453. c.Assert(str, Equals, objectValue)
  454. // Put object
  455. err = bucket.PutObject(objectName, bytes.NewReader([]byte(objectValue)))
  456. c.Assert(err, IsNil)
  457. // Get object
  458. body, err = bucket.GetObject(objectName)
  459. c.Assert(err, IsNil)
  460. str, err = readBody(body)
  461. c.Assert(err, IsNil)
  462. c.Assert(str, Equals, objectValue)
  463. // Delete object
  464. err = bucket.DeleteObject(objectName)
  465. c.Assert(err, IsNil)
  466. }
  467. // TestPutObjectType
  468. func (s *OssBucketSuite) TestPutObjectType(c *C) {
  469. objectName := objectNamePrefix + "tptt"
  470. objectValue := "乱石穿空,惊涛拍岸,卷起千堆雪。 江山如画,一时多少豪杰。"
  471. // Put
  472. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  473. c.Assert(err, IsNil)
  474. // Check
  475. time.Sleep(time.Second)
  476. body, err := s.bucket.GetObject(objectName)
  477. c.Assert(err, IsNil)
  478. str, err := readBody(body)
  479. c.Assert(err, IsNil)
  480. c.Assert(str, Equals, objectValue)
  481. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  482. c.Assert(err, IsNil)
  483. c.Assert(meta.Get("Content-Type"), Equals, "application/octet-stream")
  484. err = s.bucket.DeleteObject(objectName)
  485. c.Assert(err, IsNil)
  486. // Put
  487. err = s.bucket.PutObject(objectName+".txt", strings.NewReader(objectValue))
  488. c.Assert(err, IsNil)
  489. meta, err = s.bucket.GetObjectDetailedMeta(objectName + ".txt")
  490. c.Assert(err, IsNil)
  491. c.Assert(meta.Get("Content-Type"), Equals, "text/plain; charset=utf-8")
  492. err = s.bucket.DeleteObject(objectName + ".txt")
  493. c.Assert(err, IsNil)
  494. // Put
  495. err = s.bucket.PutObject(objectName+".apk", strings.NewReader(objectValue))
  496. c.Assert(err, IsNil)
  497. meta, err = s.bucket.GetObjectDetailedMeta(objectName + ".apk")
  498. c.Assert(err, IsNil)
  499. c.Assert(meta.Get("Content-Type"), Equals, "application/vnd.android.package-archive")
  500. err = s.bucket.DeleteObject(objectName + ".txt")
  501. c.Assert(err, IsNil)
  502. }
  503. // TestPutObject
  504. func (s *OssBucketSuite) TestPutObjectKeyChars(c *C) {
  505. objectName := objectNamePrefix + "tpokc"
  506. objectValue := "白日依山尽,黄河入海流。欲穷千里目,更上一层楼。"
  507. // Put
  508. objectKey := objectName + "十步杀一人,千里不留行。事了拂衣去,深藏身与名"
  509. err := s.bucket.PutObject(objectKey, strings.NewReader(objectValue))
  510. c.Assert(err, IsNil)
  511. // Check
  512. body, err := s.bucket.GetObject(objectKey)
  513. c.Assert(err, IsNil)
  514. str, err := readBody(body)
  515. c.Assert(err, IsNil)
  516. c.Assert(str, Equals, objectValue)
  517. err = s.bucket.DeleteObject(objectKey)
  518. c.Assert(err, IsNil)
  519. // Put
  520. objectKey = objectName + "ごきげん如何ですかおれの顔をよく拝んでおけ"
  521. err = s.bucket.PutObject(objectKey, strings.NewReader(objectValue))
  522. c.Assert(err, IsNil)
  523. // Check
  524. body, err = s.bucket.GetObject(objectKey)
  525. c.Assert(err, IsNil)
  526. str, err = readBody(body)
  527. c.Assert(err, IsNil)
  528. c.Assert(str, Equals, objectValue)
  529. err = s.bucket.DeleteObject(objectKey)
  530. c.Assert(err, IsNil)
  531. // Put
  532. objectKey = objectName + "~!@#$%^&*()_-+=|\\[]{}<>,./?"
  533. err = s.bucket.PutObject(objectKey, strings.NewReader(objectValue))
  534. c.Assert(err, IsNil)
  535. // Check
  536. body, err = s.bucket.GetObject(objectKey)
  537. c.Assert(err, IsNil)
  538. str, err = readBody(body)
  539. c.Assert(err, IsNil)
  540. c.Assert(str, Equals, objectValue)
  541. err = s.bucket.DeleteObject(objectKey)
  542. c.Assert(err, IsNil)
  543. // Put
  544. objectKey = "go/中国 日本 +-#&=*"
  545. err = s.bucket.PutObject(objectKey, strings.NewReader(objectValue))
  546. c.Assert(err, IsNil)
  547. // Check
  548. body, err = s.bucket.GetObject(objectKey)
  549. c.Assert(err, IsNil)
  550. str, err = readBody(body)
  551. c.Assert(err, IsNil)
  552. c.Assert(str, Equals, objectValue)
  553. err = s.bucket.DeleteObject(objectKey)
  554. c.Assert(err, IsNil)
  555. }
  556. // TestPutObjectNegative
  557. func (s *OssBucketSuite) TestPutObjectNegative(c *C) {
  558. objectName := objectNamePrefix + "tpon"
  559. objectValue := "大江东去,浪淘尽,千古风流人物。 "
  560. // Put
  561. objectName = objectNamePrefix + "tpox"
  562. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue),
  563. Meta("meta-my", "myprop"))
  564. c.Assert(err, IsNil)
  565. // Check meta
  566. body, err := s.bucket.GetObject(objectName)
  567. c.Assert(err, IsNil)
  568. str, err := readBody(body)
  569. c.Assert(err, IsNil)
  570. c.Assert(str, Equals, objectValue)
  571. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  572. c.Assert(err, IsNil)
  573. c.Assert(meta.Get("X-Oss-Meta-My"), Not(Equals), "myprop")
  574. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "")
  575. err = s.bucket.DeleteObject(objectName)
  576. c.Assert(err, IsNil)
  577. // Invalid option
  578. err = s.bucket.PutObject(objectName, strings.NewReader(objectValue),
  579. IfModifiedSince(pastDate))
  580. c.Assert(err, NotNil)
  581. err = s.bucket.PutObjectFromFile(objectName, "bucket.go", IfModifiedSince(pastDate))
  582. c.Assert(err, NotNil)
  583. err = s.bucket.PutObjectFromFile(objectName, "/tmp/xxx")
  584. c.Assert(err, NotNil)
  585. }
  586. // TestPutObjectFromFile
  587. func (s *OssBucketSuite) TestPutObjectFromFile(c *C) {
  588. objectName := objectNamePrefix + "tpoff"
  589. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  590. newFile := "newpic11.jpg"
  591. // Put
  592. err := s.bucket.PutObjectFromFile(objectName, localFile)
  593. c.Assert(err, IsNil)
  594. // Check
  595. err = s.bucket.GetObjectToFile(objectName, newFile)
  596. c.Assert(err, IsNil)
  597. eq, err := compareFiles(localFile, newFile)
  598. c.Assert(err, IsNil)
  599. c.Assert(eq, Equals, true)
  600. acl, err := s.bucket.GetObjectACL(objectName)
  601. c.Assert(err, IsNil)
  602. testLogger.Println("aclRes:", acl)
  603. c.Assert(acl.ACL, Equals, "default")
  604. err = s.bucket.DeleteObject(objectName)
  605. c.Assert(err, IsNil)
  606. // Put with properties
  607. options := []Option{
  608. Expires(futureDate),
  609. ObjectACL(ACLPublicRead),
  610. Meta("myprop", "mypropval"),
  611. }
  612. os.Remove(newFile)
  613. err = s.bucket.PutObjectFromFile(objectName, localFile, options...)
  614. c.Assert(err, IsNil)
  615. // Check
  616. err = s.bucket.GetObjectToFile(objectName, newFile)
  617. c.Assert(err, IsNil)
  618. eq, err = compareFiles(localFile, newFile)
  619. c.Assert(err, IsNil)
  620. c.Assert(eq, Equals, true)
  621. acl, err = s.bucket.GetObjectACL(objectName)
  622. c.Assert(err, IsNil)
  623. testLogger.Println("GetObjectACL:", acl)
  624. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  625. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  626. c.Assert(err, IsNil)
  627. testLogger.Println("GetObjectDetailedMeta:", meta)
  628. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  629. err = s.bucket.DeleteObject(objectName)
  630. c.Assert(err, IsNil)
  631. os.Remove(newFile)
  632. }
  633. // TestPutObjectFromFile
  634. func (s *OssBucketSuite) TestPutObjectFromFileType(c *C) {
  635. objectName := objectNamePrefix + "tpoffwm"
  636. localFile := "../sample/BingWallpaper-2015-11-07.jpg"
  637. newFile := "newpic11.jpg"
  638. // Put
  639. err := s.bucket.PutObjectFromFile(objectName, localFile)
  640. c.Assert(err, IsNil)
  641. // Check
  642. err = s.bucket.GetObjectToFile(objectName, newFile)
  643. c.Assert(err, IsNil)
  644. eq, err := compareFiles(localFile, newFile)
  645. c.Assert(err, IsNil)
  646. c.Assert(eq, Equals, true)
  647. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  648. c.Assert(err, IsNil)
  649. c.Assert(meta.Get("Content-Type"), Equals, "image/jpeg")
  650. err = s.bucket.DeleteObject(objectName)
  651. c.Assert(err, IsNil)
  652. os.Remove(newFile)
  653. }
  654. // TestGetObject
  655. func (s *OssBucketSuite) TestGetObject(c *C) {
  656. objectName := objectNamePrefix + "tgo"
  657. objectValue := "长忆观潮,满郭人争江上望。来疑沧海尽成空,万面鼓声中。弄潮儿向涛头立,手把红旗旗不湿。别来几向梦中看,梦觉尚心寒。"
  658. // Put
  659. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  660. c.Assert(err, IsNil)
  661. // Check
  662. body, err := s.bucket.GetObject(objectName)
  663. c.Assert(err, IsNil)
  664. data, err := ioutil.ReadAll(body)
  665. body.Close()
  666. str := string(data)
  667. c.Assert(str, Equals, objectValue)
  668. testLogger.Println("GetObjec:", str)
  669. // Range
  670. var subObjectValue = string(([]byte(objectValue))[15:36])
  671. body, err = s.bucket.GetObject(objectName, Range(15, 35))
  672. c.Assert(err, IsNil)
  673. data, err = ioutil.ReadAll(body)
  674. body.Close()
  675. str = string(data)
  676. c.Assert(str, Equals, subObjectValue)
  677. testLogger.Println("GetObject:", str, ",", subObjectValue)
  678. // If-Modified-Since
  679. _, err = s.bucket.GetObject(objectName, IfModifiedSince(futureDate))
  680. c.Assert(err, NotNil)
  681. // If-Unmodified-Since
  682. body, err = s.bucket.GetObject(objectName, IfUnmodifiedSince(futureDate))
  683. c.Assert(err, IsNil)
  684. data, err = ioutil.ReadAll(body)
  685. body.Close()
  686. c.Assert(string(data), Equals, objectValue)
  687. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  688. c.Assert(err, IsNil)
  689. // If-Match
  690. body, err = s.bucket.GetObject(objectName, IfMatch(meta.Get("Etag")))
  691. c.Assert(err, IsNil)
  692. data, err = ioutil.ReadAll(body)
  693. body.Close()
  694. c.Assert(string(data), Equals, objectValue)
  695. // If-None-Match
  696. _, err = s.bucket.GetObject(objectName, IfNoneMatch(meta.Get("Etag")))
  697. c.Assert(err, NotNil)
  698. // process
  699. err = s.bucket.PutObjectFromFile(objectName, "../sample/BingWallpaper-2015-11-07.jpg")
  700. c.Assert(err, IsNil)
  701. _, err = s.bucket.GetObject(objectName, Process("image/format,png"))
  702. c.Assert(err, IsNil)
  703. err = s.bucket.DeleteObject(objectName)
  704. c.Assert(err, IsNil)
  705. }
  706. // TestGetObjectNegative
  707. func (s *OssBucketSuite) TestGetObjectToWriterNegative(c *C) {
  708. objectName := objectNamePrefix + "tgotwn"
  709. objectValue := "长忆观潮,满郭人争江上望。"
  710. // Object not exist
  711. _, err := s.bucket.GetObject("NotExist")
  712. c.Assert(err, NotNil)
  713. // Constraint invalid
  714. err = s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  715. c.Assert(err, IsNil)
  716. // Out of range
  717. _, err = s.bucket.GetObject(objectName, Range(15, 1000))
  718. c.Assert(err, IsNil)
  719. // Not exist
  720. err = s.bucket.GetObjectToFile(objectName, "/root/123abc9874")
  721. c.Assert(err, NotNil)
  722. // Invalid option
  723. _, err = s.bucket.GetObject(objectName, ACL(ACLPublicRead))
  724. c.Assert(err, IsNil)
  725. err = s.bucket.GetObjectToFile(objectName, "newpic15.jpg", ACL(ACLPublicRead))
  726. c.Assert(err, IsNil)
  727. err = s.bucket.DeleteObject(objectName)
  728. c.Assert(err, IsNil)
  729. }
  730. // TestGetObjectToFile
  731. func (s *OssBucketSuite) TestGetObjectToFile(c *C) {
  732. objectName := objectNamePrefix + "tgotf"
  733. objectValue := "江南好,风景旧曾谙;日出江花红胜火,春来江水绿如蓝。能不忆江南?江南忆,最忆是杭州;山寺月中寻桂子,郡亭枕上看潮头。何日更重游!"
  734. newFile := "newpic15.jpg"
  735. // Put
  736. var val = []byte(objectValue)
  737. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  738. c.Assert(err, IsNil)
  739. // Check
  740. err = s.bucket.GetObjectToFile(objectName, newFile)
  741. c.Assert(err, IsNil)
  742. eq, err := compareFileData(newFile, val)
  743. c.Assert(err, IsNil)
  744. c.Assert(eq, Equals, true)
  745. os.Remove(newFile)
  746. // Range
  747. err = s.bucket.GetObjectToFile(objectName, newFile, Range(15, 35))
  748. c.Assert(err, IsNil)
  749. eq, err = compareFileData(newFile, val[15:36])
  750. c.Assert(err, IsNil)
  751. c.Assert(eq, Equals, true)
  752. os.Remove(newFile)
  753. err = s.bucket.GetObjectToFile(objectName, newFile, NormalizedRange("15-35"))
  754. c.Assert(err, IsNil)
  755. eq, err = compareFileData(newFile, val[15:36])
  756. c.Assert(err, IsNil)
  757. c.Assert(eq, Equals, true)
  758. os.Remove(newFile)
  759. err = s.bucket.GetObjectToFile(objectName, newFile, NormalizedRange("15-"))
  760. c.Assert(err, IsNil)
  761. eq, err = compareFileData(newFile, val[15:])
  762. c.Assert(err, IsNil)
  763. c.Assert(eq, Equals, true)
  764. os.Remove(newFile)
  765. err = s.bucket.GetObjectToFile(objectName, newFile, NormalizedRange("-10"))
  766. c.Assert(err, IsNil)
  767. eq, err = compareFileData(newFile, val[(len(val)-10):len(val)])
  768. c.Assert(err, IsNil)
  769. c.Assert(eq, Equals, true)
  770. os.Remove(newFile)
  771. // If-Modified-Since
  772. err = s.bucket.GetObjectToFile(objectName, newFile, IfModifiedSince(futureDate))
  773. c.Assert(err, NotNil)
  774. // If-Unmodified-Since
  775. err = s.bucket.GetObjectToFile(objectName, newFile, IfUnmodifiedSince(futureDate))
  776. c.Assert(err, IsNil)
  777. eq, err = compareFileData(newFile, val)
  778. c.Assert(err, IsNil)
  779. c.Assert(eq, Equals, true)
  780. os.Remove(newFile)
  781. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  782. c.Assert(err, IsNil)
  783. testLogger.Println("GetObjectDetailedMeta:", meta)
  784. // If-Match
  785. err = s.bucket.GetObjectToFile(objectName, newFile, IfMatch(meta.Get("Etag")))
  786. c.Assert(err, IsNil)
  787. eq, err = compareFileData(newFile, val)
  788. c.Assert(err, IsNil)
  789. c.Assert(eq, Equals, true)
  790. // If-None-Match
  791. err = s.bucket.GetObjectToFile(objectName, newFile, IfNoneMatch(meta.Get("Etag")))
  792. c.Assert(err, NotNil)
  793. // Accept-Encoding:gzip
  794. err = s.bucket.PutObjectFromFile(objectName, "../sample/The Go Programming Language.html")
  795. c.Assert(err, IsNil)
  796. err = s.bucket.GetObjectToFile(objectName, newFile, AcceptEncoding("gzip"))
  797. c.Assert(err, IsNil)
  798. os.Remove(newFile)
  799. err = s.bucket.DeleteObject(objectName)
  800. c.Assert(err, IsNil)
  801. }
  802. // TestListObjects
  803. func (s *OssBucketSuite) TestListObjects(c *C) {
  804. objectName := objectNamePrefix + "tlo"
  805. // List empty bucket
  806. lor, err := s.bucket.ListObjects()
  807. c.Assert(err, IsNil)
  808. left := len(lor.Objects)
  809. // Put three objects
  810. err = s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  811. c.Assert(err, IsNil)
  812. err = s.bucket.PutObject(objectName+"2", strings.NewReader(""))
  813. c.Assert(err, IsNil)
  814. err = s.bucket.PutObject(objectName+"3", strings.NewReader(""))
  815. c.Assert(err, IsNil)
  816. // List
  817. lor, err = s.bucket.ListObjects()
  818. c.Assert(err, IsNil)
  819. c.Assert(len(lor.Objects), Equals, left+3)
  820. // List with prefix
  821. lor, err = s.bucket.ListObjects(Prefix(objectName + "2"))
  822. c.Assert(err, IsNil)
  823. c.Assert(len(lor.Objects), Equals, 1)
  824. lor, err = s.bucket.ListObjects(Prefix(objectName + "22"))
  825. c.Assert(err, IsNil)
  826. c.Assert(len(lor.Objects), Equals, 0)
  827. // List with max keys
  828. lor, err = s.bucket.ListObjects(Prefix(objectName), MaxKeys(2))
  829. c.Assert(err, IsNil)
  830. c.Assert(len(lor.Objects), Equals, 2)
  831. // List with marker
  832. lor, err = s.bucket.ListObjects(Marker(objectName+"1"), MaxKeys(1))
  833. c.Assert(err, IsNil)
  834. c.Assert(len(lor.Objects), Equals, 1)
  835. err = s.bucket.DeleteObject(objectName + "1")
  836. c.Assert(err, IsNil)
  837. err = s.bucket.DeleteObject(objectName + "2")
  838. c.Assert(err, IsNil)
  839. err = s.bucket.DeleteObject(objectName + "3")
  840. c.Assert(err, IsNil)
  841. }
  842. // TestListObjects
  843. func (s *OssBucketSuite) TestListObjectsEncodingType(c *C) {
  844. objectName := objectNamePrefix + "床前明月光,疑是地上霜。举头望明月,低头思故乡。" + "tloet"
  845. for i := 0; i < 10; i++ {
  846. err := s.bucket.PutObject(objectName+strconv.Itoa(i), strings.NewReader(""))
  847. c.Assert(err, IsNil)
  848. }
  849. lor, err := s.bucket.ListObjects(Prefix(objectNamePrefix + "床前明月光,"))
  850. c.Assert(err, IsNil)
  851. c.Assert(len(lor.Objects), Equals, 10)
  852. lor, err = s.bucket.ListObjects(Prefix(objectNamePrefix + "床前明月光,"))
  853. c.Assert(err, IsNil)
  854. c.Assert(len(lor.Objects), Equals, 10)
  855. lor, err = s.bucket.ListObjects(Marker(objectNamePrefix + "床前明月光,疑是地上霜。举头望明月,低头思故乡。"))
  856. c.Assert(err, IsNil)
  857. c.Assert(len(lor.Objects), Equals, 10)
  858. lor, err = s.bucket.ListObjects(Prefix(objectNamePrefix + "床前明月光"))
  859. c.Assert(err, IsNil)
  860. for i, obj := range lor.Objects {
  861. c.Assert(obj.Key, Equals, objectNamePrefix+"床前明月光,疑是地上霜。举头望明月,低头思故乡。tloet"+strconv.Itoa(i))
  862. }
  863. for i := 0; i < 10; i++ {
  864. err = s.bucket.DeleteObject(objectName + strconv.Itoa(i))
  865. c.Assert(err, IsNil)
  866. }
  867. // Special characters
  868. objectName = "go go ` ~ ! @ # $ % ^ & * () - _ + =[] {} \\ | < > , . ? / 0"
  869. err = s.bucket.PutObject(objectName, strings.NewReader("明月几时有,把酒问青天"))
  870. c.Assert(err, IsNil)
  871. lor, err = s.bucket.ListObjects(Prefix(objectName))
  872. c.Assert(err, IsNil)
  873. c.Assert(len(lor.Objects), Equals, 1)
  874. err = s.bucket.DeleteObject(objectName)
  875. c.Assert(err, IsNil)
  876. objectName = "go/中国 日本 +-#&=*"
  877. err = s.bucket.PutObject(objectName, strings.NewReader("明月几时有,把酒问青天"))
  878. c.Assert(err, IsNil)
  879. lor, err = s.bucket.ListObjects(Prefix(objectName))
  880. c.Assert(err, IsNil)
  881. c.Assert(len(lor.Objects), Equals, 1)
  882. err = s.bucket.DeleteObject(objectName)
  883. c.Assert(err, IsNil)
  884. }
  885. // TestIsBucketExist
  886. func (s *OssBucketSuite) TestIsObjectExist(c *C) {
  887. objectName := objectNamePrefix + "tibe"
  888. // Put three objects
  889. err := s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  890. c.Assert(err, IsNil)
  891. err = s.bucket.PutObject(objectName+"11", strings.NewReader(""))
  892. c.Assert(err, IsNil)
  893. err = s.bucket.PutObject(objectName+"111", strings.NewReader(""))
  894. c.Assert(err, IsNil)
  895. // Exist
  896. exist, err := s.bucket.IsObjectExist(objectName + "11")
  897. c.Assert(err, IsNil)
  898. c.Assert(exist, Equals, true)
  899. exist, err = s.bucket.IsObjectExist(objectName + "1")
  900. c.Assert(err, IsNil)
  901. c.Assert(exist, Equals, true)
  902. exist, err = s.bucket.IsObjectExist(objectName + "111")
  903. c.Assert(err, IsNil)
  904. c.Assert(exist, Equals, true)
  905. // Not exist
  906. exist, err = s.bucket.IsObjectExist(objectName + "1111")
  907. c.Assert(err, IsNil)
  908. c.Assert(exist, Equals, false)
  909. exist, err = s.bucket.IsObjectExist(objectName)
  910. c.Assert(err, IsNil)
  911. c.Assert(exist, Equals, false)
  912. err = s.bucket.DeleteObject(objectName + "1")
  913. c.Assert(err, IsNil)
  914. err = s.bucket.DeleteObject(objectName + "11")
  915. c.Assert(err, IsNil)
  916. err = s.bucket.DeleteObject(objectName + "111")
  917. c.Assert(err, IsNil)
  918. }
  919. // TestDeleteObject
  920. func (s *OssBucketSuite) TestDeleteObject(c *C) {
  921. objectName := objectNamePrefix + "tdo"
  922. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  923. c.Assert(err, IsNil)
  924. lor, err := s.bucket.ListObjects(Prefix(objectName))
  925. c.Assert(err, IsNil)
  926. c.Assert(len(lor.Objects), Equals, 1)
  927. // Delete
  928. err = s.bucket.DeleteObject(objectName)
  929. c.Assert(err, IsNil)
  930. // Duplicate delete
  931. err = s.bucket.DeleteObject(objectName)
  932. c.Assert(err, IsNil)
  933. lor, err = s.bucket.ListObjects(Prefix(objectName))
  934. c.Assert(err, IsNil)
  935. c.Assert(len(lor.Objects), Equals, 0)
  936. }
  937. // TestDeleteObjects
  938. func (s *OssBucketSuite) TestDeleteObjects(c *C) {
  939. objectName := objectNamePrefix + "tdos"
  940. // Delete objects
  941. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  942. c.Assert(err, IsNil)
  943. res, err := s.bucket.DeleteObjects([]string{objectName})
  944. c.Assert(err, IsNil)
  945. c.Assert(len(res.DeletedObjects), Equals, 1)
  946. lor, err := s.bucket.ListObjects(Prefix(objectName))
  947. c.Assert(err, IsNil)
  948. c.Assert(len(lor.Objects), Equals, 0)
  949. // Delete objects
  950. err = s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  951. c.Assert(err, IsNil)
  952. err = s.bucket.PutObject(objectName+"2", strings.NewReader(""))
  953. c.Assert(err, IsNil)
  954. res, err = s.bucket.DeleteObjects([]string{objectName + "1", objectName + "2"})
  955. c.Assert(err, IsNil)
  956. c.Assert(len(res.DeletedObjects), Equals, 2)
  957. lor, err = s.bucket.ListObjects(Prefix(objectName))
  958. c.Assert(err, IsNil)
  959. c.Assert(len(lor.Objects), Equals, 0)
  960. // Delete 0
  961. _, err = s.bucket.DeleteObjects([]string{})
  962. c.Assert(err, NotNil)
  963. // DeleteObjectsQuiet
  964. err = s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  965. c.Assert(err, IsNil)
  966. err = s.bucket.PutObject(objectName+"2", strings.NewReader(""))
  967. c.Assert(err, IsNil)
  968. res, err = s.bucket.DeleteObjects([]string{objectName + "1", objectName + "2"},
  969. DeleteObjectsQuiet(false))
  970. c.Assert(err, IsNil)
  971. c.Assert(len(res.DeletedObjects), Equals, 2)
  972. lor, err = s.bucket.ListObjects(Prefix(objectName))
  973. c.Assert(err, IsNil)
  974. c.Assert(len(lor.Objects), Equals, 0)
  975. // DeleteObjectsQuiet
  976. err = s.bucket.PutObject(objectName+"1", strings.NewReader(""))
  977. c.Assert(err, IsNil)
  978. err = s.bucket.PutObject(objectName+"2", strings.NewReader(""))
  979. c.Assert(err, IsNil)
  980. res, err = s.bucket.DeleteObjects([]string{objectName + "1", objectName + "2"},
  981. DeleteObjectsQuiet(true))
  982. c.Assert(err, IsNil)
  983. c.Assert(len(res.DeletedObjects), Equals, 0)
  984. lor, err = s.bucket.ListObjects(Prefix(objectName))
  985. c.Assert(err, IsNil)
  986. c.Assert(len(lor.Objects), Equals, 0)
  987. // EncodingType
  988. err = s.bucket.PutObject("中国人", strings.NewReader(""))
  989. c.Assert(err, IsNil)
  990. res, err = s.bucket.DeleteObjects([]string{"中国人"})
  991. c.Assert(err, IsNil)
  992. c.Assert(len(res.DeletedObjects), Equals, 1)
  993. c.Assert(res.DeletedObjects[0], Equals, "中国人")
  994. // EncodingType
  995. err = s.bucket.PutObject("中国人", strings.NewReader(""))
  996. c.Assert(err, IsNil)
  997. res, err = s.bucket.DeleteObjects([]string{"中国人"}, DeleteObjectsQuiet(false))
  998. c.Assert(err, IsNil)
  999. c.Assert(len(res.DeletedObjects), Equals, 1)
  1000. c.Assert(res.DeletedObjects[0], Equals, "中国人")
  1001. // EncodingType
  1002. err = s.bucket.PutObject("中国人", strings.NewReader(""))
  1003. c.Assert(err, IsNil)
  1004. res, err = s.bucket.DeleteObjects([]string{"中国人"}, DeleteObjectsQuiet(true))
  1005. c.Assert(err, IsNil)
  1006. c.Assert(len(res.DeletedObjects), Equals, 0)
  1007. // Special characters
  1008. key := "A ' < > \" & ~ ` ! @ # $ % ^ & * ( ) [] {} - _ + = / | \\ ? . , : ; A"
  1009. err = s.bucket.PutObject(key, strings.NewReader("value"))
  1010. c.Assert(err, IsNil)
  1011. _, err = s.bucket.DeleteObjects([]string{key})
  1012. c.Assert(err, IsNil)
  1013. ress, err := s.bucket.ListObjects(Prefix(key))
  1014. c.Assert(err, IsNil)
  1015. c.Assert(len(ress.Objects), Equals, 0)
  1016. // Not exist
  1017. _, err = s.bucket.DeleteObjects([]string{"NotExistObject"})
  1018. c.Assert(err, IsNil)
  1019. }
  1020. // TestSetObjectMeta
  1021. func (s *OssBucketSuite) TestSetObjectMeta(c *C) {
  1022. objectName := objectNamePrefix + "tsom"
  1023. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  1024. c.Assert(err, IsNil)
  1025. err = s.bucket.SetObjectMeta(objectName,
  1026. Expires(futureDate),
  1027. Meta("myprop", "mypropval"))
  1028. c.Assert(err, IsNil)
  1029. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  1030. c.Assert(err, IsNil)
  1031. testLogger.Println("Meta:", meta)
  1032. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  1033. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  1034. acl, err := s.bucket.GetObjectACL(objectName)
  1035. c.Assert(err, IsNil)
  1036. c.Assert(acl.ACL, Equals, "default")
  1037. // Invalid option
  1038. err = s.bucket.SetObjectMeta(objectName, AcceptEncoding("url"))
  1039. c.Assert(err, IsNil)
  1040. // Invalid option value
  1041. err = s.bucket.SetObjectMeta(objectName, ServerSideEncryption("invalid"))
  1042. c.Assert(err, NotNil)
  1043. err = s.bucket.DeleteObject(objectName)
  1044. c.Assert(err, IsNil)
  1045. // Not exist
  1046. err = s.bucket.SetObjectMeta(objectName, Expires(futureDate))
  1047. c.Assert(err, NotNil)
  1048. }
  1049. // TestGetObjectMeta
  1050. func (s *OssBucketSuite) TestGetObjectMeta(c *C) {
  1051. objectName := objectNamePrefix + "tgom"
  1052. // Put
  1053. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  1054. c.Assert(err, IsNil)
  1055. meta, err := s.bucket.GetObjectMeta(objectName)
  1056. c.Assert(err, IsNil)
  1057. c.Assert(len(meta) > 0, Equals, true)
  1058. err = s.bucket.DeleteObject(objectName)
  1059. c.Assert(err, IsNil)
  1060. _, err = s.bucket.GetObjectMeta("NotExistObject")
  1061. c.Assert(err, NotNil)
  1062. }
  1063. // TestGetObjectDetailedMeta
  1064. func (s *OssBucketSuite) TestGetObjectDetailedMeta(c *C) {
  1065. objectName := objectNamePrefix + "tgodm"
  1066. // Put
  1067. err := s.bucket.PutObject(objectName, strings.NewReader(""),
  1068. Expires(futureDate), Meta("myprop", "mypropval"))
  1069. c.Assert(err, IsNil)
  1070. // Check
  1071. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  1072. c.Assert(err, IsNil)
  1073. testLogger.Println("GetObjectDetailedMeta:", meta)
  1074. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  1075. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  1076. c.Assert(meta.Get("Content-Length"), Equals, "0")
  1077. c.Assert(len(meta.Get("Date")) > 0, Equals, true)
  1078. c.Assert(len(meta.Get("X-Oss-Request-Id")) > 0, Equals, true)
  1079. c.Assert(len(meta.Get("Last-Modified")) > 0, Equals, true)
  1080. // IfModifiedSince/IfModifiedSince
  1081. _, err = s.bucket.GetObjectDetailedMeta(objectName, IfModifiedSince(futureDate))
  1082. c.Assert(err, NotNil)
  1083. meta, err = s.bucket.GetObjectDetailedMeta(objectName, IfUnmodifiedSince(futureDate))
  1084. c.Assert(err, IsNil)
  1085. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  1086. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  1087. // IfMatch/IfNoneMatch
  1088. _, err = s.bucket.GetObjectDetailedMeta(objectName, IfNoneMatch(meta.Get("Etag")))
  1089. c.Assert(err, NotNil)
  1090. meta, err = s.bucket.GetObjectDetailedMeta(objectName, IfMatch(meta.Get("Etag")))
  1091. c.Assert(err, IsNil)
  1092. c.Assert(meta.Get("Expires"), Equals, futureDate.Format(http.TimeFormat))
  1093. c.Assert(meta.Get("X-Oss-Meta-Myprop"), Equals, "mypropval")
  1094. err = s.bucket.DeleteObject(objectName)
  1095. c.Assert(err, IsNil)
  1096. _, err = s.bucket.GetObjectDetailedMeta("NotExistObject")
  1097. c.Assert(err, NotNil)
  1098. }
  1099. // TestSetAndGetObjectAcl
  1100. func (s *OssBucketSuite) TestSetAndGetObjectAcl(c *C) {
  1101. objectName := objectNamePrefix + "tsgba"
  1102. err := s.bucket.PutObject(objectName, strings.NewReader(""))
  1103. c.Assert(err, IsNil)
  1104. // Default
  1105. acl, err := s.bucket.GetObjectACL(objectName)
  1106. c.Assert(err, IsNil)
  1107. c.Assert(acl.ACL, Equals, "default")
  1108. // Set ACL_PUBLIC_RW
  1109. err = s.bucket.SetObjectACL(objectName, ACLPublicReadWrite)
  1110. c.Assert(err, IsNil)
  1111. acl, err = s.bucket.GetObjectACL(objectName)
  1112. c.Assert(err, IsNil)
  1113. c.Assert(acl.ACL, Equals, string(ACLPublicReadWrite))
  1114. // Set ACL_PRIVATE
  1115. err = s.bucket.SetObjectACL(objectName, ACLPrivate)
  1116. c.Assert(err, IsNil)
  1117. acl, err = s.bucket.GetObjectACL(objectName)
  1118. c.Assert(err, IsNil)
  1119. c.Assert(acl.ACL, Equals, string(ACLPrivate))
  1120. // Set ACL_PUBLIC_R
  1121. err = s.bucket.SetObjectACL(objectName, ACLPublicRead)
  1122. c.Assert(err, IsNil)
  1123. acl, err = s.bucket.GetObjectACL(objectName)
  1124. c.Assert(err, IsNil)
  1125. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  1126. err = s.bucket.DeleteObject(objectName)
  1127. c.Assert(err, IsNil)
  1128. }
  1129. // TestSetAndGetObjectAclNegative
  1130. func (s *OssBucketSuite) TestSetAndGetObjectAclNegative(c *C) {
  1131. objectName := objectNamePrefix + "tsgban"
  1132. // Object not exist
  1133. err := s.bucket.SetObjectACL(objectName, ACLPublicRead)
  1134. c.Assert(err, NotNil)
  1135. }
  1136. // TestCopyObject
  1137. func (s *OssBucketSuite) TestCopyObject(c *C) {
  1138. objectName := objectNamePrefix + "tco"
  1139. objectValue := "男儿何不带吴钩,收取关山五十州。请君暂上凌烟阁,若个书生万户侯?"
  1140. err := s.bucket.PutObject(objectName, strings.NewReader(objectValue),
  1141. ACL(ACLPublicRead), Meta("my", "myprop"))
  1142. c.Assert(err, IsNil)
  1143. // Copy
  1144. var objectNameDest = objectName + "dest"
  1145. _, err = s.bucket.CopyObject(objectName, objectNameDest)
  1146. c.Assert(err, IsNil)
  1147. // Check
  1148. lor, err := s.bucket.ListObjects(Prefix(objectName))
  1149. c.Assert(err, IsNil)
  1150. testLogger.Println("objects:", lor.Objects)
  1151. c.Assert(len(lor.Objects), Equals, 2)
  1152. body, err := s.bucket.GetObject(objectName)
  1153. c.Assert(err, IsNil)
  1154. str, err := readBody(body)
  1155. c.Assert(err, IsNil)
  1156. c.Assert(str, Equals, objectValue)
  1157. err = s.bucket.DeleteObject(objectNameDest)
  1158. c.Assert(err, IsNil)
  1159. // Copy with constraints x-oss-copy-source-if-modified-since
  1160. _, err = s.bucket.CopyObject(objectName, objectNameDest, CopySourceIfModifiedSince(futureDate))
  1161. c.Assert(err, NotNil)
  1162. testLogger.Println("CopyObject:", err)
  1163. // Copy with constraints x-oss-copy-source-if-unmodified-since
  1164. _, err = s.bucket.CopyObject(objectName, objectNameDest, CopySourceIfUnmodifiedSince(futureDate))
  1165. c.Assert(err, IsNil)
  1166. // Check
  1167. lor, err = s.bucket.ListObjects(Prefix(objectName))
  1168. c.Assert(err, IsNil)
  1169. testLogger.Println("objects:", lor.Objects)
  1170. c.Assert(len(lor.Objects), Equals, 2)
  1171. body, err = s.bucket.GetObject(objectName)
  1172. c.Assert(err, IsNil)
  1173. str, err = readBody(body)
  1174. c.Assert(err, IsNil)
  1175. c.Assert(str, Equals, objectValue)
  1176. err = s.bucket.DeleteObject(objectNameDest)
  1177. c.Assert(err, IsNil)
  1178. // Copy with constraints x-oss-copy-source-if-match
  1179. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  1180. c.Assert(err, IsNil)
  1181. testLogger.Println("GetObjectDetailedMeta:", meta)
  1182. _, err = s.bucket.CopyObject(objectName, objectNameDest, CopySourceIfMatch(meta.Get("Etag")))
  1183. c.Assert(err, IsNil)
  1184. // Check
  1185. body, err = s.bucket.GetObject(objectName)
  1186. c.Assert(err, IsNil)
  1187. str, err = readBody(body)
  1188. c.Assert(err, IsNil)
  1189. c.Assert(str, Equals, objectValue)
  1190. err = s.bucket.DeleteObject(objectNameDest)
  1191. c.Assert(err, IsNil)
  1192. // Copy with constraints x-oss-copy-source-if-none-match
  1193. _, err = s.bucket.CopyObject(objectName, objectNameDest, CopySourceIfNoneMatch(meta.Get("Etag")))
  1194. c.Assert(err, NotNil)
  1195. // Copy with constraints x-oss-metadata-directive
  1196. _, err = s.bucket.CopyObject(objectName, objectNameDest, Meta("my", "mydestprop"),
  1197. MetadataDirective(MetaCopy))
  1198. c.Assert(err, IsNil)
  1199. // Check
  1200. body, err = s.bucket.GetObject(objectName)
  1201. c.Assert(err, IsNil)
  1202. str, err = readBody(body)
  1203. c.Assert(err, IsNil)
  1204. c.Assert(str, Equals, objectValue)
  1205. destMeta, err := s.bucket.GetObjectDetailedMeta(objectNameDest)
  1206. c.Assert(err, IsNil)
  1207. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  1208. acl, err := s.bucket.GetObjectACL(objectNameDest)
  1209. c.Assert(err, IsNil)
  1210. c.Assert(acl.ACL, Equals, "default")
  1211. err = s.bucket.DeleteObject(objectNameDest)
  1212. c.Assert(err, IsNil)
  1213. // Copy with constraints x-oss-metadata-directive and self defined dest object meta
  1214. options := []Option{
  1215. ObjectACL(ACLPublicReadWrite),
  1216. Meta("my", "mydestprop"),
  1217. MetadataDirective(MetaReplace),
  1218. }
  1219. _, err = s.bucket.CopyObject(objectName, objectNameDest, options...)
  1220. c.Assert(err, IsNil)
  1221. // Check
  1222. body, err = s.bucket.GetObject(objectName)
  1223. c.Assert(err, IsNil)
  1224. str, err = readBody(body)
  1225. c.Assert(err, IsNil)
  1226. c.Assert(str, Equals, objectValue)
  1227. destMeta, err = s.bucket.GetObjectDetailedMeta(objectNameDest)
  1228. c.Assert(err, IsNil)
  1229. c.Assert(destMeta.Get("X-Oss-Meta-My"), Equals, "mydestprop")
  1230. acl, err = s.bucket.GetObjectACL(objectNameDest)
  1231. c.Assert(err, IsNil)
  1232. c.Assert(acl.ACL, Equals, string(ACLPublicReadWrite))
  1233. err = s.bucket.DeleteObject(objectNameDest)
  1234. c.Assert(err, IsNil)
  1235. err = s.bucket.DeleteObject(objectName)
  1236. c.Assert(err, IsNil)
  1237. }
  1238. // TestCopyObjectToOrFrom
  1239. func (s *OssBucketSuite) TestCopyObjectToOrFrom(c *C) {
  1240. objectName := objectNamePrefix + "tcotof"
  1241. objectValue := "男儿何不带吴钩,收取关山五十州。请君暂上凌烟阁,若个书生万户侯?"
  1242. destBucket := bucketName + "-dest"
  1243. objectNameDest := objectName + "dest"
  1244. s.client.CreateBucket(destBucket)
  1245. destBuck, err := s.client.Bucket(destBucket)
  1246. c.Assert(err, IsNil)
  1247. err = s.bucket.PutObject(objectName, strings.NewReader(objectValue))
  1248. c.Assert(err, IsNil)
  1249. // Copy from
  1250. _, err = destBuck.CopyObjectFrom(bucketName, objectName, objectNameDest)
  1251. c.Assert(err, IsNil)
  1252. // Check
  1253. body, err := destBuck.GetObject(objectNameDest)
  1254. c.Assert(err, IsNil)
  1255. str, err := readBody(body)
  1256. c.Assert(err, IsNil)
  1257. c.Assert(str, Equals, objectValue)
  1258. err = s.bucket.DeleteObject(objectName)
  1259. c.Assert(err, IsNil)
  1260. // Copy to
  1261. _, err = destBuck.CopyObjectTo(bucketName, objectName, objectNameDest)
  1262. c.Assert(err, IsNil)
  1263. // Check
  1264. body, err = s.bucket.GetObject(objectName)
  1265. c.Assert(err, IsNil)
  1266. str, err = readBody(body)
  1267. c.Assert(err, IsNil)
  1268. c.Assert(str, Equals, objectValue)
  1269. // Clean
  1270. err = destBuck.DeleteObject(objectNameDest)
  1271. c.Assert(err, IsNil)
  1272. err = s.bucket.DeleteObject(objectName)
  1273. c.Assert(err, IsNil)
  1274. err = s.client.DeleteBucket(destBucket)
  1275. c.Assert(err, IsNil)
  1276. }
  1277. // TestCopyObjectToOrFromNegative
  1278. func (s *OssBucketSuite) TestCopyObjectToOrFromNegative(c *C) {
  1279. objectName := objectNamePrefix + "tcotofn"
  1280. destBucket := bucketName + "-destn"
  1281. objectNameDest := objectName + "destn"
  1282. // Object not exist
  1283. _, err := s.bucket.CopyObjectTo(bucketName, objectName, objectNameDest)
  1284. c.Assert(err, NotNil)
  1285. // Bucket not exist
  1286. _, err = s.bucket.CopyObjectFrom(destBucket, objectNameDest, objectName)
  1287. c.Assert(err, NotNil)
  1288. }
  1289. // TestAppendObject
  1290. func (s *OssBucketSuite) TestAppendObject(c *C) {
  1291. objectName := objectNamePrefix + "tao"
  1292. objectValue := "昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。"
  1293. var val = []byte(objectValue)
  1294. var localFile = "testx.txt"
  1295. var nextPos int64
  1296. var midPos = 1 + rand.Intn(len(val)-1)
  1297. var err = createFileAndWrite(localFile+"1", val[0:midPos])
  1298. c.Assert(err, IsNil)
  1299. err = createFileAndWrite(localFile+"2", val[midPos:])
  1300. c.Assert(err, IsNil)
  1301. // String append
  1302. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader("昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,"), nextPos)
  1303. c.Assert(err, IsNil)
  1304. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader("却道海棠依旧。知否?知否?应是绿肥红瘦。"), nextPos)
  1305. c.Assert(err, IsNil)
  1306. body, err := s.bucket.GetObject(objectName)
  1307. c.Assert(err, IsNil)
  1308. str, err := readBody(body)
  1309. c.Assert(err, IsNil)
  1310. c.Assert(str, Equals, objectValue)
  1311. err = s.bucket.DeleteObject(objectName)
  1312. c.Assert(err, IsNil)
  1313. // Byte append
  1314. nextPos = 0
  1315. nextPos, err = s.bucket.AppendObject(objectName, bytes.NewReader(val[0:midPos]), nextPos)
  1316. c.Assert(err, IsNil)
  1317. nextPos, err = s.bucket.AppendObject(objectName, bytes.NewReader(val[midPos:]), nextPos)
  1318. c.Assert(err, IsNil)
  1319. body, err = s.bucket.GetObject(objectName)
  1320. c.Assert(err, IsNil)
  1321. str, err = readBody(body)
  1322. c.Assert(err, IsNil)
  1323. c.Assert(str, Equals, objectValue)
  1324. err = s.bucket.DeleteObject(objectName)
  1325. c.Assert(err, IsNil)
  1326. // File append
  1327. options := []Option{
  1328. ObjectACL(ACLPublicReadWrite),
  1329. Meta("my", "myprop"),
  1330. }
  1331. fd, err := os.Open(localFile + "1")
  1332. c.Assert(err, IsNil)
  1333. defer fd.Close()
  1334. nextPos = 0
  1335. nextPos, err = s.bucket.AppendObject(objectName, fd, nextPos, options...)
  1336. c.Assert(err, IsNil)
  1337. meta, err := s.bucket.GetObjectDetailedMeta(objectName)
  1338. c.Assert(err, IsNil)
  1339. testLogger.Println("GetObjectDetailedMeta:", meta, ",", nextPos)
  1340. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Appendable")
  1341. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  1342. c.Assert(meta.Get("x-oss-Meta-Mine"), Equals, "")
  1343. c.Assert(meta.Get("X-Oss-Next-Append-Position"), Equals, strconv.FormatInt(nextPos, 10))
  1344. acl, err := s.bucket.GetObjectACL(objectName)
  1345. c.Assert(err, IsNil)
  1346. testLogger.Println("GetObjectACL:", acl)
  1347. c.Assert(acl.ACL, Equals, string(ACLPublicReadWrite))
  1348. // Second append
  1349. options = []Option{
  1350. ObjectACL(ACLPublicRead),
  1351. Meta("my", "myproptwo"),
  1352. Meta("mine", "mypropmine"),
  1353. }
  1354. fd, err = os.Open(localFile + "2")
  1355. c.Assert(err, IsNil)
  1356. defer fd.Close()
  1357. nextPos, err = s.bucket.AppendObject(objectName, fd, nextPos, options...)
  1358. c.Assert(err, IsNil)
  1359. body, err = s.bucket.GetObject(objectName)
  1360. c.Assert(err, IsNil)
  1361. str, err = readBody(body)
  1362. c.Assert(err, IsNil)
  1363. c.Assert(str, Equals, objectValue)
  1364. meta, err = s.bucket.GetObjectDetailedMeta(objectName)
  1365. c.Assert(err, IsNil)
  1366. testLogger.Println("GetObjectDetailedMeta xxx:", meta)
  1367. c.Assert(meta.Get("X-Oss-Object-Type"), Equals, "Appendable")
  1368. c.Assert(meta.Get("X-Oss-Meta-My"), Equals, "myprop")
  1369. c.Assert(meta.Get("x-Oss-Meta-Mine"), Equals, "")
  1370. c.Assert(meta.Get("X-Oss-Next-Append-Position"), Equals, strconv.FormatInt(nextPos, 10))
  1371. acl, err = s.bucket.GetObjectACL(objectName)
  1372. c.Assert(err, IsNil)
  1373. c.Assert(acl.ACL, Equals, string(ACLPublicRead))
  1374. err = s.bucket.DeleteObject(objectName)
  1375. c.Assert(err, IsNil)
  1376. }
  1377. // TestAppendObjectNegative
  1378. func (s *OssBucketSuite) TestAppendObjectNegative(c *C) {
  1379. objectName := objectNamePrefix + "taon"
  1380. nextPos := int64(0)
  1381. nextPos, err := s.bucket.AppendObject(objectName, strings.NewReader("ObjectValue"), nextPos)
  1382. c.Assert(err, IsNil)
  1383. nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader("ObjectValue"), 0)
  1384. c.Assert(err, NotNil)
  1385. err = s.bucket.DeleteObject(objectName)
  1386. c.Assert(err, IsNil)
  1387. }
  1388. // TestContentType
  1389. func (s *OssBucketSuite) TestAddContentType(c *C) {
  1390. opts := addContentType(nil, "abc.txt")
  1391. typ, err := findOption(opts, HTTPHeaderContentType, "")
  1392. c.Assert(err, IsNil)
  1393. c.Assert(typ, Equals, "text/plain; charset=utf-8")
  1394. opts = addContentType(nil)
  1395. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1396. c.Assert(err, IsNil)
  1397. c.Assert(len(opts), Equals, 1)
  1398. c.Assert(typ, Equals, "application/octet-stream")
  1399. opts = addContentType(nil, "abc.txt", "abc.pdf")
  1400. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1401. c.Assert(err, IsNil)
  1402. c.Assert(typ, Equals, "text/plain; charset=utf-8")
  1403. opts = addContentType(nil, "abc", "abc.txt", "abc.pdf")
  1404. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1405. c.Assert(err, IsNil)
  1406. c.Assert(typ, Equals, "text/plain; charset=utf-8")
  1407. opts = addContentType(nil, "abc", "abc", "edf")
  1408. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1409. c.Assert(err, IsNil)
  1410. c.Assert(typ, Equals, "application/octet-stream")
  1411. opts = addContentType([]Option{Meta("meta", "my")}, "abc", "abc.txt", "abc.pdf")
  1412. typ, err = findOption(opts, HTTPHeaderContentType, "")
  1413. c.Assert(err, IsNil)
  1414. c.Assert(len(opts), Equals, 2)
  1415. c.Assert(typ, Equals, "text/plain; charset=utf-8")
  1416. }
  1417. func (s *OssBucketSuite) TestGetConfig(c *C) {
  1418. client, err := New(endpoint, accessID, accessKey, UseCname(true),
  1419. Timeout(11, 12), SecurityToken("token"), EnableMD5(false))
  1420. c.Assert(err, IsNil)
  1421. bucket, err := client.Bucket(bucketName)
  1422. c.Assert(err, IsNil)
  1423. c.Assert(bucket.getConfig().HTTPTimeout.ConnectTimeout, Equals, time.Second*11)
  1424. c.Assert(bucket.getConfig().HTTPTimeout.ReadWriteTimeout, Equals, time.Second*12)
  1425. c.Assert(bucket.getConfig().HTTPTimeout.HeaderTimeout, Equals, time.Second*12)
  1426. c.Assert(bucket.getConfig().HTTPTimeout.IdleConnTimeout, Equals, time.Second*12)
  1427. c.Assert(bucket.getConfig().HTTPTimeout.LongTimeout, Equals, time.Second*12*10)
  1428. c.Assert(bucket.getConfig().SecurityToken, Equals, "token")
  1429. c.Assert(bucket.getConfig().IsCname, Equals, true)
  1430. c.Assert(bucket.getConfig().IsEnableMD5, Equals, false)
  1431. }
  1432. func (s *OssBucketSuite) TestSTSToken(c *C) {
  1433. objectName := objectNamePrefix + "tst"
  1434. objectValue := "红藕香残玉簟秋。轻解罗裳,独上兰舟。云中谁寄锦书来?雁字回时,月满西楼。"
  1435. stsClient := sts.NewClient(stsaccessID, stsaccessKey, stsARN, "oss_test_sess")
  1436. resp, err := stsClient.AssumeRole(1800)
  1437. c.Assert(err, IsNil)
  1438. client, err := New(endpoint, resp.Credentials.AccessKeyId, resp.Credentials.AccessKeySecret,
  1439. SecurityToken(resp.Credentials.SecurityToken))
  1440. c.Assert(err, IsNil)
  1441. bucket, err := client.Bucket(bucketName)
  1442. c.Assert(err, IsNil)
  1443. // Put
  1444. err = bucket.PutObject(objectName, strings.NewReader(objectValue))
  1445. c.Assert(err, IsNil)
  1446. // Get
  1447. body, err := bucket.GetObject(objectName)
  1448. c.Assert(err, IsNil)
  1449. str, err := readBody(body)
  1450. c.Assert(err, IsNil)
  1451. c.Assert(str, Equals, objectValue)
  1452. // List
  1453. lor, err := bucket.ListObjects()
  1454. c.Assert(err, IsNil)
  1455. testLogger.Println("Objects:", lor.Objects)
  1456. // Put with URL
  1457. signedURL, err := bucket.SignURL(objectName, HTTPPut, 3600)
  1458. c.Assert(err, IsNil)
  1459. err = bucket.PutObjectWithURL(signedURL, strings.NewReader(objectValue))
  1460. c.Assert(err, IsNil)
  1461. // Get with URL
  1462. signedURL, err = bucket.SignURL(objectName, HTTPGet, 3600)
  1463. c.Assert(err, IsNil)
  1464. body, err = bucket.GetObjectWithURL(signedURL)
  1465. c.Assert(err, IsNil)
  1466. str, err = readBody(body)
  1467. c.Assert(err, IsNil)
  1468. c.Assert(str, Equals, objectValue)
  1469. // Delete
  1470. err = bucket.DeleteObject(objectName)
  1471. c.Assert(err, IsNil)
  1472. }
  1473. func (s *OssBucketSuite) TestSTSTonekNegative(c *C) {
  1474. objectName := objectNamePrefix + "tstg"
  1475. localFile := objectName + ".jpg"
  1476. client, err := New(endpoint, accessID, accessKey, SecurityToken("Invalid"))
  1477. c.Assert(err, IsNil)
  1478. _, err = client.ListBuckets()
  1479. c.Assert(err, NotNil)
  1480. bucket, err := client.Bucket(bucketName)
  1481. c.Assert(err, IsNil)
  1482. err = bucket.PutObject(objectName, strings.NewReader(""))
  1483. c.Assert(err, NotNil)
  1484. err = bucket.PutObjectFromFile(objectName, "")
  1485. c.Assert(err, NotNil)
  1486. _, err = bucket.GetObject(objectName)
  1487. c.Assert(err, NotNil)
  1488. err = bucket.GetObjectToFile(objectName, "")
  1489. c.Assert(err, NotNil)
  1490. _, err = bucket.ListObjects()
  1491. c.Assert(err, NotNil)
  1492. err = bucket.SetObjectACL(objectName, ACLPublicRead)
  1493. c.Assert(err, NotNil)
  1494. _, err = bucket.GetObjectACL(objectName)
  1495. c.Assert(err, NotNil)
  1496. err = bucket.UploadFile(objectName, localFile, MinPartSize)
  1497. c.Assert(err, NotNil)
  1498. err = bucket.DownloadFile(objectName, localFile, MinPartSize)
  1499. c.Assert(err, NotNil)
  1500. _, err = bucket.IsObjectExist(objectName)
  1501. c.Assert(err, NotNil)
  1502. _, err = bucket.ListMultipartUploads()
  1503. c.Assert(err, NotNil)
  1504. err = bucket.DeleteObject(objectName)
  1505. c.Assert(err, NotNil)
  1506. _, err = bucket.DeleteObjects([]string{objectName})
  1507. c.Assert(err, NotNil)
  1508. err = client.DeleteBucket(bucketName)
  1509. c.Assert(err, NotNil)
  1510. }
  1511. func (s *OssBucketSuite) TestUploadBigFile(c *C) {
  1512. objectName := objectNamePrefix + "tubf"
  1513. bigFile := "D:\\tmp\\bigfile.zip"
  1514. newFile := "D:\\tmp\\newbigfile.zip"
  1515. exist, err := isFileExist(bigFile)
  1516. c.Assert(err, IsNil)
  1517. if !exist {
  1518. return
  1519. }
  1520. // Put
  1521. start := GetNowSec()
  1522. err = s.bucket.PutObjectFromFile(objectName, bigFile)
  1523. c.Assert(err, IsNil)
  1524. end := GetNowSec()
  1525. testLogger.Println("Put big file:", bigFile, "use sec:", end-start)
  1526. // Check
  1527. start = GetNowSec()
  1528. err = s.bucket.GetObjectToFile(objectName, newFile)
  1529. c.Assert(err, IsNil)
  1530. end = GetNowSec()
  1531. testLogger.Println("Get big file:", bigFile, "use sec:", end-start)
  1532. start = GetNowSec()
  1533. eq, err := compareFiles(bigFile, newFile)
  1534. c.Assert(err, IsNil)
  1535. c.Assert(eq, Equals, true)
  1536. end = GetNowSec()
  1537. testLogger.Println("Compare big file:", bigFile, "use sec:", end-start)
  1538. err = s.bucket.DeleteObject(objectName)
  1539. c.Assert(err, IsNil)
  1540. }
  1541. func (s *OssBucketSuite) TestSymlink(c *C) {
  1542. objectName := objectNamePrefix + "符号链接"
  1543. targetObjectName := objectNamePrefix + "符号链接目标文件"
  1544. err := s.bucket.DeleteObject(objectName)
  1545. c.Assert(err, IsNil)
  1546. err = s.bucket.DeleteObject(targetObjectName)
  1547. c.Assert(err, IsNil)
  1548. meta, err := s.bucket.GetSymlink(objectName)
  1549. c.Assert(err, NotNil)
  1550. // Put symlink
  1551. err = s.bucket.PutSymlink(objectName, targetObjectName)
  1552. c.Assert(err, IsNil)
  1553. err = s.bucket.PutObject(targetObjectName, strings.NewReader("target"))
  1554. c.Assert(err, IsNil)
  1555. err = s.bucket.PutSymlink(objectName, targetObjectName)
  1556. c.Assert(err, IsNil)
  1557. meta, err = s.bucket.GetSymlink(objectName)
  1558. c.Assert(err, IsNil)
  1559. c.Assert(meta.Get(HTTPHeaderOssSymlinkTarget), Equals, targetObjectName)
  1560. // List object
  1561. lor, err := s.bucket.ListObjects()
  1562. c.Assert(err, IsNil)
  1563. exist, v := s.getObject(lor.Objects, objectName)
  1564. c.Assert(exist, Equals, true)
  1565. c.Assert(v.Type, Equals, "Symlink")
  1566. body, err := s.bucket.GetObject(objectName)
  1567. c.Assert(err, IsNil)
  1568. str, err := readBody(body)
  1569. c.Assert(err, IsNil)
  1570. c.Assert(str, Equals, "target")
  1571. meta, err = s.bucket.GetSymlink(targetObjectName)
  1572. c.Assert(err, NotNil)
  1573. err = s.bucket.PutObject(objectName, strings.NewReader("src"))
  1574. c.Assert(err, IsNil)
  1575. body, err = s.bucket.GetObject(objectName)
  1576. c.Assert(err, IsNil)
  1577. str, err = readBody(body)
  1578. c.Assert(err, IsNil)
  1579. c.Assert(str, Equals, "src")
  1580. err = s.bucket.DeleteObject(objectName)
  1581. c.Assert(err, IsNil)
  1582. err = s.bucket.DeleteObject(targetObjectName)
  1583. c.Assert(err, IsNil)
  1584. // Put symlink again
  1585. objectName = objectNamePrefix + "symlink"
  1586. targetObjectName = objectNamePrefix + "symlink-target"
  1587. err = s.bucket.PutSymlink(objectName, targetObjectName)
  1588. c.Assert(err, IsNil)
  1589. err = s.bucket.PutObject(targetObjectName, strings.NewReader("target1"))
  1590. c.Assert(err, IsNil)
  1591. meta, err = s.bucket.GetSymlink(objectName)
  1592. c.Assert(err, IsNil)
  1593. c.Assert(meta.Get(HTTPHeaderOssSymlinkTarget), Equals, targetObjectName)
  1594. body, err = s.bucket.GetObject(objectName)
  1595. c.Assert(err, IsNil)
  1596. str, err = readBody(body)
  1597. c.Assert(err, IsNil)
  1598. c.Assert(str, Equals, "target1")
  1599. err = s.bucket.DeleteObject(objectName)
  1600. c.Assert(err, IsNil)
  1601. err = s.bucket.DeleteObject(targetObjectName)
  1602. c.Assert(err, IsNil)
  1603. }
  1604. // TestRestoreObject
  1605. func (s *OssBucketSuite) TestRestoreObject(c *C) {
  1606. objectName := objectNamePrefix + "restore"
  1607. // List objects
  1608. lor, err := s.archiveBucket.ListObjects()
  1609. c.Assert(err, IsNil)
  1610. left := len(lor.Objects)
  1611. // Put object
  1612. err = s.archiveBucket.PutObject(objectName, strings.NewReader(""))
  1613. c.Assert(err, IsNil)
  1614. // List
  1615. lor, err = s.archiveBucket.ListObjects()
  1616. c.Assert(err, IsNil)
  1617. c.Assert(len(lor.Objects), Equals, left+1)
  1618. for _, object := range lor.Objects {
  1619. c.Assert(object.StorageClass, Equals, string(StorageArchive))
  1620. c.Assert(object.Type, Equals, "Normal")
  1621. }
  1622. // Head object
  1623. meta, err := s.archiveBucket.GetObjectDetailedMeta(objectName)
  1624. c.Assert(err, IsNil)
  1625. _, ok := meta["X-Oss-Restore"]
  1626. c.Assert(ok, Equals, false)
  1627. c.Assert(meta.Get("X-Oss-Storage-Class"), Equals, "Archive")
  1628. // Error restore object
  1629. err = s.archiveBucket.RestoreObject("notexistobject")
  1630. c.Assert(err, NotNil)
  1631. // Restore object
  1632. err = s.archiveBucket.RestoreObject(objectName)
  1633. c.Assert(err, IsNil)
  1634. // Head object
  1635. meta, err = s.archiveBucket.GetObjectDetailedMeta(objectName)
  1636. c.Assert(err, IsNil)
  1637. c.Assert(meta.Get("X-Oss-Restore"), Equals, "ongoing-request=\"true\"")
  1638. c.Assert(meta.Get("X-Oss-Storage-Class"), Equals, "Archive")
  1639. }
  1640. // TestProcessObject
  1641. func (s *OssBucketSuite) TestProcessObject(c *C) {
  1642. objectName := objectNamePrefix + "_process_src.jpg"
  1643. err := s.bucket.PutObjectFromFile(objectName, "../sample/BingWallpaper-2015-11-07.jpg")
  1644. c.Assert(err, IsNil)
  1645. // If bucket-name not specified, it is saved to the current bucket by default.
  1646. destObjName := objectNamePrefix + "_process_dest_1.jpg"
  1647. process := fmt.Sprintf("image/resize,w_100|sys/saveas,o_%v", base64.URLEncoding.EncodeToString([]byte(destObjName)))
  1648. result, err := s.bucket.ProcessObject(objectName, process)
  1649. c.Assert(err, IsNil)
  1650. exist, _ := s.bucket.IsObjectExist(destObjName)
  1651. c.Assert(exist, Equals, true)
  1652. c.Assert(result.Bucket, Equals, "")
  1653. c.Assert(result.Object, Equals, destObjName)
  1654. destObjName = objectNamePrefix + "_process_dest_1.jpg"
  1655. process = fmt.Sprintf("image/resize,w_100|sys/saveas,o_%v,b_%v", base64.URLEncoding.EncodeToString([]byte(destObjName)), base64.URLEncoding.EncodeToString([]byte(s.bucket.BucketName)))
  1656. result, err = s.bucket.ProcessObject(objectName, process)
  1657. c.Assert(err, IsNil)
  1658. exist, _ = s.bucket.IsObjectExist(destObjName)
  1659. c.Assert(exist, Equals, true)
  1660. c.Assert(result.Bucket, Equals, s.bucket.BucketName)
  1661. c.Assert(result.Object, Equals, destObjName)
  1662. //no support process
  1663. process = fmt.Sprintf("image/resize,w_100|saveas,o_%v,b_%v", base64.URLEncoding.EncodeToString([]byte(destObjName)), base64.URLEncoding.EncodeToString([]byte(s.bucket.BucketName)))
  1664. result, err = s.bucket.ProcessObject(objectName, process)
  1665. c.Assert(err, NotNil)
  1666. }
  1667. // Private
  1668. func createFileAndWrite(fileName string, data []byte) error {
  1669. os.Remove(fileName)
  1670. fo, err := os.Create(fileName)
  1671. if err != nil {
  1672. return err
  1673. }
  1674. defer fo.Close()
  1675. bytes, err := fo.Write(data)
  1676. if err != nil {
  1677. return err
  1678. }
  1679. if bytes != len(data) {
  1680. return fmt.Errorf(fmt.Sprintf("write %d bytes not equal data length %d", bytes, len(data)))
  1681. }
  1682. return nil
  1683. }
  1684. // Compare the content between fileL and fileR
  1685. func compareFiles(fileL string, fileR string) (bool, error) {
  1686. finL, err := os.Open(fileL)
  1687. if err != nil {
  1688. return false, err
  1689. }
  1690. defer finL.Close()
  1691. finR, err := os.Open(fileR)
  1692. if err != nil {
  1693. return false, err
  1694. }
  1695. defer finR.Close()
  1696. statL, err := finL.Stat()
  1697. if err != nil {
  1698. return false, err
  1699. }
  1700. statR, err := finR.Stat()
  1701. if err != nil {
  1702. return false, err
  1703. }
  1704. if statL.Size() != statR.Size() {
  1705. return false, nil
  1706. }
  1707. size := statL.Size()
  1708. if size > 102400 {
  1709. size = 102400
  1710. }
  1711. bufL := make([]byte, size)
  1712. bufR := make([]byte, size)
  1713. for {
  1714. n, _ := finL.Read(bufL)
  1715. if 0 == n {
  1716. break
  1717. }
  1718. n, _ = finR.Read(bufR)
  1719. if 0 == n {
  1720. break
  1721. }
  1722. if !bytes.Equal(bufL, bufR) {
  1723. return false, nil
  1724. }
  1725. }
  1726. return true, nil
  1727. }
  1728. // Compare the content of file and data
  1729. func compareFileData(file string, data []byte) (bool, error) {
  1730. fin, err := os.Open(file)
  1731. if err != nil {
  1732. return false, err
  1733. }
  1734. defer fin.Close()
  1735. stat, err := fin.Stat()
  1736. if err != nil {
  1737. return false, err
  1738. }
  1739. if stat.Size() != (int64)(len(data)) {
  1740. return false, nil
  1741. }
  1742. buf := make([]byte, stat.Size())
  1743. n, err := fin.Read(buf)
  1744. if err != nil {
  1745. return false, err
  1746. }
  1747. if stat.Size() != (int64)(n) {
  1748. return false, errors.New("read error")
  1749. }
  1750. if !bytes.Equal(buf, data) {
  1751. return false, nil
  1752. }
  1753. return true, nil
  1754. }
  1755. func walkDir(dirPth, suffix string) ([]string, error) {
  1756. var files = []string{}
  1757. suffix = strings.ToUpper(suffix)
  1758. err := filepath.Walk(dirPth,
  1759. func(filename string, fi os.FileInfo, err error) error {
  1760. if err != nil {
  1761. return err
  1762. }
  1763. if fi.IsDir() {
  1764. return nil
  1765. }
  1766. if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
  1767. files = append(files, filename)
  1768. }
  1769. return nil
  1770. })
  1771. return files, err
  1772. }
  1773. func removeTempFiles(path string, prefix string) error {
  1774. files, err := walkDir(path, prefix)
  1775. if err != nil {
  1776. return nil
  1777. }
  1778. for _, file := range files {
  1779. os.Remove(file)
  1780. }
  1781. return nil
  1782. }
  1783. func isFileExist(filename string) (bool, error) {
  1784. _, err := os.Stat(filename)
  1785. if err != nil && os.IsNotExist(err) {
  1786. return false, nil
  1787. } else if err != nil {
  1788. return false, err
  1789. } else {
  1790. return true, nil
  1791. }
  1792. }
  1793. func readBody(body io.ReadCloser) (string, error) {
  1794. data, err := ioutil.ReadAll(body)
  1795. body.Close()
  1796. if err != nil {
  1797. return "", err
  1798. }
  1799. return string(data), nil
  1800. }
  1801. func (s *OssBucketSuite) getObject(objects []ObjectProperties, object string) (bool, ObjectProperties) {
  1802. for _, v := range objects {
  1803. if v.Key == object {
  1804. return true, v
  1805. }
  1806. }
  1807. return false, ObjectProperties{}
  1808. }