bucket_test.go 63 KB

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