本文整理汇总了Golang中labix/org/v2/mgo.GridFile.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang GridFile.Read方法的具体用法?Golang GridFile.Read怎么用?Golang GridFile.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类labix/org/v2/mgo.GridFile
的用法示例。
在下文中一共展示了GridFile.Read方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestGridFSOpenNext
func (s *S) TestGridFSOpenNext(c *C) {
session, err := mgo.Dial("localhost:40011")
c.Assert(err, IsNil)
defer session.Close()
db := session.DB("mydb")
gfs := db.GridFS("fs")
file, err := gfs.Create("myfile1.txt")
c.Assert(err, IsNil)
file.Write([]byte{'1'})
file.Close()
file, err = gfs.Create("myfile2.txt")
c.Assert(err, IsNil)
file.Write([]byte{'2'})
file.Close()
var f *mgo.GridFile
var b [1]byte
iter := gfs.Find(nil).Sort("-filename").Iter()
ok := gfs.OpenNext(iter, &f)
c.Assert(ok, Equals, true)
c.Check(f.Name(), Equals, "myfile2.txt")
_, err = f.Read(b[:])
c.Assert(err, IsNil)
c.Assert(string(b[:]), Equals, "2")
ok = gfs.OpenNext(iter, &f)
c.Assert(ok, Equals, true)
c.Check(f.Name(), Equals, "myfile1.txt")
_, err = f.Read(b[:])
c.Assert(err, IsNil)
c.Assert(string(b[:]), Equals, "1")
ok = gfs.OpenNext(iter, &f)
c.Assert(ok, Equals, false)
c.Assert(iter.Close(), IsNil)
c.Assert(f, IsNil)
// Do it again with a more restrictive query to make sure
// it's actually taken into account.
iter = gfs.Find(bson.M{"filename": "myfile1.txt"}).Iter()
ok = gfs.OpenNext(iter, &f)
c.Assert(ok, Equals, true)
c.Check(f.Name(), Equals, "myfile1.txt")
ok = gfs.OpenNext(iter, &f)
c.Assert(ok, Equals, false)
c.Assert(iter.Close(), IsNil)
c.Assert(f, IsNil)
}
示例2: GetFileData
func GetFileData(name string) []byte {
var (
result []byte
file *mgo.GridFile
err error
)
file = GetGridFile(name)
result = make([]byte, file.Size())
_, err = file.Read(result)
check(err)
return result
}
示例3: FindAllFiles
// 查找文件
func FindAllFiles() {
gfs := db.GridFS("fs")
iter := gfs.Find(nil).Iter()
var f *mgo.GridFile
for gfs.OpenNext(iter, &f) {
fmt.Printf("Filename: %s, %v\n", f.Name(), f.Id())
b := make([]byte, 512)
w, err := os.Create("upload/" + f.Name())
if err != nil {
continue
}
for {
n, _ := f.Read(b)
if n == 0 {
break
}
w.Write(b[0:n])
}
defer w.Close()
}
if iter.Err() != nil {
panic(iter.Err())
}
}