forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridfs_examples_test.go
153 lines (127 loc) · 3.62 KB
/
gridfs_examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package gridfs_test
import (
"bytes"
"context"
"fmt"
"io"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/gridfs"
"go.mongodb.org/mongo-driver/mongo/options"
)
func ExampleBucket_OpenUploadStream() {
var fileContent []byte
var bucket *gridfs.Bucket
// Specify the Metadata option to include a "metadata" field in the files collection document.
uploadOpts := options.GridFSUpload().SetMetadata(bson.D{{"metadata tag", "tag"}})
uploadStream, err := bucket.OpenUploadStream("filename", uploadOpts)
if err != nil {
log.Fatal(err)
}
defer func() {
if err = uploadStream.Close(); err != nil {
log.Fatal(err)
}
}()
// Use SetWriteDeadline to force a timeout if the upload does not succeed in 2 seconds.
if err = uploadStream.SetWriteDeadline(time.Now().Add(2 * time.Second)); err != nil {
log.Fatal(err)
}
if _, err = uploadStream.Write(fileContent); err != nil {
log.Fatal(err)
}
}
func ExampleBucket_UploadFromStream() {
var fileContent []byte
var bucket *gridfs.Bucket
// Specify the Metadata option to include a "metadata" field in the files collection document.
uploadOpts := options.GridFSUpload().SetMetadata(bson.D{{"metadata tag", "tag"}})
fileID, err := bucket.UploadFromStream("filename", bytes.NewBuffer(fileContent), uploadOpts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("new file created with ID %s", fileID)
}
func ExampleBucket_OpenDownloadStream() {
var bucket *gridfs.Bucket
var fileID primitive.ObjectID
downloadStream, err := bucket.OpenDownloadStream(fileID)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := downloadStream.Close(); err != nil {
log.Fatal(err)
}
}()
// Use SetReadDeadline to force a timeout if the download does not succeed in 2 seconds.
if err = downloadStream.SetReadDeadline(time.Now().Add(2 * time.Second)); err != nil {
log.Fatal(err)
}
fileBuffer := bytes.NewBuffer(nil)
if _, err := io.Copy(fileBuffer, downloadStream); err != nil {
log.Fatal(err)
}
}
func ExampleBucket_DownloadToStream() {
var bucket *gridfs.Bucket
var fileID primitive.ObjectID
fileBuffer := bytes.NewBuffer(nil)
if _, err := bucket.DownloadToStream(fileID, fileBuffer); err != nil {
log.Fatal(err)
}
}
func ExampleBucket_Delete() {
var bucket *gridfs.Bucket
var fileID primitive.ObjectID
if err := bucket.Delete(fileID); err != nil {
log.Fatal(err)
}
}
func ExampleBucket_Find() {
var bucket *gridfs.Bucket
// Specify a filter to find all files with a length greater than 1000 bytes.
filter := bson.D{
{"length", bson.D{{"$gt", 1000}}},
}
cursor, err := bucket.Find(filter)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := cursor.Close(context.TODO()); err != nil {
log.Fatal(err)
}
}()
type gridfsFile struct {
Name string `bson:"filename"`
Length int64 `bson:"length"`
}
var foundFiles []gridfsFile
if err = cursor.All(context.TODO(), &foundFiles); err != nil {
log.Fatal(err)
}
for _, file := range foundFiles {
fmt.Printf("filename: %s, length: %d\n", file.Name, file.Length)
}
}
func ExampleBucket_Rename() {
var bucket *gridfs.Bucket
var fileID primitive.ObjectID
if err := bucket.Rename(fileID, "new file name"); err != nil {
log.Fatal(err)
}
}
func ExampleBucket_Drop() {
var bucket *gridfs.Bucket
if err := bucket.Drop(); err != nil {
log.Fatal(err)
}
}