Last active
October 14, 2020 03:22
-
-
Save tientp-floware/4a8bbbcc71f2ce98cc2f0249ce8ede3c to your computer and use it in GitHub Desktop.
Upload large file into Mongodb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"context" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"path" | |
"time" | |
"go.mongodb.org/mongo-driver/bson" | |
"go.mongodb.org/mongo-driver/mongo" | |
"go.mongodb.org/mongo-driver/mongo/gridfs" | |
"go.mongodb.org/mongo-driver/mongo/options" | |
) | |
func InitiateMongoClient() *mongo.Client { | |
var err error | |
var client *mongo.Client | |
uri := "mongodb://localhost:27017" | |
opts := options.Client() | |
opts.ApplyURI(uri) | |
opts.SetMaxPoolSize(5) | |
if client, err = mongo.Connect(context.Background(), opts); err != nil { | |
fmt.Println(err.Error()) | |
} | |
return client | |
} | |
func UploadFile(file, filename string) { | |
data, err := ioutil.ReadFile(file) | |
if err != nil { | |
log.Fatal(err) | |
} | |
conn := InitiateMongoClient() | |
bucket, err := gridfs.NewBucket( | |
conn.Database("myfiles"), | |
) | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
uploadStream, err := bucket.OpenUploadStream( | |
filename, | |
) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
defer uploadStream.Close() | |
fileSize, err := uploadStream.Write(data) | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
log.Printf("Write file to DB was successful. File size: %d\n", fileSize) | |
} | |
func DownloadFile(fileName string) { | |
conn := InitiateMongoClient() | |
// For CRUD operations, here is an example | |
db := conn.Database("myfiles") | |
fsFiles := db.Collection("fs.files") | |
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) | |
var results bson.M | |
err := fsFiles.FindOne(ctx, bson.M{}).Decode(&results) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// you can print out the results | |
fmt.Println(results) | |
bucket, _ := gridfs.NewBucket( | |
db, | |
) | |
var buf bytes.Buffer | |
dStream, err := bucket.DownloadToStreamByName(fileName, &buf) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("File size to download: %v\n", dStream) | |
ioutil.WriteFile(fileName, buf.Bytes(), 0600) | |
} | |
func main() { | |
// Get os.Args values | |
file := os.Args[1] //os.Args[1] = testfile.zip | |
filename := path.Base(file) | |
UploadFile(file, filename) | |
// Uncomment the below line and comment the UploadFile above this line to download the file | |
//DownloadFile(filename) | |
} | |
// next step will integrate to | |
// https://github.com/ulule/gostorages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment