# Need example RClone copy using golang

**URL:** <https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121>\
**Category:** Dev Discussions\
**Created:** [September 15, 2020, 4:14am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121 "2020-09-15T04:14:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 15, 2020, 4:14am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/1 "2020-09-15T04:14:03Z")

</div>

Hi,

I already read the golang docs about rclone. but still clueless about the implementation as the title said. need help the sample rclone copy using golang script.

Thanks.

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 15, 2020, 8:32pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/2 "2020-09-15T20:32:34Z")

</div>

You want to call rclone from go?

You could call it as an external command using the exec module.

That would be easy.

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 16, 2020, 12:40am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/3 "2020-09-16T00:40:13Z")

</div>

Hi Nick,

Yes sure I want to call rclone from go. Exec command is great, but it had often got permission issue.

Thanks

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 16, 2020, 1:16pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/4 "2020-09-16T13:16:53Z")

</div>

What exactly do you want to do? Just copy a file? Or control the whole of rclone?

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 16, 2020, 1:54pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/5 "2020-09-16T13:54:15Z")

</div>

The idea is that we had our own application which we need rclone to perform such as copy. but on our side need to avoid using command Exec inorder to not installing them on our server for better maintenance.

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 17, 2020, 8:56am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/6 "2020-09-17T08:56:39Z")

</div>

besides all of our application which we used rclone are dockerized. so its not a good idea to use cmd exec in golang script.

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 17, 2020, 9:58am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/7 "2020-09-17T09:58:44Z")

</div>

There are several levels you can hook into rclone

If you don't mind using the rclone config system then it is quite easy

```auto
package main

import (
        "context"
        "fmt"
        "log"

        //_ "github.com/rclone/rclone/backend/drive"
        _ "github.com/rclone/rclone/backend/all"
        "github.com/rclone/rclone/fs"
)

func main() {
        f, err := fs.NewFs("drive:")
        if err != nil {
                log.Fatal(err)
        }
        entries, err := f.List(context.Background(), "")
        if err != nil {
                log.Fatal(err)
        }
        fmt.Println(entries)
        for i, o := range entries {
                fmt.Printf("object[%i] = (%T) %v\n", i, o, o)
        }
}

```

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 17, 2020, 10:17am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/8 "2020-09-17T10:17:31Z")

</div>

Hi nick,

```
Actually what we need is only the copy script in golang which it can also include the file what we want to search as well. the rest like listing that you are pointed out is no problem from us. 

```

Thanks

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 17, 2020, 11:09am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/9 "2020-09-17T11:09:51Z")

</div>

If you want to copy a file then you want `operations.Copy`. You'll need to make an `Fs` for the source and dest, find the object you wish to copy then call operations.Copy

> **[Package operations](https://godoc.org/github.com/rclone/rclone/fs/operations#Copy)**
>
> Package operations does generic operations on filesystems and objects

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 17, 2020, 11:24am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/10 "2020-09-17T11:24:44Z")

</div>

Then how about like include that only wildcards like example "\*.csv" to copy then?

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 17, 2020, 1:03pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/11 "2020-09-17T13:03:18Z")

</div>

You can set up the [Filter](https://godoc.org/github.com/rclone/rclone/fs/filter) or filter the objects yourself from the directory listings.

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 17, 2020, 4:01pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/12 "2020-09-17T16:01:47Z")

</div>

we did the code like this:

```
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/rclone/rclone/fs"
	"github.com/rclone/rclone/fs/filter"
)

func main() {
	var DefaultOpt = filter.Opt{
		MinAge: fs.DurationOff,
		MaxAge: fs.DurationOff,
		MinSize: fs.SizeSuffix(-1),
		MaxSize: fs.SizeSuffix(-1),
		IncludeRule: []string{"*.csv"},
	}
	_, err := filter.NewFilter(&DefaultOpt)
	if err != nil {
		log.Fatal(err.Error())
	}
	fsource, err := fs.NewFs("henrio test:")
	if err != nil {
		log.Fatal(err)
	}
	entries, err := fsource.List(context.Background(), "")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(entries)
}

```

it turns out the message like this :

> 2020/09/17 22:52:13 didn't find section in config file

so how I supposed to do? we are not just starting the copy yet, but we are looking the files that are matched with included wildcards?

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 17, 2020, 9:39pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/13 "2020-09-17T21:39:05Z")

</div>

Did you run rclone config to make a remote called "henrio test" ?

That is what the error means I think.

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 18, 2020, 12:36am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/14 "2020-09-18T00:36:22Z")

</div>

actually yes. I did the config run

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 18, 2020, 1:01am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/15 "2020-09-18T01:01:33Z")

</div>

