C# code for rclone copy

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