C# code for rclone copy

What is the problem you are having with rclone?

I need to run rclone copy command using c#

Run the command 'rclone version' and share the full output of the command.

rclone v1.59.2

  • os/version: Microsoft Windows 10 Pro 21H2 (64 bit)
  • os/kernel: 10.0.19044.2251 (x86_64)
  • os/type: windows
  • os/arch: amd64
  • go/version: go1.18.6
  • go/linking: static
  • go/tags: cmount
yes

Which cloud storage system are you using? (eg Google Drive)

idrive e2

The command you were trying to run (eg rclone copy /tmp remote:tmp)

rclone copy code

Paste command here

The rclone config contents with secrets removed.

Paste config here

A log from the command with the -vv flag

Paste  log here

Hi Akhil,

Here is an example to get you started:

I am using VS Code with REST Client to call the rclone REST API exposed by rclone rcd.

Once satisfied with my request (to the left and middle) I select the code, right click and choose "Generate Code Snippet", select C# and then get the output to the right. You can see rclone rcd running with debug info the bottom.

I find this the easiest way to start using the rclone API. Once you master this you may want to consider if sync/copy (possibly combined with async execution) fits better for your use case.

If very advanced then you could also consider using the in process calls using the librclone library. Note: librclone doesn't support the above core/command call, only sync/copy.

Here is the HTTP REST code used in the above example:

POST http://localhost:5572/core/command HTTP/1.1
content-type: application/json

{
    "command": "copy",
    "arg": [
        "myRemote:", 
        "../testfolder1", 
        "--dry-run", 
        "--log-level=INFO",
        "--use-json-log=false" 
        ],
    "returnType": "STREAM"
}

and the corresponding code in C#:

var client = new HttpClient();
var request = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    RequestUri = new Uri("http://localhost:5572/core/command"),
    Headers =
    {
        { "user-agent", "vscode-restclient" },
    },
    Content = new StringContent("{\"command\": \"copy\",\"arg\": [\"myRemote:\",\"../testfolder1\",\"--dry-run\",\"--log-level=INFO\",\"--use-json-log=false\"],\"returnType\": \"STREAM\"}")
    {
        Headers =
        {
            ContentType = new MediaTypeHeaderValue("application/json")
        }
    }
};
using (var response = await client.SendAsync(request))
{
    response.EnsureSuccessStatusCode();
    var body = await response.Content.ReadAsStringAsync();
    Console.WriteLine(body);
}

I hope this helps!

1 Like

That REST Client is cool! I wonder if it can generate Go code?

I must try VS Code one day. I've been using emacs for the last 20+ years but it never hurts to look at the competition!

can you share how to programmatically read the copy progress in c# from bucket to bucket

Yes, sure:

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "http://localhost:5572/core/command"

	payload := strings.NewReader("{\"command\": \"copy\",\"arg\": [\"myRemote:\",\"../testfolder1\",\"--dry-run\",\"--log-level=INFO\",\"--use-json-log=false\"],\"returnType\": \"STREAM\"}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("user-agent", "vscode-restclient")
	req.Header.Add("content-type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}

Looks fine to me.

You certainly should, I will happily share my setup and tips.

I think you will like the many possibilities and smooth integration, probably "the swiss army knife of coding" :smile:

1 Like

Hello Ole
Can you please provide c# code to copy from one cloud to another could with using C# code .. I need to use that code in a project

Sure, are you student or paid?

OK, what is your project, learning objectives and deadline?

Thanks Akhil, just making sure I am not solving your entire project.

Here is an example, that should give you a good starting point:

The REST calls are:

POST http://localhost:5572/sync/copy HTTP/1.1
content-type: application/json

{
    "srcFs": "myRemote:mybucket",
    "dstFs": "./testfolder5",
    "_config": { "DryRun": false, "IgnoreTimes": true },
    "_async": true,
    "_group": "myCopy/1"
}

###

POST http://localhost:5572/core/stats HTTP/1.1
content-type: application/json

{
    "group": "myCopy/1"
}

###

POST http://localhost:5572/job/stopgroup HTTP/1.1
content-type: application/json

{
    "group": "myCopy/1"
}

I think you can easily translate them into C# based on the above. Remember to start "rclone rcd --rc-no-auth" first.

Happy coding and good luck! :sweat_smile:

Hello ole it worked fine. But how get copy progress output has "stream". I have used "returnType":"STREAM" but it's not working

Hello Akhil,

Great, sounds like you are using the core/command example I gave you first, where you get the output (stream) in the response.

I may have confused you by changing from the simple core/command request used in the first example to the more advanced sync/copy request.

I changed because you need to use the sync/copy command to be able to get the copy progress. It will return immediately with an OK (job started) and then you can use repeated core/stats requests to monitor the progress.

Here is a step by step guide to use sync/copy and then monitor status:

First start "rclone rcd --rc-no-auth", then initiate the copy something like this (note the URL):

it will return almost immediately and then you can monitor status with repeated calls something like this:

you can see the returned status in the middle of my above screenshot.

I think you can easily translate the above REST requests into C# by editing the example I gave for the core/command request.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.