Got a new error after I change the default config to custom config file. the current config file looks like this :

> [henrio test]  
> type = drive  
> scope = drive  
> root\_folder\_id = root\_folder\_id  
> token = {"access\_token":"access\_token","token\_type":"Bearer","refresh\_token":"refresh\_token","expiry":"2020-09-17T12:05:22.621859978+07:00"}  
> client\_id = clientid  
> client\_secret = secret

this is the updated code :

```
package main

import (
	"log"

	"github.com/rclone/rclone/fs"
	"github.com/rclone/rclone/fs/config"
	"github.com/rclone/rclone/fs/filter"
)

func main() {
	var DefaultOpt = filter.Opt{
		MinAge: fs.DurationOff,
		MaxAge: fs.DurationOff,
		MinSize: fs.SizeSuffix(-1),
		MaxSize: fs.SizeSuffix(-1),
		IncludeRule: []string{"*.csv"},
	}
	_, err := filter.NewFilter(&DefaultOpt)
	if err != nil {
		log.Fatal(err.Error())
	}
	config.ConfigPath = "/home/henrio/.myconfig"
	res, err := fs.NewFs("henrio test:")
	if err != nil {
		log.Fatal(err)
	}
	panic(res)
	
} 

```

the message are like this :

> 2020/09/18 07:55:51 didn't find backend called "drive"

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 18, 2020, 9:09am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/16 "2020-09-18T09:09:37Z")

</div>

> [@henrio](#):
>
> 2020/09/18 07:55:51 didn't find backend called "drive"

You need to import the backends you want - see the example above. So if you just want the drive backend and the local backend

```auto
import (
        _ "github.com/rclone/rclone/backend/drive"
        _ "github.com/rclone/rclone/backend/local"
)

```

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 18, 2020, 12:57pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/17 "2020-09-18T12:57:23Z")

</div>

The error is not shown anymore. thank you. but when I want to include only wildcard \*.xls instead of \*.csv to copy, the \*.csv file also got copied. what should I do next?

```
package main

import (
	"context"
	"fmt"
	"log"

	_ "github.com/rclone/rclone/backend/drive"
	_ "github.com/rclone/rclone/backend/local"
	"github.com/rclone/rclone/fs"
	"github.com/rclone/rclone/fs/config"
	"github.com/rclone/rclone/fs/filter"
	"github.com/rclone/rclone/fs/sync"
)

func main() {
	var DefaultOpt = filter.Opt{
		MinAge: fs.DurationOff,
		MaxAge: fs.DurationOff,
		MinSize: fs.SizeSuffix(-1),
		MaxSize: fs.SizeSuffix(-1),
		IncludeRule: []string{"*.xls"},
	}
	_, err := filter.NewFilter(&DefaultOpt)
	if err != nil {
		log.Fatal(err.Error())
	}
	config.ConfigPath = "/home/henrio/.myconfig"
	fsource, err := fs.NewFs("henrio test:")
	if err != nil {
		log.Fatal(err)
	}
	fdest, err := fs.NewFs("/home/henrio/datas")
	if err != nil {
		log.Fatal(err)
	}
	entries, err := fsource.List(context.Background(), "")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(entries)
	sync.CopyDir(context.Background(), fdest, fsource, true)
}
```

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 18, 2020, 4:59pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/18 "2020-09-18T16:59:55Z")

</div>

> [@henrio](#):
>
> ```auto
> _, err := filter.NewFilter(&DefaultOpt)
> if err != nil {
> log.Fatal(err.Error())
> }
> 
> ```

You need to set the global filter `filter.Active` to the result of `filter.NewFilter(&DefaultOpt)`

---

<div class="post-metadata">

**Author:** ![henrio](https://forum.rclone.org/letter_avatar_proxy/v4/letter/h/ee59a6/32.png) [@henrio](https://forum.rclone.org/u/henrio)\
**Post date:** [September 19, 2020, 6:55am UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/19 "2020-09-19T06:55:57Z")

</div>

> [@henrio](#):
>
> ```auto
> fsource, err := fs.NewFs("henrio test:")
> if err != nil {
> log.Fatal(err)
> }
> 
> ```

okay. thank you the copy it works really well.

---

<div class="post-metadata">

**Author:** ![ncw](https://forum.rclone.org/user_avatar/forum.rclone.org/ncw/32/18_2.png) [@ncw](https://forum.rclone.org/u/ncw)\
**Post date:** [September 19, 2020, 3:22pm UTC](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121/20 "2020-09-19T15:22:03Z")

</div>

Great! Make sure you either vendor rclone or use go modules to pin the version as the backend interface can change.

[Next page](https://forum.rclone.org/t/need-example-rclone-copy-using-golang/19121.md?page=2)
