Skip to content

Commit

Permalink
增加京东云的支持。
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjusss committed Sep 12, 2018
1 parent 91fb59d commit a7e7fba
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 5 deletions.
105 changes: 105 additions & 0 deletions Backup2Cloud/Worker/Uploader/JDCloudUploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Backup2Cloud.Worker.Uploader
{
/// <summary>
/// 京东云对象存储上传实现类。
/// </summary>
[ProviderName("JDCloud")]
public class JDCloudUploader : IUploader
{
/// <summary>
/// 对象存储空间的外网访问域名
/// </summary>
public string domain;
/// <summary>
/// 京东云 Access Key ID
/// </summary>
public string accessKeyId;
/// <summary>
/// 京东云 Secret Access Key
/// </summary>
public string secretAccessKey;
/// <summary>
/// 文件在Bucket下的路径,作为上传路径前缀。
/// </summary>
public string path;
/// <summary>
/// 提示信息
/// </summary>
public string Tips
{
get
{
return "domain:对象存储空间的外网访问域名,在 对象存储-空间管理-空间信息 里查看,例如backup.oss.cn-east-2.jcloudcs.com" +
"accessKeyId:Access Key ID;" +
"secretAccessKey:Secret Access Key;" +
"path:文件在Bucket/桶下的路径前缀,例如\"data/some\",最终会生成类似\"data/some201809092054.zip\"之类的文件";
}
}
/// <summary>
/// 获取示例实例。
/// </summary>
/// <returns>示例配置实例</returns>
public IUploader GetExample()
{
return new JDCloudUploader()
{
domain = "backup.oss.cn-east-2.jcloudcs.com",
accessKeyId = "xxxx",
secretAccessKey = "yyy",
path = "data/file"
};
}

/// <summary>
/// 上传指定文件。
/// </summary>
/// <param name="file">待上传的文件</param>
/// <param name="suffix">文件在云空间里的后缀</param>
/// <exception cref="Exception"/>
public async Task Upload(string file, string suffix)
{
string pattern = @"^([^\.]+)\.oss\.([^\.]+)\.jcloudcs\.com$";
var matches = Regex.Matches(domain.Trim(), pattern);
if (matches.Count != 1)
{
throw new Exception(string.Format("域名{0}不符合京东云对象存储空间的外网访问域名的格式。", domain));
}

var groups = matches[0].Groups;
string bucket = groups[1].Value;
string systemName = groups[2].Value;
string serverUrl = "http://s3." + systemName + ".jcloudcs.com";
AmazonS3Config config = new AmazonS3Config()
{
UseHttp = true,
ServiceURL = serverUrl,
SignatureVersion = "v4"
};

using (FileStream stream = new FileStream(file, FileMode.Open))
{
using (AmazonS3Client client = new AmazonS3Client(accessKeyId, secretAccessKey, config))
{
PutObjectRequest request = new PutObjectRequest()
{
BucketName = bucket,
Key = path + suffix,
InputStream = stream,
UseChunkEncoding = false
};

await client.PutObjectAsync(request);
}
}
}
}
}
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![GitHub license](https://img.shields.io/github/license/sanjusss/backup2cloud.svg)](https://github.com/sanjusss/backup2cloud/blob/master/LICENSE)

# 用途
定时将服务器上的文件或文件夹备份到网络云存储(阿里云oss或腾讯云cos)。
定时将服务器上的文件或文件夹备份到网络云存储(OSS或S3)。
可以设置在多个时间点,备份多个文件或文件夹到多个网络云存储。

# 支持的云存储
Expand All @@ -20,7 +20,8 @@
[七牛云对象存储](https://www.qiniu.com/products/kodo)
[AWS S3](https://amazonaws-china.com/cn/s3/)
[UCloud对象存储(UFile)](https://www.ucloud.cn/site/product/ufile.html)
[华为云OBS](https://www.huaweicloud.com/product/obs.html)
[华为云OBS](https://www.huaweicloud.com/product/obs.html)
[京东云](https://www.jdcloud.com/products/cloudstorag)

# 使用方法(Docker)
### 运行
Expand Down Expand Up @@ -50,7 +51,6 @@ docker run --rm sanjusss/backup2cloud --help
docker run --rm sanjusss/backup2cloud --version
```


# 使用方法(Windows)
### 下载
[点我打开软件下载页面](https://github.com/sanjusss/backup2cloud/releases/latest)
Expand Down Expand Up @@ -80,7 +80,6 @@ backup2cloud --help
backup2cloud --version
```


# 引用项目
https://www.newtonsoft.com/json
https://github.com/vla/aliyun-oss-csharp-sdk
Expand Down
36 changes: 35 additions & 1 deletion example.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"awsSecretAccessKey": "yyy",
"bucket": "backup",
"path": "data/file",
"Tips": "regionSystemName:区域的系统名,例如 us-west-1awsAccessKeyId:AWS Access Key ID;awsSecretAccessKey:AWS Secret Access Key;bucket:存储空间名;path:文件在存储空间下的路径前缀,例如\"data/some\",最终会生成类似\"data/some201809092054.zip\"之类的文件"
"Tips": "regionSystemName:区域的系统名,例如 us-west-1awsAccessKeyId:AWS Access Key ID;awsSecretAccessKey:AWS Secret Access Key;bucket:Bucket/桶;path:文件在Bucket/桶下的路径前缀,例如\"data/some\",最终会生成类似\"data/some201809092054.zip\"之类的文件"
},
"Tips": "name:任务名称;provider:上传服务提供商;path:需要备份的文件夹或文件在本地的路径;crontab:启动备份的时间集合,可以参考http://cron.qqe2.com/,使用时需要注意时区;uploader:上传设置"
},
Expand All @@ -52,6 +52,40 @@
},
"Tips": "name:任务名称;provider:上传服务提供商;path:需要备份的文件夹或文件在本地的路径;crontab:启动备份的时间集合,可以参考http://cron.qqe2.com/,使用时需要注意时区;uploader:上传设置"
},
{
"name": "上传到 huawei",
"provider": "huawei",
"path": "/data",
"crontab": [
"0,30 * * * * ?"
],
"uploader": {
"endpoint": "obs.cn-north-1.myhwclouds.com",
"accessKeyId": "xxxx",
"secretAccessKey": "yyyy",
"bucket": "backup",
"path": "data/some",
"deleteAfterDays": 20,
"Tips": "endpoint:地域节点(可以在控制台查看);accessKeyId:华为云 Access Key Id(可以在控制台查看);secretAccessKey:华为云 Secret Access Key(可以在控制台查看);bucket:桶名称path:文件在存储空间下的路径前缀,例如\"data/some\",最终会生成类似\"data/some201809092054.zip\"之类的文件;deleteAfterDays:多少天后自动删除,可以不设置"
},
"Tips": "name:任务名称;provider:上传服务提供商;path:需要备份的文件夹或文件在本地的路径;crontab:启动备份的时间集合,可以参考http://cron.qqe2.com/,使用时需要注意时区;uploader:上传设置"
},
{
"name": "上传到 JDCloud",
"provider": "JDCloud",
"path": "/data",
"crontab": [
"0,30 * * * * ?"
],
"uploader": {
"domain": "backup.oss.cn-east-2.jcloudcs.com",
"accessKeyId": "xxxx",
"secretAccessKey": "yyy",
"path": "data/file",
"Tips": "domain:对象存储空间的外网访问域名,在 对象存储-空间管理-空间信息 里查看,例如backup.oss.cn-east-2.jcloudcs.comaccessKeyId:Access Key ID;secretAccessKey:Secret Access Key;path:文件在Bucket/桶下的路径前缀,例如\"data/some\",最终会生成类似\"data/some201809092054.zip\"之类的文件"
},
"Tips": "name:任务名称;provider:上传服务提供商;path:需要备份的文件夹或文件在本地的路径;crontab:启动备份的时间集合,可以参考http://cron.qqe2.com/,使用时需要注意时区;uploader:上传设置"
},
{
"name": "上传到 qcloud",
"provider": "qcloud",
Expand Down

0 comments on commit a7e7fba

Please sign in to comment.